*/
struct peer_atom
{
+ tr_peer * peer; /* will be NULL if not connected */
uint8_t from;
uint8_t flags; /* these match the added_f flags */
uint8_t myflags; /* flags that aren't defined in added_f */
return tr_compareAddresses( tr_peerAddress( a ), tr_peerAddress( b ) );
}
-static int
-peerCompareToAddr( const void * a, const void * vb )
-{
- return tr_compareAddresses( tr_peerAddress( a ), vb );
-}
-
-static tr_peer*
-getExistingPeer( Torrent * torrent,
- const tr_address * addr )
-{
- assert( torrentIsLocked( torrent ) );
- assert( addr );
-
- return tr_ptrArrayFindSorted( &torrent->peers, addr, peerCompareToAddr );
-}
-
static struct peer_atom*
getExistingAtom( const Torrent * t,
const tr_address * addr )
}
static tr_bool
-peerIsInUse( const Torrent * ct,
- const tr_address * addr )
+peerIsInUse( const Torrent * ct, const struct peer_atom * atom )
{
Torrent * t = (Torrent*) ct;
assert( torrentIsLocked ( t ) );
- return getExistingPeer( t, addr )
- || getExistingHandshake( &t->outgoingHandshakes, addr )
- || getExistingHandshake( &t->manager->incomingHandshakes, addr );
+ return ( atom->peer != NULL )
+ || getExistingHandshake( &t->outgoingHandshakes, &atom->addr )
+ || getExistingHandshake( &t->manager->incomingHandshakes, &atom->addr );
}
static tr_peer*
tr_peer * peer = tr_new0( tr_peer, 1 );
tr_bitsetConstructor( &peer->have, 0 );
peer->atom = atom;
+ atom->peer = peer;
+ peer->atom = atom;
return peer;
}
assert( torrentIsLocked( torrent ) );
- peer = getExistingPeer( torrent, &atom->addr );
+ peer = atom->peer;
if( peer == NULL )
{
tr_bitsetDestructor( &peer->have );
tr_bitfieldFree( peer->blame );
tr_free( peer->client );
+ peer->atom->peer = NULL;
tr_free( peer );
}
}
else
{
- tr_peer * peer = getExistingPeer( t, addr );
+ tr_peer * peer = atom->peer;
if( peer ) /* we already have this peer */
{
continue;
/* we don't need two connections to the same peer... */
- if( peerIsInUse( t, &atom->addr ) )
+ if( peerIsInUse( t, atom ) )
continue;
/* no need to connect if we're both seeds... */
/* keep the ones that are in use */
for( i=0; i<atomCount; ++i ) {
struct peer_atom * atom = atoms[i];
- if( peerIsInUse( t, &atom->addr ) )
+ if( peerIsInUse( t, atom ) )
keep[keepCount++] = atom;
else
test[testCount++] = atom;