source: trunk/libtransmission/peermessages.h @ 1555

Last change on this file since 1555 was 1555, checked in by joshe, 15 years ago

Send the correct bitfield in the bitfield message.

  • Property svn:keywords set to Date Rev Author Id
File size: 10.0 KB
Line 
1/******************************************************************************
2 * $Id: peermessages.h 1555 2007-03-09 01:43:10Z joshe $
3 *
4 * Copyright (c) 2005-2006 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25/***********************************************************************
26 * This file handles all outgoing messages
27 **********************************************************************/
28
29#define PEER_MSG_CHOKE          0
30#define PEER_MSG_UNCHOKE        1
31#define PEER_MSG_INTERESTED     2
32#define PEER_MSG_UNINTERESTED   3
33#define PEER_MSG_HAVE           4
34#define PEER_MSG_BITFIELD       5
35#define PEER_MSG_REQUEST        6
36#define PEER_MSG_PIECE          7
37#define PEER_MSG_CANCEL         8
38#define PEER_MSG_PORT           9
39
40static uint8_t * messagesPending( tr_peer_t * peer, int * size )
41{
42    if( peer->outBlockSending || peer->outMessagesPos < 1 )
43    {
44        return NULL;
45    }
46
47    *size = MIN( peer->outMessagesPos, 1024 );
48
49    return peer->outMessages;
50}
51
52static void messagesSent( tr_peer_t * peer, int size )
53{
54    peer->outMessagesPos -= size;
55    memmove( peer->outMessages, &peer->outMessages[size],
56             peer->outMessagesPos );
57}
58
59static uint8_t * blockPending( tr_torrent_t * tor, tr_peer_t * peer,
60                               int * size )
61{
62    if( !peer->outBlockLoaded )
63    {
64        uint8_t * p;
65        tr_request_t * r;
66
67        if( peer->amChoking || peer->outRequestCount < 1 )
68        {
69            /* No piece to send */
70            return NULL;
71        }
72
73        /* We need to load the block for the next request */
74        r = &peer->outRequests[0];
75
76        /* Sanity check */
77        if( !tr_cpPieceIsComplete( tor->completion, r->index ) )
78        {
79            /* We have been asked for something we don't have, buggy client?
80               Let's just drop this request */
81            tr_inf( "Block %d/%d/%d was requested but we don't have it",
82                    r->index, r->begin, r->length );
83            (peer->outRequestCount)--;
84            memmove( &peer->outRequests[0], &peer->outRequests[1],
85                     peer->outRequestCount * sizeof( tr_request_t ) );
86            return NULL;
87        }
88       
89        p = (uint8_t *) peer->outBlock;
90
91        TR_HTONL( 9 + r->length, p );
92        p[4] = PEER_MSG_PIECE;
93        TR_HTONL( r->index, p + 5 );
94        TR_HTONL( r->begin, p + 9 );
95
96        tr_ioRead( tor->io, r->index, r->begin, r->length, &p[13] );
97
98        if( peer->outRequestCount < 1 )
99        {
100            /* We were choked during the read */
101            return NULL;
102        }
103
104        peer_dbg( "SEND piece %d/%d (%d bytes)",
105                  r->index, r->begin, r->length );
106
107        peer->outBlockSize   = 13 + r->length;
108        peer->outBlockLoaded = 1;
109
110        (peer->outRequestCount)--;
111        memmove( &peer->outRequests[0], &peer->outRequests[1],
112                 peer->outRequestCount * sizeof( tr_request_t ) );
113    }
114
115    *size = MIN( 1024, peer->outBlockSize );
116
117    return (uint8_t *) peer->outBlock;
118}
119
120static void blockSent( tr_peer_t * peer, int size )
121{
122    peer->outBlockSize -= size;
123    memmove( peer->outBlock, &peer->outBlock[size], peer->outBlockSize );
124
125    if( peer->outBlockSize > 0 )
126    {
127        /* We can't send messages until we are done sending the block */
128        peer->outBlockSending = 1;
129    }
130    else
131    {
132        /* Block fully sent */
133        peer->outBlockSending = 0;
134        peer->outBlockLoaded  = 0;
135    }
136}
137
138static uint8_t * getMessagePointer( tr_peer_t * peer, int size, int id )
139{
140    uint8_t * p;
141
142    size += 4;
143    if( 0 <= id )
144    {
145        size++;
146    }
147
148    if( peer->outMessagesPos + size > peer->outMessagesSize )
149    {
150        peer->outMessagesSize = peer->outMessagesPos + size;
151        peer->outMessages     = realloc( peer->outMessages,
152                                         peer->outMessagesSize );
153    }
154
155    p                     = &peer->outMessages[peer->outMessagesPos];
156    peer->outMessagesPos += size;
157
158    TR_HTONL( size - 4, p );
159    p += 4;
160    if( 0 <= id )
161    {
162        *p = id;
163        p++;
164    }
165
166    return p;
167}
168
169/***********************************************************************
170 * sendKeepAlive
171 ***********************************************************************
172 *
173 **********************************************************************/
174static void sendKeepAlive( tr_peer_t * peer )
175{
176    uint8_t * p;
177
178    p = getMessagePointer( peer, 0, -1 );
179
180    peer_dbg( "SEND keep-alive" );
181}
182
183
184/***********************************************************************
185 * sendChoke
186 ***********************************************************************
187 *
188 **********************************************************************/
189static void sendChoke( tr_peer_t * peer, int yes )
190{
191    uint8_t * p;
192    int       id;
193
194    id = ( yes ? PEER_MSG_CHOKE : PEER_MSG_UNCHOKE );
195    p = getMessagePointer( peer, 0, id );
196
197    peer->amChoking = yes;
198
199    if( !yes )
200    {
201        /* Drop older requests from the last time it was unchoked,
202           if any */
203        peer->outRequestCount = 0;
204    }
205
206    peer_dbg( "SEND %schoke", yes ? "" : "un" );
207}
208
209/***********************************************************************
210 * sendInterest
211 ***********************************************************************
212 *
213 **********************************************************************/
214static void sendInterest( tr_peer_t * peer, int yes )
215{
216    uint8_t * p;
217    int       id;
218
219    id = ( yes ? PEER_MSG_INTERESTED : PEER_MSG_UNINTERESTED );
220    p = getMessagePointer( peer, 0, id );
221
222    peer->amInterested = yes;
223
224    peer_dbg( "SEND %sinterested", yes ? "" : "un" );
225}
226
227/***********************************************************************
228 * sendHave
229 ***********************************************************************
230 *
231 **********************************************************************/
232static void sendHave( tr_peer_t * peer, int piece )
233{
234    uint8_t * p;
235
236    p = getMessagePointer( peer, 4, PEER_MSG_HAVE );
237
238    TR_HTONL( piece, p );
239
240    peer_dbg( "SEND have %d", piece );
241}
242
243/***********************************************************************
244 * sendBitfield
245 ***********************************************************************
246 * Builds a 'bitfield' message:
247 *  - size = 5 + X (4 bytes)
248 *  - id   = 5     (1 byte)
249 *  - bitfield     (X bytes)
250 **********************************************************************/
251static void sendBitfield( tr_torrent_t * tor, tr_peer_t * peer )
252{
253    uint8_t       * p;
254    tr_bitfield_t * bitfield;
255
256    bitfield = tr_cpPieceBitfield( tor->completion );
257    p = getMessagePointer( peer, bitfield->len, PEER_MSG_BITFIELD );
258
259    memcpy( p, bitfield->bits, bitfield->len );
260
261    peer_dbg( "SEND bitfield" );
262}
263
264/***********************************************************************
265 * sendRequest
266 ***********************************************************************
267 *
268 **********************************************************************/
269static void sendRequest( tr_torrent_t * tor, tr_peer_t * peer, int block )
270{
271    tr_info_t * inf = &tor->info;
272    tr_request_t * r;
273    uint8_t * p;
274
275    /* Get the piece the block is a part of, its position in the piece
276       and its size */
277    r         = &peer->inRequests[peer->inRequestCount];
278    r->index  = block / ( inf->pieceSize / tor->blockSize );
279    r->begin  = ( block % ( inf->pieceSize / tor->blockSize ) ) *
280                    tor->blockSize;
281    r->length = tr_blockSize( block );
282    (peer->inRequestCount)++;
283
284    /* Build the "ask" message */
285    p = getMessagePointer( peer, 12, PEER_MSG_REQUEST );
286
287    TR_HTONL( r->index,  p     );
288    TR_HTONL( r->begin,  p + 4 );
289    TR_HTONL( r->length, p + 8 );
290
291    tr_cpDownloaderAdd( tor->completion, block );
292
293    peer_dbg( "SEND request %d/%d (%d bytes)",
294              r->index, r->begin, r->length );
295}
296
297/***********************************************************************
298 * sendCancel
299 ***********************************************************************
300 *
301 **********************************************************************/
302static void sendCancel( tr_torrent_t * tor, int block )
303{
304    int i, j;
305    uint8_t * p;
306    tr_peer_t * peer;
307    tr_request_t * r;
308
309    for( i = 0; i < tor->peerCount; i++ )
310    {
311        peer = tor->peers[i];
312
313        for( j = 1; j < peer->inRequestCount; j++ )
314        {
315            r = &peer->inRequests[j];
316
317            if( block != tr_block( r->index, r->begin ) )
318            {
319                continue;
320            }
321
322            p = getMessagePointer( peer, 12, PEER_MSG_CANCEL );
323       
324            /* Build the "cancel" message */
325            TR_HTONL( r->index,  p     );
326            TR_HTONL( r->begin,  p + 4 );
327            TR_HTONL( r->length, p + 8 );
328
329            peer_dbg( "SEND cancel %d/%d (%d bytes)",
330                      r->index, r->begin, r->length );
331
332            (peer->inRequestCount)--;
333            memmove( &peer->inRequests[j], &peer->inRequests[j+1],
334                     ( peer->inRequestCount - j ) * sizeof( tr_request_t ) );
335            break;
336        }
337    }
338}
Note: See TracBrowser for help on using the repository browser.