Changeset 2985


Ignore:
Timestamp:
Sep 7, 2007, 8:55:38 PM (15 years ago)
Author:
charles
Message:

work on incoming connections, and better deciding of which pieces to request first.

Location:
branches/encryption/libtransmission
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/encryption/libtransmission/crypto.c

    r2942 r2985  
    285285tr_cryptoGetTorrentHash( const tr_crypto * crypto )
    286286{
     287    assert( crypto != NULL );
    287288    assert( crypto->torrentHashIsSet );
    288289
    289290    return crypto->torrentHash;
    290291}
     292
     293int
     294tr_cryptoHasTorrentHash( const tr_crypto * crypto )
     295{
     296    assert( crypto != NULL );
     297
     298    return crypto->torrentHashIsSet ? 1 : 0;
     299}
  • branches/encryption/libtransmission/crypto.h

    r2942 r2985  
    4141const uint8_t*  tr_cryptoGetTorrentHash( const tr_crypto * crypto );
    4242
     43int tr_cryptoHasTorrentHash( const tr_crypto * crypto );
     44
    4345/**
    4446***
  • branches/encryption/libtransmission/handshake.c

    r2978 r2985  
    646646
    647647    /* pstrlen */
    648     evbuffer_remove( inbuf, &pstrlen, 1 );
    649     bytesRead++;
     648    pstrlen = EVBUFFER_DATA(inbuf)[0];
    650649    fprintf( stderr, "pstrlen 1 is %d [%c]\n", (int)pstrlen, pstrlen );
    651650    isEncrypted = pstrlen != 19;
     
    655654    if( isEncrypted ) {
    656655        fprintf( stderr, "I guess it's encrypted...\n" );
     656        if( tr_peerIoIsIncoming( handshake->io ) ) {
     657            setState( handshake, AWAITING_YA );
     658            return READ_AGAIN;
     659        }
    657660        tr_cryptoDecrypt( handshake->crypto, 1, &pstrlen, &pstrlen );
    658661    }
     662    bytesRead++;
     663    evbuffer_drain( inbuf, 1 );
    659664    fprintf( stderr, "pstrlen is %d [%c]\n", (int)pstrlen, pstrlen );
    660665    assert( pstrlen == 19 );
     
    674679    /* torrent hash */
    675680    tr_peerIoReadBytes( handshake->io, inbuf, hash, sizeof(hash) );
    676     assert( !memcmp( hash, tr_peerIoGetTorrentHash(handshake->io), SHA_DIGEST_LENGTH ) );
    677681    bytesRead += sizeof(hash);
     682    if( tr_peerIoIsIncoming( handshake->io ) )
     683    {
     684        assert( !tr_peerIoHasTorrentHash( handshake->io ) );
     685        tr_peerIoSetTorrentHash( handshake->io, hash );
     686    }
     687    else
     688    {
     689        assert( tr_peerIoHasTorrentHash( handshake->io ) );
     690        assert( !memcmp( hash, tr_peerIoGetTorrentHash(handshake->io), SHA_DIGEST_LENGTH ) );
     691    }
    678692
    679693    /* peer id */
  • branches/encryption/libtransmission/peer-io.c

    r2978 r2985  
    113113    c->rateToPeer = tr_rcInit( );
    114114    c->rateToClient = tr_rcInit( );
     115    c->isIncoming = isIncoming ? 1 : 0;
    115116fprintf( stderr, "io %p rates: peer %p client %p\n", c, c->rateToPeer, c->rateToClient );
    116117    c->bufev = bufferevent_new( c->socket,
     
    218219tr_peerIoIsIncoming( const tr_peerIo * c )
    219220{
    220     return c->isIncoming;
     221    return c->isIncoming ? 1 : 0;
    221222}
    222223
     
    260261                         const uint8_t * hash )
    261262{
     263    assert( io != NULL );
     264
    262265    tr_cryptoSetTorrentHash( io->crypto, hash );
    263266}
     
    266269tr_peerIoGetTorrentHash( tr_peerIo * io )
    267270{
     271    assert( io != NULL );
     272    assert( io->crypto != NULL );
     273
    268274    return tr_cryptoGetTorrentHash( io->crypto );
     275}
     276
     277int
     278tr_peerIoHasTorrentHash( const tr_peerIo * io )
     279{
     280    assert( io != NULL );
     281    assert( io->crypto != NULL );
     282
     283    return tr_cryptoHasTorrentHash( io->crypto );
    269284}
    270285
  • branches/encryption/libtransmission/peer-io.h

    r2971 r2985  
    7070const uint8_t*
    7171      tr_peerIoGetTorrentHash( tr_peerIo * io );
     72
     73int   tr_peerIoHasTorrentHash( const tr_peerIo * io );
    7274
    7375void  tr_peerIoSetTorrentHash( tr_peerIo      * io,
  • branches/encryption/libtransmission/peer-mgr.c

    r2984 r2985  
    220220        case TR_PEERMSG_GOT_HAVE: {
    221221            const uint32_t begin = tr_torPieceFirstBlock( t->tor, e->pieceIndex );
    222             const uint32_t end = begin + (uint32_t)tr_torPieceCountBlocks( t->tor, (int)e->pieceIndex );
     222            const uint32_t end = begin + tr_torPieceCountBlocks( t->tor, (int)e->pieceIndex );
    223223            uint32_t i;
    224224            for( i=begin; i<end; ++i ) {
     
    251251{
    252252    int ok = isConnected;
    253     const uint8_t * hash = tr_peerIoGetTorrentHash( io );
    254     tr_peerMgr * manager = (tr_peerMgr*) vmanager;
    255     Torrent * t = getExistingTorrent( manager, hash );
    256253    uint16_t port;
    257254    const struct in_addr * in_addr;
     255    tr_peerMgr * manager = (tr_peerMgr*) vmanager;
     256    const uint8_t * hash = NULL;
     257    Torrent * t;
     258
     259    assert( io != NULL );
     260
     261    in_addr = tr_peerIoGetAddress( io, &port );
     262
     263    if( !tr_peerIoHasTorrentHash( io ) ) /* incoming connection gone wrong? */
     264    {
     265        tr_peerIoFree( io );
     266        --manager->connectionCount;
     267        return;
     268    }
     269
     270    hash = tr_peerIoGetTorrentHash( io );
     271    t = getExistingTorrent( manager, hash );
    258272
    259273    fprintf( stderr, "peer-mgr: torrent [%s] finished a handshake; isConnected is %d\n", t->tor->info.name, isConnected );
    260 
    261     assert( io != NULL );
    262 
    263     in_addr = tr_peerIoGetAddress( io, &port );
    264274
    265275    /* if we couldn't connect or were snubbed,
     
    304314    ++manager->connectionCount;
    305315
     316fprintf( stderr, "peer-mgr: new INCOMING CONNECTION...\n" );
    306317    tr_handshakeAdd( tr_peerIoNewIncoming( manager->handle, addr, socket ),
    307318                     HANDSHAKE_ENCRYPTION_PREFERRED,
     
    319330                 (uint32_t) peer->in_addr.s_addr, peer->port );
    320331
    321         peer->io = tr_peerIoNewOutgoing( manager->handle, &peer->in_addr, peer->port, t->hash );
     332        ++manager->connectionCount;
     333
     334        peer->io = tr_peerIoNewOutgoing( manager->handle,
     335                                         &peer->in_addr,
     336                                         peer->port,
     337                                         t->hash );
    322338
    323339        tr_handshakeAdd( peer->io, HANDSHAKE_ENCRYPTION_PREFERRED,
     
    461477    for( i=0; i<t->blockCount; ++i ) {
    462478        t->blocks[i].block = i;
    463         t->blocks[i].scarcity = tr_cpPieceIsComplete( t->tor->completion, i )
    464                                                     ? UINT32_MAX : 0;
     479        t->blocks[i].scarcity = tr_cpBlockIsComplete( t->tor->completion, i )
     480                                                      ? UINT32_MAX : 0;
    465481    }
    466482    memcpy( t->hash, tor->info.hash, SHA_DIGEST_LENGTH );
  • branches/encryption/libtransmission/peer-msgs.c

    r2984 r2985  
    723723    size_t len;
    724724
    725 fprintf( stderr, "peer %p pulse... notlistening %d, outblock size: %d, outmessages size %d, peerAskedFor %p\n",
    726          vpeer,
    727          (int)peer->notListening,
    728          (int)EVBUFFER_LENGTH( peer->outBlock ),
    729          (int)EVBUFFER_LENGTH( peer->outMessages ),
    730          peer->peerAskedFor );
    731 
    732725    /* if we froze out a downloaded block because of speed limits,
    733726       start listening to the peer again */
     
    741734    if(( len = EVBUFFER_LENGTH( peer->outBlock ) ))
    742735    {
    743 fprintf( stderr, "peer %p needing to upload... canUpload %d\n", peer, canUpload(peer) );
    744736        if( canUpload( peer ) )
    745737        {
    746             const size_t outlen = MIN( len, 2048 );
    747 fprintf( stderr, "peer %p writing %d bytes...\n", peer, (int)outlen );
     738            const size_t outlen = MIN( len, 4096 );
     739fprintf( stderr, "peer %p outblock writing %d bytes...\n", peer, (int)outlen );
    748740            tr_peerIoWrite( peer->io, EVBUFFER_DATA(peer->outBlock), outlen );
    749741            evbuffer_drain( peer->outBlock, outlen );
     
    782774didWrite( struct bufferevent * evin UNUSED, void * vpeer )
    783775{
    784     tr_peermsgs * peer = (tr_peermsgs *) vpeer;
    785     fprintf( stderr, "peer %p got a didWrite...\n", peer );
    786     pulse( vpeer );
     776    pulse( (tr_peermsgs *) vpeer );
    787777}
    788778
  • branches/encryption/libtransmission/transmission.c

    r2956 r2985  
    9090    int           i;
    9191
     92#ifndef WIN32
     93    /* Don't exit when writing on a broken socket */
     94    signal( SIGPIPE, SIG_IGN );
     95#endif
     96
    9297    tr_msgInit();
    9398
     
    111116    for( i=0; i < TR_AZ_ID_LEN; ++i )
    112117        h->azId[i] = tr_rand( 0xff );
    113 
    114 #ifndef WIN32
    115     /* Don't exit when writing on a broken socket */
    116     signal( SIGPIPE, SIG_IGN );
    117 #endif
    118118
    119119    /* Initialize rate and file descripts controls */
Note: See TracChangeset for help on using the changeset viewer.