if we're seeding, disconnect from other seeds (part 1 of 2)

This commit is contained in:
Charles Kerr 2007-09-20 23:07:36 +00:00
parent f3b991cd78
commit 379beed969
4 changed files with 42 additions and 18 deletions

View File

@ -28,6 +28,7 @@ typedef struct tr_peer
unsigned int clientIsChoked : 1;
unsigned int clientIsInterested : 1;
unsigned int peerSupportsEncryption : 1;
unsigned int doDisconnect : 1;
struct in_addr in_addr;
uint16_t port;

View File

@ -522,8 +522,9 @@ broadcastGotBlock( Torrent * t, uint32_t index, uint32_t offset, uint32_t length
}
static void
msgsCallbackFunc( void * source UNUSED, void * vevent, void * vt )
msgsCallbackFunc( void * vpeer, void * vevent, void * vt )
{
tr_peer * peer = vpeer;
Torrent * t = (Torrent *) vt;
const tr_peermsgs_event * e = (const tr_peermsgs_event *) vevent;
@ -540,12 +541,22 @@ msgsCallbackFunc( void * source UNUSED, void * vevent, void * vt )
broadcastClientHave( t, e->pieceIndex );
break;
case TR_PEERMSG_PEER_PROGRESS: { /* if we're both seeds, then disconnect. */
const int clientIsSeed = tr_cpGetStatus( t->tor->completion ) != TR_CP_INCOMPLETE;
const int peerIsSeed = e->progress >= 1.0;
if( clientIsSeed && peerIsSeed ) {
fprintf( stderr, "DISCONNECTING FROM PEER because both of us are seeds\n" );
peer->doDisconnect = 1;
}
break;
}
case TR_PEERMSG_CLIENT_BLOCK:
broadcastGotBlock( t, e->pieceIndex, e->offset, e->length );
break;
case TR_PEERMSG_GOT_ERROR:
/* FIXME */
peer->doDisconnect = 1;
break;
default:

View File

@ -154,14 +154,20 @@ myDebug( const char * file, int line, const struct tr_peermsgs * msgs, const cha
*** EVENTS
**/
static const tr_peermsgs_event blankEvent = { 0, 0, 0, 0 };
static const tr_peermsgs_event blankEvent = { 0, 0, 0, 0, 0.0f };
static void
publishEvent( tr_peermsgs * peer, int eventType )
publish( tr_peermsgs * msgs, tr_peermsgs_event * e )
{
tr_publisherPublish( msgs->publisher, msgs->info, e );
}
static void
fireGotError( tr_peermsgs * msgs )
{
tr_peermsgs_event e = blankEvent;
e.eventType = eventType;
tr_publisherPublish( peer->publisher, peer, &e );
e.eventType = TR_PEERMSG_GOT_ERROR;
publish( msgs, &e );
}
static void
@ -169,7 +175,16 @@ fireNeedReq( tr_peermsgs * msgs )
{
tr_peermsgs_event e = blankEvent;
e.eventType = TR_PEERMSG_NEED_REQ;
tr_publisherPublish( msgs->publisher, msgs, &e );
publish( msgs, &e );
}
static void
firePeerProgress( tr_peermsgs * msgs )
{
tr_peermsgs_event e = blankEvent;
e.eventType = TR_PEERMSG_PEER_PROGRESS;
e.progress = msgs->info->progress;
publish( msgs, &e );
}
static void
@ -178,24 +193,18 @@ fireClientHave( tr_peermsgs * msgs, uint32_t pieceIndex )
tr_peermsgs_event e = blankEvent;
e.eventType = TR_PEERMSG_CLIENT_HAVE;
e.pieceIndex = pieceIndex;
tr_publisherPublish( msgs->publisher, msgs, &e );
publish( msgs, &e );
}
static void
fireGotBlock( tr_peermsgs * peer, uint32_t pieceIndex, uint32_t offset, uint32_t length )
fireGotBlock( tr_peermsgs * msgs, uint32_t pieceIndex, uint32_t offset, uint32_t length )
{
tr_peermsgs_event e = blankEvent;
e.eventType = TR_PEERMSG_CLIENT_BLOCK;
e.pieceIndex = pieceIndex;
e.offset = offset;
e.length = length;
tr_publisherPublish( peer->publisher, peer, &e );
}
static void
fireGotError( tr_peermsgs * peer )
{
publishEvent( peer, TR_PEERMSG_GOT_ERROR );
publish( msgs, &e );
}
/**
@ -647,6 +656,7 @@ readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf )
msgs->info->progress = tr_bitfieldCountTrueBits( msgs->info->have ) / (float)msgs->torrent->info.pieceCount;
dbgmsg( msgs, "after the HAVE message, peer progress is %f", msgs->info->progress );
updateInterest( msgs );
firePeerProgress( msgs );
break;
case BT_BITFIELD:
@ -657,7 +667,7 @@ readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf )
dbgmsg( msgs, "after the HAVE message, peer progress is %f", msgs->info->progress );
updateInterest( msgs );
fireNeedReq( msgs );
/* FIXME: maybe unchoke */
firePeerProgress( msgs );
break;
case BT_REQUEST: {
@ -1034,7 +1044,7 @@ didWrite( struct bufferevent * evin UNUSED, void * vpeer )
static void
gotError( struct bufferevent * evbuf UNUSED, short what UNUSED, void * vpeer )
{
fireGotError( (tr_peermsgs*)vpeer );
fireGotError( vpeer );
}
static void

View File

@ -58,6 +58,7 @@ typedef enum
{
TR_PEERMSG_CLIENT_HAVE,
TR_PEERMSG_CLIENT_BLOCK,
TR_PEERMSG_PEER_PROGRESS,
TR_PEERMSG_GOT_ERROR,
TR_PEERMSG_NEED_REQ
}
@ -69,6 +70,7 @@ typedef struct
uint32_t pieceIndex; /* for TR_PEERMSG_GOT_BLOCK, TR_PEERMSG_GOT_HAVE */
uint32_t offset; /* for TR_PEERMSG_GOT_BLOCK */
uint32_t length; /* for TR_PEERMSG_GOT_BLOCK */
float progress; /* for TR_PEERMSG_PEER_PROGRESS */
}
tr_peermsgs_event;