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