Changeset 3057


Ignore:
Timestamp:
Sep 13, 2007, 4:37:08 PM (15 years ago)
Author:
charles
Message:

I think this winds up the last few days of pulling my hair out. The memory debugger is finally giving the current codebase a clean bill of health, so I can get back to my "must do before merging" checklist. PS. if anyone wants to profile this with shark, I'd appreciate it.

Location:
branches/encryption/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/encryption/libtransmission/peer-mgr.c

    r3055 r3057  
    193193getExistingPeer( Torrent * torrent, const struct in_addr * in_addr )
    194194{
     195    assert( torrent != NULL );
     196    assert( torrent->peers != NULL );
     197    assert( in_addr != NULL );
     198
    195199    return (tr_peer*) tr_ptrArrayFindSorted( torrent->peers,
    196200                                             in_addr,
     
    202206{
    203207    tr_peer * peer = getExistingPeer( torrent, in_addr );
     208
    204209    if( peer == NULL )
    205210    {
     
    251256    int i, size;
    252257    tr_peer ** peers;
     258    uint8_t hash[SHA_DIGEST_LENGTH];
     259
    253260fprintf( stderr, "timer freeTorrent %p\n", t );
    254261
     
    256263    assert( t != NULL );
    257264    assert( t->peers != NULL );
     265    assert( getExistingTorrent( manager, t->hash ) != NULL );
     266
     267    memcpy( hash, t->hash, SHA_DIGEST_LENGTH );
     268
     269    tr_timerFree( &t->chokeTimer );
     270    tr_timerFree( &t->refillTimer );
    258271
    259272    peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &size );
    260 fprintf( stderr, "timer CHOKE %p being freed\n", t->chokeTimer );
    261     tr_timerFree( &t->chokeTimer );
    262 fprintf( stderr, "timer REFILL %p being freed\n", t->refillTimer );
    263     tr_timerFree( &t->refillTimer );
    264273    for( i=0; i<size; ++i )
    265274        freePeer( peers[i] );
     275
    266276    tr_ptrArrayFree( t->peers );
    267277    tr_ptrArrayRemoveSorted( manager->torrents, t, torrentCompare );
    268278    tr_free( t->blocks );
    269279    tr_free( t );
     280
     281    assert( getExistingTorrent( manager, hash ) == NULL );
    270282}
    271283
     
    495507    const uint8_t * hash = NULL;
    496508    Torrent * t;
     509    tr_handshake * ours;
    497510
    498511    assert( io != NULL );
    499512
    500     tr_ptrArrayRemoveSorted( manager->handshakes, handshake, tr_comparePointers );
     513    ours = tr_ptrArrayRemoveSorted( manager->handshakes,
     514                                    handshake,
     515                                    tr_comparePointers );
     516    assert( handshake == ours );
    501517
    502518    in_addr = tr_peerIoGetAddress( io, &port );
     
    511527    hash = tr_peerIoGetTorrentHash( io );
    512528    t = getExistingTorrent( manager, hash );
    513     if( !t )
     529    if( !t || !t->isRunning )
    514530    {
    515531        tr_peerIoFree( io );
     
    525541        tr_peer * peer = getExistingPeer( t, in_addr );
    526542        fprintf( stderr, "peer-mgr: torrent [%s] got a bad one, and you know what? fuck them.\n", t->tor->info.name );
     543        tr_peerIoFree( io );
     544        --manager->connectionCount;
    527545        if( peer ) {
    528546            tr_ptrArrayRemoveSorted( t->peers, peer, peerCompare );
    529547            freePeer( peer );
    530         } else  {
    531             tr_peerIoFree( io );
    532548        }
    533         --manager->connectionCount;
    534549        return;
    535550    }
    536 
    537 #if 0
    538     /* ONLY DO THIS TEST FOR INCOMING CONNECTIONS */
    539     /* check for duplicates */
    540     if( getExistingPeer( t, in_addr ) ) {
    541         tr_dbg( "dropping a duplicate connection... dropping." );
    542         tr_peerIoFree( io );
    543         return;
    544     }
    545 #endif
    546551
    547552    if( 1 ) {
    548553        tr_peer * peer = getPeer( t, in_addr );
    549         peer->port = port;
    550         peer->io = io;
    551         peer->msgs = tr_peerMsgsNew( t->tor, peer );
    552 fprintf( stderr, "PUB sub peer %p to msgs %p\n", peer, peer->msgs );
    553         peer->msgsTag = tr_peerMsgsSubscribe( peer->msgs, msgsCallbackFunc, t );
    554         chokePulse( t );
    555     }
     554        if( peer->msgs != NULL ) { /* we alerady have this peer */
     555            tr_peerIoFree( io );
     556            --manager->connectionCount;
     557        } else {
     558            peer->port = port;
     559            peer->io = io;
     560            peer->msgs = tr_peerMsgsNew( t->tor, peer );
     561            fprintf( stderr, "PUB sub peer %p to msgs %p\n", peer, peer->msgs );
     562            peer->msgsTag = tr_peerMsgsSubscribe( peer->msgs, msgsCallbackFunc, t );
     563            chokePulse( t );
     564        }
     565    }
     566}
     567
     568static void
     569initiateHandshake( tr_peerMgr * manager, tr_peerIo * io )
     570{
     571    tr_handshake * handshake = tr_handshakeNew( io,
     572                                                HANDSHAKE_ENCRYPTION_PREFERRED,
     573                                                myHandshakeDoneCB,
     574                                                manager );
     575    ++manager->connectionCount;
     576
     577    tr_ptrArrayInsertSorted( manager->handshakes, handshake, tr_comparePointers );
    556578}
    557579
     
    561583                       int               socket )
    562584{
    563     tr_handshake * handshake;
    564 
    565     ++manager->connectionCount;
    566 
    567     handshake = tr_handshakeNew( tr_peerIoNewIncoming( manager->handle, addr, socket ),
    568                                  HANDSHAKE_ENCRYPTION_PREFERRED,
    569                                  myHandshakeDoneCB,
    570                                  manager );
    571 
    572     tr_ptrArrayInsertSorted( manager->handshakes, handshake, tr_comparePointers );
     585    tr_peerIo * io = tr_peerIoNewIncoming( manager->handle, addr, socket );
     586    initiateHandshake( manager, io );
    573587}
    574588
     
    576590maybeConnect( tr_peerMgr * manager, Torrent * t, tr_peer * peer )
    577591{
    578     if( tr_peerMgrIsAcceptingConnections( manager ) )
    579     {
    580         fprintf( stderr, "peer-mgr: torrent [%s] is handshaking with a new peer %08x:%04x\n",
    581                  t->tor->info.name,
    582                  (uint32_t) peer->in_addr.s_addr, peer->port );
    583 
    584         ++manager->connectionCount;
    585 
    586         peer->io = tr_peerIoNewOutgoing( manager->handle,
    587                                          &peer->in_addr,
    588                                          peer->port,
    589                                          t->hash );
    590 
    591         tr_handshakeNew( peer->io, HANDSHAKE_ENCRYPTION_PREFERRED,
    592                          myHandshakeDoneCB, manager );
    593     }
     592    tr_peerIo * io;
     593
     594    assert( manager != NULL );
     595    assert( t != NULL );
     596    assert( peer != NULL );
     597
     598    if( peer->io != NULL ) { /* already connected */
     599        fprintf( stderr, "not connecting because we already have an IO for that address\n" );
     600        return;
     601    }
     602    if( !t->isRunning ) { /* torrent's not running */
     603        fprintf( stderr, "OUTGOING connection not being made because t [%s] is not running\n", t->tor->info.name );
     604        return;
     605    }
     606
     607fprintf( stderr, "OUTGOING OUTGOING OUTGOING OUTGOING connection trying to connect to the peer...\n" );
     608
     609    io = tr_peerIoNewOutgoing( manager->handle,
     610                               &peer->in_addr,
     611                               peer->port,
     612                               t->hash );
     613    initiateHandshake( manager, io );
    594614}
    595615
     
    696716
    697717void
    698 tr_peerMgrStartTorrent( tr_peerMgr     * manager UNUSED,
    699                         const uint8_t  * torrentHash UNUSED)
    700 {
     718tr_peerMgrStartTorrent( tr_peerMgr     * manager,
     719                        const uint8_t  * torrentHash )
     720{
     721    int i, peerCount;
    701722    Torrent * t = getExistingTorrent( manager, torrentHash );
    702     t->isRunning = TRUE;
     723
     724    t->isRunning = 1;
     725
     726    peerCount = tr_ptrArraySize( t->peers );
     727    for( i=0; i<peerCount; ++i )
     728        maybeConnect( manager, t, tr_ptrArrayNth( t->peers, i ) );
    703729}
    704730
     
    707733                       const uint8_t  * torrentHash)
    708734{
    709     int i, size;
     735    int i, peerCount;
    710736    Torrent * t = getExistingTorrent( manager, torrentHash );
    711     tr_peer ** peers = getConnectedPeers( t, &size );
    712 
    713     t->isRunning = FALSE;
    714 
    715     for( i=0; i<size; ++i )
    716         disconnectPeer( peers[i] );
     737
     738    t->isRunning = 0;
     739
     740    peerCount = tr_ptrArraySize( t->peers );
     741    for( i=0; i<peerCount; ++i )
     742        disconnectPeer( tr_ptrArrayNth( t->peers, i ) );
    717743}
    718744
  • branches/encryption/libtransmission/torrent.c

    r3049 r3057  
    273273    tor->startDate = tr_date();
    274274    tr_trackerStart( tor->tracker );
     275    tr_peerMgrStartTorrent( tor->handle->peerMgr, tor->info.hash );
    275276}
    276277
Note: See TracChangeset for help on using the changeset viewer.