Changeset 1060 for trunk/libtransmission


Ignore:
Timestamp:
Nov 9, 2006, 4:45:14 AM (15 years ago)
Author:
titer
Message:

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

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fastresume.h

    r931 r1060  
    5151/* number of bytes uploaded */
    5252#define FR_ID_UPLOADED          0x03
     53/* IPs and ports of connectable peers */
     54#define FR_ID_PEERS             0x04
    5355
    5456/* macros for the length of various pieces of the progress data */
     
    127129    uint8_t * buf;
    128130    uint64_t  total;
     131    int       size;
    129132
    130133    buf = malloc( FR_PROGRESS_LEN( tor ) );
     
    167170    total = tor->uploadedCur + tor->uploadedPrev;
    168171    fastResumeWriteData( FR_ID_UPLOADED, &total, 8, 1, file );
     172
     173    /* Write IPs and ports of connectable peers, if any */
     174    if( ( size = tr_peerGetConnectable( tor, &buf ) ) > 0 )
     175    {
     176        fastResumeWriteData( FR_ID_PEERS, buf, size, 1, file );
     177        free( buf );
     178    }
    169179
    170180    fclose( file );
     
    359369                break;
    360370
     371            case FR_ID_PEERS:
     372            {
     373                uint8_t * buf = malloc( len );
     374                if( 1 != fread( buf, len, 1, file ) )
     375                {
     376                    free( buf );
     377                    fclose( file );
     378                    return 1;
     379                }
     380                tr_peerAddCompactMany( tor, buf, len );
     381                free( buf );
     382                continue;
     383            }
     384
    361385            default:
    362386                break;
  • trunk/libtransmission/peer.c

    r931 r1060  
    152152
    153153/***********************************************************************
     154 * tr_peerAddCompactMany
     155 ***********************************************************************
     156 * Adds several peers in compact form
     157 **********************************************************************/
     158void tr_peerAddCompactMany( tr_torrent_t * tor, uint8_t * buf, int len )
     159{
     160    struct in_addr addr;
     161    in_port_t port;
     162    int i;
     163
     164    len /= 6;
     165    for( i = 0; i < len; i++ )
     166    {
     167        memcpy( &addr, buf, 4 ); buf += 4;
     168        memcpy( &port, buf, 2 ); buf += 2;
     169        tr_peerAddCompact( tor, addr, port );
     170    }
     171}
     172
     173/***********************************************************************
    154174 * tr_peerInit
    155175 ***********************************************************************
     
    617637    tr_bitfieldRem( peer->blamefield, piece );
    618638}
     639
     640int tr_peerGetConnectable( tr_torrent_t * tor, uint8_t ** _buf )
     641{
     642    int count = 0;
     643    uint8_t * buf = malloc( 6 * tor->peerCount );
     644    tr_peer_t * peer;
     645    int i;
     646
     647    for( i = 0; i < tor->peerCount; i++ )
     648    {
     649        peer = tor->peers[i];
     650
     651        /* Skip peers for which the connection isn't established,
     652         * and peers that came from incoming connections */
     653        if( peer->status < PEER_STATUS_CONNECTED )
     654            continue;
     655        if( peer->incoming )
     656            continue;
     657
     658        memcpy( &buf[count*6], &peer->addr, 4 );
     659        memcpy( &buf[count*6+4], &peer->port, 2 );
     660        count++;
     661    }
     662
     663    if( count < 1 )
     664    {
     665        free( buf );
     666        *_buf = NULL;
     667    }
     668    else
     669    {
     670        *_buf = buf;
     671    }
     672
     673    return count * 6;
     674}
  • trunk/libtransmission/peer.h

    r932 r1060  
    3030void        tr_peerAddOld        ( tr_torrent_t *, char *, int );
    3131void        tr_peerAddCompact    ( tr_torrent_t *, struct in_addr, in_port_t );
     32void        tr_peerAddCompactMany( tr_torrent_t *, uint8_t *, int );
    3233tr_peer_t * tr_peerInit          ( struct in_addr, in_port_t, int );
    3334void        tr_peerAttach        ( tr_torrent_t *, tr_peer_t * );
     
    5556                                   int piece, int success );
    5657struct in_addr * tr_peerAddress  ( tr_peer_t * );
     58int         tr_peerGetConnectable( tr_torrent_t *, uint8_t ** );
    5759
    5860#endif
Note: See TracChangeset for help on using the changeset viewer.