Changeset 3615
- Timestamp:
- Oct 28, 2007, 3:20:24 PM (15 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-msgs.c
r3606 r3615 71 71 KEEPALIVE_INTERVAL_SECS = 90, /* idle seconds before we send a keepalive */ 72 72 PEX_INTERVAL = (60 * 1000), /* msec between calls to sendPex() */ 73 PEER_PULSE_INTERVAL = (1 33),/* msec between calls to pulse() */73 PEER_PULSE_INTERVAL = (100), /* msec between calls to pulse() */ 74 74 RATE_PULSE_INTERVAL = (333), /* msec between calls to ratePulse() */ 75 75 }; … … 111 111 tr_publisher_t * publisher; 112 112 113 struct evbuffer * outBlock; /* buffer of all the current piece message */ 113 114 struct evbuffer * outMessages; /* buffer of all the non-piece messages */ 114 115 struct evbuffer * inBlock; /* the block we're currently receiving */ … … 233 234 tr_peerIoWriteUint32( io, out, sizeof(uint8_t) ); 234 235 tr_peerIoWriteUint8 ( io, out, choke ? BT_CHOKE : BT_UNCHOKE ); 235 }236 237 static void238 protocolSendPiece( tr_peermsgs * msgs,239 const struct peer_request * r,240 const uint8_t * pieceData )241 {242 tr_peerIo * io = msgs->io;243 struct evbuffer * out = evbuffer_new( );244 245 dbgmsg( msgs, "sending block %u:%u->%u", r->index, r->offset, r->length );246 tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + 2*sizeof(uint32_t) + r->length );247 tr_peerIoWriteUint8 ( io, out, BT_PIECE );248 tr_peerIoWriteUint32( io, out, r->index );249 tr_peerIoWriteUint32( io, out, r->offset );250 tr_peerIoWriteBytes ( io, out, pieceData, r->length );251 tr_peerIoWriteBuf ( io, out );252 253 evbuffer_free( out );254 236 } 255 237 … … 1364 1346 { 1365 1347 /* don't let our outbuffer get too large */ 1366 if( tr_peerIoWriteBytesWaiting( msgs->io ) > 8192)1348 if( tr_peerIoWriteBytesWaiting( msgs->io ) > 4096 ) 1367 1349 return FALSE; 1368 1350 … … 1370 1352 } 1371 1353 1372 static int 1373 canUpload( const tr_peermsgs * msgs ) 1374 { 1354 static size_t 1355 getUploadMax( const tr_peermsgs * msgs ) 1356 { 1357 static const size_t maxval = ~0; 1375 1358 const tr_torrent * tor = msgs->torrent; 1376 1359 1377 1360 if( !canWrite( msgs ) ) 1378 return FALSE;1361 return 0; 1379 1362 1380 1363 if( tor->uploadLimitMode == TR_SPEEDLIMIT_GLOBAL ) 1381 return !tor->handle->useUploadLimit || tr_rcCanTransfer( tor->handle->upload );1364 return tor->handle->useUploadLimit ? tr_rcBytesLeft( tor->handle->upload ) : maxval; 1382 1365 1383 1366 if( tor->uploadLimitMode == TR_SPEEDLIMIT_SINGLE ) 1384 return tr_rc CanTransfer( tor->upload );1385 1386 return TRUE;1367 return tr_rcBytesLeft( tor->upload ); 1368 1369 return maxval; 1387 1370 } 1388 1371 … … 1398 1381 } 1399 1382 1383 static struct peer_request* 1384 popNextRequest( tr_peermsgs * msgs ) 1385 { 1386 struct peer_request * ret; 1387 ret = tr_list_pop_front( &msgs->peerAskedForFast ); 1388 if( !ret ) 1389 ret = tr_list_pop_front( &msgs->peerAskedFor); 1390 return ret; 1391 } 1392 1400 1393 static int 1401 1394 pulse( void * vmsgs ) 1402 1395 { 1403 1396 const time_t now = time( NULL ); 1404 tr_peermsgs * msgs = (tr_peermsgs *) vmsgs; 1397 tr_peermsgs * msgs = vmsgs; 1398 struct peer_request * r; 1405 1399 size_t len; 1406 1400 … … 1418 1412 { 1419 1413 } 1414 else if(( len = EVBUFFER_LENGTH( msgs->outBlock ) )) 1415 { 1416 const size_t uploadMax = getUploadMax( msgs ); 1417 const size_t outlen = MIN( len, uploadMax ); 1418 tr_peerIoWrite( msgs->io, EVBUFFER_DATA( msgs->outBlock ), outlen ); 1419 evbuffer_drain( msgs->outBlock, outlen ); 1420 msgs->clientSentAnythingAt = now; 1421 peerGotBytes( msgs, outlen ); 1422 len -= outlen; 1423 dbgmsg( msgs, "wrote %d bytes; %d left in block", (int)outlen, (int)len ); 1424 fflush( stdout ); 1425 } 1420 1426 else if(( len = EVBUFFER_LENGTH( msgs->outMessages ) )) 1421 1427 { … … 1423 1429 msgs->clientSentAnythingAt = now; 1424 1430 } 1425 else if( msgs->peerAskedForFast || msgs->peerAskedFor ) 1426 { 1427 if( canUpload( msgs ) ) 1431 else if( ( now - msgs->clientSentAnythingAt ) > KEEPALIVE_INTERVAL_SECS ) 1432 { 1433 sendKeepalive( msgs ); 1434 } 1435 1436 if( !EVBUFFER_LENGTH( msgs->outBlock ) 1437 && (( r = popNextRequest( msgs ))) 1438 && requestIsValid( msgs, r ) 1439 && tr_cpPieceIsComplete( msgs->torrent->completion, r->index ) ) 1440 { 1441 uint8_t * buf = tr_new( uint8_t, r->length ); 1442 1443 if( !tr_ioRead( msgs->torrent, r->index, r->offset, r->length, buf ) ) 1428 1444 { 1429 struct peer_request * r; 1430 uint8_t * buf; 1431 1432 r = tr_list_pop_front( &msgs->peerAskedForFast ); 1433 if( r == NULL ) 1434 r = tr_list_pop_front( &msgs->peerAskedFor); 1435 1436 buf = tr_new( uint8_t, r->length ); 1437 1438 if( requestIsValid( msgs, r ) 1439 && tr_cpPieceIsComplete( msgs->torrent->completion, r->index ) 1440 && !tr_ioRead( msgs->torrent, r->index, r->offset, r->length, buf ) ) 1441 { 1442 protocolSendPiece( msgs, r, buf ); 1443 peerGotBytes( msgs, r->length ); 1444 msgs->clientSentAnythingAt = now; 1445 } 1446 1447 tr_free( buf ); 1448 tr_free( r ); 1445 tr_peerIo * io = msgs->io; 1446 struct evbuffer * out = msgs->outBlock; 1447 1448 dbgmsg( msgs, "sending block %u:%u->%u", r->index, r->offset, r->length ); 1449 tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + 2*sizeof(uint32_t) + r->length ); 1450 tr_peerIoWriteUint8 ( io, out, BT_PIECE ); 1451 tr_peerIoWriteUint32( io, out, r->index ); 1452 tr_peerIoWriteUint32( io, out, r->offset ); 1453 tr_peerIoWriteBytes ( io, out, buf, r->length ); 1449 1454 } 1450 } 1451 else if( ( now - msgs->clientSentAnythingAt ) > KEEPALIVE_INTERVAL_SECS ) 1452 { 1453 sendKeepalive( msgs ); 1455 1456 tr_free( buf ); 1457 tr_free( r ); 1458 1459 pulse( msgs ); /* start sending it right away */ 1454 1460 } 1455 1461 … … 1661 1667 m->pexTimer = tr_timerNew( m->handle, pexPulse, m, PEX_INTERVAL ); 1662 1668 m->outMessages = evbuffer_new( ); 1669 m->outBlock = evbuffer_new( ); 1663 1670 m->inBlock = evbuffer_new( ); 1664 1671 m->peerAllowedPieces = NULL; … … 1725 1732 tr_list_free( &msgs->peerAskedFor, tr_free ); 1726 1733 evbuffer_free( msgs->outMessages ); 1734 evbuffer_free( msgs->outBlock ); 1727 1735 evbuffer_free( msgs->inBlock ); 1728 1736 tr_free( msgs->pex ); -
trunk/libtransmission/ratecontrol.c
r3105 r3615 113 113 } 114 114 115 size_t 116 tr_rcBytesLeft( const tr_ratecontrol * r ) 117 { 118 size_t bytes = 0; 119 120 if( r != NULL ) 121 { 122 float cur, max; 123 size_t kb; 124 125 tr_lockLock( (tr_lock*)r->lock ); 126 127 cur = rateForInterval( r, SHORT_INTERVAL_MSEC ); 128 max = r->limit; 129 kb = max>cur ? max-cur : 0; 130 bytes = kb * 1024u; 131 132 tr_lockUnlock( (tr_lock*)r->lock ); 133 } 134 135 return bytes; 136 } 137 115 138 float 116 139 tr_rcRate( const tr_ratecontrol * r ) -
trunk/libtransmission/ratecontrol.h
r3105 r3615 32 32 int tr_rcGetLimit( const tr_ratecontrol * ); 33 33 int tr_rcCanTransfer( const tr_ratecontrol * ); 34 size_t tr_rcBytesLeft( const tr_ratecontrol * ); 34 35 void tr_rcTransferred( tr_ratecontrol *, size_t byteCount ); 35 36 float tr_rcRate( const tr_ratecontrol * );
Note: See TracChangeset
for help on using the changeset viewer.