Saves a list of peers when you pause a torrent, and tries to reconnect to them next time you resume it

This commit is contained in:
Eric Petit 2006-11-09 04:45:14 +00:00
parent 52e66c96f8
commit 9b3802f23a
3 changed files with 82 additions and 0 deletions

View File

@ -50,6 +50,8 @@
#define FR_ID_DOWNLOADED 0x02
/* number of bytes uploaded */
#define FR_ID_UPLOADED 0x03
/* IPs and ports of connectable peers */
#define FR_ID_PEERS 0x04
/* macros for the length of various pieces of the progress data */
#define FR_MTIME_LEN( t ) \
@ -126,6 +128,7 @@ static void fastResumeSave( tr_io_t * io )
char * path;
uint8_t * buf;
uint64_t total;
int size;
buf = malloc( FR_PROGRESS_LEN( tor ) );
@ -167,6 +170,13 @@ static void fastResumeSave( tr_io_t * io )
total = tor->uploadedCur + tor->uploadedPrev;
fastResumeWriteData( FR_ID_UPLOADED, &total, 8, 1, file );
/* Write IPs and ports of connectable peers, if any */
if( ( size = tr_peerGetConnectable( tor, &buf ) ) > 0 )
{
fastResumeWriteData( FR_ID_PEERS, buf, size, 1, file );
free( buf );
}
fclose( file );
tr_dbg( "Resume file '%s' written", path );
@ -358,6 +368,20 @@ static int fastResumeLoad( tr_io_t * io )
}
break;
case FR_ID_PEERS:
{
uint8_t * buf = malloc( len );
if( 1 != fread( buf, len, 1, file ) )
{
free( buf );
fclose( file );
return 1;
}
tr_peerAddCompactMany( tor, buf, len );
free( buf );
continue;
}
default:
break;
}

View File

@ -150,6 +150,26 @@ void tr_peerAddCompact( tr_torrent_t * tor, struct in_addr addr,
addWithAddr( tor, addr, port );
}
/***********************************************************************
* tr_peerAddCompactMany
***********************************************************************
* Adds several peers in compact form
**********************************************************************/
void tr_peerAddCompactMany( tr_torrent_t * tor, uint8_t * buf, int len )
{
struct in_addr addr;
in_port_t port;
int i;
len /= 6;
for( i = 0; i < len; i++ )
{
memcpy( &addr, buf, 4 ); buf += 4;
memcpy( &port, buf, 2 ); buf += 2;
tr_peerAddCompact( tor, addr, port );
}
}
/***********************************************************************
* tr_peerInit
***********************************************************************
@ -616,3 +636,39 @@ void tr_peerBlame( tr_torrent_t * tor, tr_peer_t * peer,
}
tr_bitfieldRem( peer->blamefield, piece );
}
int tr_peerGetConnectable( tr_torrent_t * tor, uint8_t ** _buf )
{
int count = 0;
uint8_t * buf = malloc( 6 * tor->peerCount );
tr_peer_t * peer;
int i;
for( i = 0; i < tor->peerCount; i++ )
{
peer = tor->peers[i];
/* Skip peers for which the connection isn't established,
* and peers that came from incoming connections */
if( peer->status < PEER_STATUS_CONNECTED )
continue;
if( peer->incoming )
continue;
memcpy( &buf[count*6], &peer->addr, 4 );
memcpy( &buf[count*6+4], &peer->port, 2 );
count++;
}
if( count < 1 )
{
free( buf );
*_buf = NULL;
}
else
{
*_buf = buf;
}
return count * 6;
}

View File

@ -29,6 +29,7 @@ typedef struct tr_peer_s tr_peer_t;
void tr_peerAddOld ( tr_torrent_t *, char *, int );
void tr_peerAddCompact ( tr_torrent_t *, struct in_addr, in_port_t );
void tr_peerAddCompactMany( tr_torrent_t *, uint8_t *, int );
tr_peer_t * tr_peerInit ( struct in_addr, in_port_t, int );
void tr_peerAttach ( tr_torrent_t *, tr_peer_t * );
void tr_peerDestroy ( tr_fd_t *, tr_peer_t * );
@ -54,5 +55,6 @@ int tr_peerIsOptimistic ( tr_peer_t * );
void tr_peerBlame ( tr_torrent_t *, tr_peer_t *,
int piece, int success );
struct in_addr * tr_peerAddress ( tr_peer_t * );
int tr_peerGetConnectable( tr_torrent_t *, uint8_t ** );
#endif