Changeset 9086
- Timestamp:
- Sep 10, 2009, 2:21:03 AM (13 years ago)
- Location:
- branches/1.7x/libtransmission
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1.7x/libtransmission/peer-mgr.c
r9051 r9086 1521 1521 #endif 1522 1522 1523 /* better goes first */ 1524 static int 1525 compareAtomsByUsefulness( const void * va, const void *vb ) 1526 { 1527 const struct peer_atom * a = * (const struct peer_atom**) va; 1528 const struct peer_atom * b = * (const struct peer_atom**) vb; 1529 1530 assert( ( 0 < a->from ) && ( a->from < TR_PEER_FROM__MAX ) ); 1531 assert( ( 0 < b->from ) && ( b->from < TR_PEER_FROM__MAX ) ); 1532 1533 if( a->piece_data_time != b->piece_data_time ) 1534 return a->piece_data_time > b->piece_data_time ? -1 : 1; 1535 if( a->from != b->from ) 1536 return a->from < b->from ? -1 : 1; 1537 if( a->numFails != b->numFails ) 1538 return a->numFails < b->numFails ? -1 : 1; 1539 1540 return 0; 1541 } 1542 1523 1543 int 1524 tr_peerMgrGetPeers( tr_torrent * tor, tr_pex ** setme_pex, uint8_t af )1544 tr_peerMgrGetPeers( tr_torrent * tor, tr_pex ** setme_pex, uint8_t af, int maxPeerCount ) 1525 1545 { 1526 1546 int count = 0; … … 1531 1551 { 1532 1552 int i; 1533 const struct peer_atom ** atoms = (const struct peer_atom**) tr_ptrArrayBase( &t->pool );1534 1553 const int atomCount = tr_ptrArraySize( &t->pool ); 1554 const int pexCount = MIN( atomCount, maxPeerCount ); 1555 const struct peer_atom ** atomsBase = (const struct peer_atom**) tr_ptrArrayBase( &t->pool ); 1556 struct peer_atom ** atoms = tr_memdup( atomsBase, atomCount * sizeof( struct peer_atom * ) ); 1535 1557 /* for now, this will waste memory on torrents that have both 1536 1558 * ipv6 and ipv4 peers */ … … 1538 1560 tr_pex * walk = pex; 1539 1561 1540 for( i=0; i<atomCount; ++i ) 1562 qsort( atoms, atomCount, sizeof( struct peer_atom * ), compareAtomsByUsefulness ); 1563 1564 for( i=0; i<atomCount && count<pexCount; ++i ) 1541 1565 { 1542 1566 const struct peer_atom * atom = atoms[i]; … … 1553 1577 1554 1578 assert( ( walk - pex ) == count ); 1555 qsort( pex, count, sizeof( tr_pex ), tr_pexCompare );1556 1579 *setme_pex = pex; 1580 1581 tr_free( atoms ); 1557 1582 } 1558 1583 -
branches/1.7x/libtransmission/peer-mgr.h
r8695 r9086 151 151 int tr_peerMgrGetPeers( tr_torrent * tor, 152 152 tr_pex ** setme_pex, 153 uint8_t af); 153 uint8_t af, 154 int maxPeerCount ); 154 155 155 156 void tr_peerMgrStartTorrent( tr_torrent * tor ); -
branches/1.7x/libtransmission/peer-msgs.c
r8963 r9086 70 70 TR_LTEP_PEX = 1, 71 71 72 72 MAX_PEX_PEER_COUNT = 100, 73 73 74 74 MIN_CHOKE_PERIOD_SEC = ( 10 ), … … 1976 1976 tr_pex * newPex = NULL; 1977 1977 tr_pex * newPex6 = NULL; 1978 const int newCount = tr_peerMgrGetPeers( msgs->torrent, &newPex, TR_AF_INET );1979 const int newCount6 = tr_peerMgrGetPeers( msgs->torrent, &newPex6, TR_AF_INET6 );1978 const int newCount = tr_peerMgrGetPeers( msgs->torrent, &newPex, TR_AF_INET, MAX_PEX_PEER_COUNT ); 1979 const int newCount6 = tr_peerMgrGetPeers( msgs->torrent, &newPex6, TR_AF_INET6, MAX_PEX_PEER_COUNT ); 1980 1980 1981 1981 /* build the diffs */ -
branches/1.7x/libtransmission/resume.c
r8930 r9086 59 59 #define KEY_PROGRESS_BITFIELD "bitfield" 60 60 61 enum 62 { 63 MAX_REMEMBERED_PEERS = 200 64 }; 65 61 66 static char* 62 67 getResumeFilename( const tr_torrent * tor ) … … 74 79 75 80 static void 76 savePeers( tr_benc * dict, 77 const tr_torrent * tor ) 81 savePeers( tr_benc * dict, const tr_torrent * tor ) 78 82 { 79 83 int count; 80 84 tr_pex * pex; 81 85 82 count = tr_peerMgrGetPeers( (tr_torrent*) tor, &pex, TR_AF_INET );86 count = tr_peerMgrGetPeers( (tr_torrent*) tor, &pex, TR_AF_INET, MAX_REMEMBERED_PEERS ); 83 87 if( count > 0 ) 84 88 tr_bencDictAddRaw( dict, KEY_PEERS, pex, sizeof( tr_pex ) * count ); 85 89 tr_free( pex ); 86 90 87 count = tr_peerMgrGetPeers( (tr_torrent*) tor, &pex, TR_AF_INET6 );91 count = tr_peerMgrGetPeers( (tr_torrent*) tor, &pex, TR_AF_INET6, MAX_REMEMBERED_PEERS ); 88 92 if( count > 0 ) 89 93 tr_bencDictAddRaw( dict, KEY_PEERS6, pex, sizeof( tr_pex ) * count ); … … 92 96 } 93 97 94 static uint64_t 95 loadPeers( tr_benc * dict, 96 tr_torrent * tor ) 98 static tr_bool 99 tr_isPex( const tr_pex * pex ) 100 { 101 return tr_isAddress( &pex->addr ) 102 && ( pex->flags & 3 ) == pex->flags; 103 } 104 105 static int 106 addPeers( tr_torrent * tor, const uint8_t * buf, int buflen ) 107 { 108 int i; 109 int numAdded = 0; 110 const int count = buflen / sizeof( tr_pex ); 111 112 for( i=0; i<count && numAdded<MAX_REMEMBERED_PEERS; ++i ) 113 { 114 tr_pex pex; 115 memcpy( &pex, buf + ( i * sizeof( tr_pex ) ), sizeof( tr_pex ) ); 116 if( tr_isPex( &pex ) ) 117 { 118 tr_peerMgrAddPex( tor, TR_PEER_FROM_CACHE, &pex ); 119 ++numAdded; 120 } 121 } 122 123 return numAdded; 124 } 125 126 127 static uint64_t 128 loadPeers( tr_benc * dict, tr_torrent * tor ) 97 129 { 98 130 uint64_t ret = 0; … … 102 134 if( tr_bencDictFindRaw( dict, KEY_PEERS, &str, &len ) ) 103 135 { 104 int i; 105 const int count = len / sizeof( tr_pex ); 106 for( i = 0; i < count; ++i ) 107 { 108 tr_pex pex; 109 memcpy( &pex, str + ( i * sizeof( tr_pex ) ), sizeof( tr_pex ) ); 110 tr_peerMgrAddPex( tor, TR_PEER_FROM_CACHE, &pex ); 111 } 112 tr_tordbg( tor, "Loaded %d IPv4 peers from resume file", count ); 136 const int numAdded = addPeers( tor, str, len ); 137 tr_tordbg( tor, "Loaded %d IPv4 peers from resume file", numAdded ); 113 138 ret = TR_FR_PEERS; 114 139 } … … 116 141 if( tr_bencDictFindRaw( dict, KEY_PEERS6, &str, &len ) ) 117 142 { 118 int i; 119 const int count = len / sizeof( tr_pex ); 120 for( i = 0; i < count; ++i ) 121 { 122 tr_pex pex; 123 memcpy( &pex, str + ( i * sizeof( tr_pex ) ), sizeof( tr_pex ) ); 124 tr_peerMgrAddPex( tor, TR_PEER_FROM_CACHE, &pex ); 125 } 126 tr_tordbg( tor, "Loaded %d IPv6 peers from resume file", count ); 143 const int numAdded = addPeers( tor, str, len ); 144 tr_tordbg( tor, "Loaded %d IPv6 peers from resume file", numAdded ); 127 145 ret = TR_FR_PEERS; 128 146 }
Note: See TracChangeset
for help on using the changeset viewer.