(trunk libT) #2318: improve the peer lists used in the .resume file and in PEX

This commit is contained in:
Charles Kerr 2009-08-07 05:41:33 +00:00
parent 24bb159bfe
commit b2e26ea90a
1 changed files with 28 additions and 31 deletions

View File

@ -1006,8 +1006,15 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt )
{ {
case TR_PEER_UPLOAD_ONLY: case TR_PEER_UPLOAD_ONLY:
/* update our atom */ /* update our atom */
if( peer ) if( peer ) {
peer->atom->uploadOnly = e->uploadOnly ? UPLOAD_ONLY_YES : UPLOAD_ONLY_NO; if( e->uploadOnly ) {
peer->atom->uploadOnly = UPLOAD_ONLY_YES;
peer->atom->flags |= ADDED_F_SEED_FLAG;
} else {
peer->atom->uploadOnly = UPLOAD_ONLY_NO;
peer->atom->flags &= ~ADDED_F_SEED_FLAG;
}
}
break; break;
case TR_PEER_NEED_REQ: case TR_PEER_NEED_REQ:
@ -1087,13 +1094,9 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt )
if( peer ) if( peer )
{ {
struct peer_atom * atom = peer->atom; struct peer_atom * atom = peer->atom;
const int peerIsSeed = e->progress >= 1.0; if( e->progress >= 1.0 ) {
if( peerIsSeed ) {
tordbg( t, "marking peer %s as a seed", tr_peerIoAddrStr( &atom->addr, atom->port ) ); tordbg( t, "marking peer %s as a seed", tr_peerIoAddrStr( &atom->addr, atom->port ) );
atom->flags |= ADDED_F_SEED_FLAG; atom->flags |= ADDED_F_SEED_FLAG;
} else {
tordbg( t, "marking peer %s as a non-seed", tr_peerIoAddrStr( &atom->addr, atom->port ) );
atom->flags &= ~ADDED_F_SEED_FLAG;
} }
} }
break; break;
@ -1514,6 +1517,7 @@ tr_pexCompare( const void * va, const void * vb )
return 0; return 0;
} }
#if 0
static int static int
peerPrefersCrypto( const tr_peer * peer ) peerPrefersCrypto( const tr_peer * peer )
{ {
@ -1525,53 +1529,46 @@ peerPrefersCrypto( const tr_peer * peer )
return tr_peerIoIsEncrypted( peer->io ); return tr_peerIoIsEncrypted( peer->io );
} }
#endif
int int
tr_peerMgrGetPeers( tr_torrent * tor, tr_peerMgrGetPeers( tr_torrent * tor, tr_pex ** setme_pex, uint8_t af )
tr_pex ** setme_pex,
uint8_t af)
{ {
int peersReturning = 0; int count = 0;
const Torrent * t = tor->torrentPeers; const Torrent * t = tor->torrentPeers;
managerLock( t->manager ); managerLock( t->manager );
{ {
int i; int i;
const tr_peer ** peers = (const tr_peer**) tr_ptrArrayBase( &t->peers ); const struct peer_atom ** atoms = (const struct peer_atom**) tr_ptrArrayBase( &t->pool );
const int peerCount = tr_ptrArraySize( &t->peers ); const int atomCount = tr_ptrArraySize( &t->pool );
/* for now, this will waste memory on torrents that have both /* for now, this will waste memory on torrents that have both
* ipv6 and ipv4 peers */ * ipv6 and ipv4 peers */
tr_pex * pex = tr_new0( tr_pex, peerCount ); tr_pex * pex = tr_new0( tr_pex, atomCount );
tr_pex * walk = pex; tr_pex * walk = pex;
for( i=0; i<peerCount; ++i ) for( i=0; i<atomCount; ++i )
{ {
const tr_peer * peer = peers[i]; const struct peer_atom * atom = atoms[i];
if( peer->addr.type == af ) if( atom->addr.type == af )
{ {
const struct peer_atom * atom = peer->atom; assert( tr_isAddress( &atom->addr ) );
walk->addr = atom->addr;
assert( tr_isAddress( &peer->addr ) ); walk->port = atom->port;
walk->addr = peer->addr; walk->flags = atom->flags;
walk->port = peer->port; ++count;
walk->flags = 0;
if( peerPrefersCrypto( peer ) )
walk->flags |= ADDED_F_ENCRYPTION_FLAG;
if( ( atom->uploadOnly == UPLOAD_ONLY_YES ) || ( peer->progress >= 1.0 ) )
walk->flags |= ADDED_F_SEED_FLAG;
++peersReturning;
++walk; ++walk;
} }
} }
assert( ( walk - pex ) == peersReturning ); assert( ( walk - pex ) == count );
qsort( pex, peersReturning, sizeof( tr_pex ), tr_pexCompare ); qsort( pex, count, sizeof( tr_pex ), tr_pexCompare );
*setme_pex = pex; *setme_pex = pex;
} }
managerUnlock( t->manager ); managerUnlock( t->manager );
return peersReturning; return count;
} }
static void static void