source: trunk/libtransmission/peermessages.h @ 1447

Last change on this file since 1447 was 1447, checked in by titer, 16 years ago

Fixes a race condition

  • Property svn:keywords set to Date Rev Author Id
File size: 10.0 KB
Line 
1/******************************************************************************
2 * $Id: peermessages.h 1447 2007-01-30 20:06:42Z titer $
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    int       bitfieldSize = ( tor->info.pieceCount + 7 ) / 8;
255
256    p = getMessagePointer( peer, bitfieldSize, PEER_MSG_BITFIELD );
257
258    memcpy( p, tr_cpPieceBitfield( tor->completion ), bitfieldSize );
259
260    peer_dbg( "SEND bitfield" );
261}
262
263/***********************************************************************
264 * sendRequest
265 ***********************************************************************
266 *
267 **********************************************************************/
268static void sendRequest( tr_torrent_t * tor, tr_peer_t * peer, int block )
269{
270    tr_info_t * inf = &tor->info;
271    tr_request_t * r;
272    uint8_t * p;
273
274    /* Get the piece the block is a part of, its position in the piece
275       and its size */
276    r         = &peer->inRequests[peer->inRequestCount];
277    r->index  = block / ( inf->pieceSize / tor->blockSize );
278    r->begin  = ( block % ( inf->pieceSize / tor->blockSize ) ) *
279                    tor->blockSize;
280    r->length = tr_blockSize( block );
281    (peer->inRequestCount)++;
282
283    /* Build the "ask" message */
284    p = getMessagePointer( peer, 12, PEER_MSG_REQUEST );
285
286    TR_HTONL( r->index,  p     );
287    TR_HTONL( r->begin,  p + 4 );
288    TR_HTONL( r->length, p + 8 );
289
290    tr_cpDownloaderAdd( tor->completion, block );
291
292    peer_dbg( "SEND request %d/%d (%d bytes)",
293              r->index, r->begin, r->length );
294}
295
296/***********************************************************************
297 * sendCancel
298 ***********************************************************************
299 *
300 **********************************************************************/
301static void sendCancel( tr_torrent_t * tor, int block )
302{
303    int i, j;
304    uint8_t * p;
305    tr_peer_t * peer;
306    tr_request_t * r;
307
308    for( i = 0; i < tor->peerCount; i++ )
309    {
310        peer = tor->peers[i];
311
312        for( j = 1; j < peer->inRequestCount; j++ )
313        {
314            r = &peer->inRequests[j];
315
316            if( block != tr_block( r->index, r->begin ) )
317            {
318                continue;
319            }
320
321            p = getMessagePointer( peer, 12, PEER_MSG_CANCEL );
322       
323            /* Build the "cancel" message */
324            TR_HTONL( r->index,  p     );
325            TR_HTONL( r->begin,  p + 4 );
326            TR_HTONL( r->length, p + 8 );
327
328            peer_dbg( "SEND cancel %d/%d (%d bytes)",
329                      r->index, r->begin, r->length );
330
331            (peer->inRequestCount)--;
332            memmove( &peer->inRequests[j], &peer->inRequests[j+1],
333                     ( peer->inRequestCount - j ) * sizeof( tr_request_t ) );
334            break;
335        }
336    }
337}
Note: See TracBrowser for help on using the repository browser.