Changeset 3057
- Timestamp:
- Sep 13, 2007, 4:37:08 PM (15 years ago)
- Location:
- branches/encryption/libtransmission
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/encryption/libtransmission/peer-mgr.c
r3055 r3057 193 193 getExistingPeer( Torrent * torrent, const struct in_addr * in_addr ) 194 194 { 195 assert( torrent != NULL ); 196 assert( torrent->peers != NULL ); 197 assert( in_addr != NULL ); 198 195 199 return (tr_peer*) tr_ptrArrayFindSorted( torrent->peers, 196 200 in_addr, … … 202 206 { 203 207 tr_peer * peer = getExistingPeer( torrent, in_addr ); 208 204 209 if( peer == NULL ) 205 210 { … … 251 256 int i, size; 252 257 tr_peer ** peers; 258 uint8_t hash[SHA_DIGEST_LENGTH]; 259 253 260 fprintf( stderr, "timer freeTorrent %p\n", t ); 254 261 … … 256 263 assert( t != NULL ); 257 264 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 ); 258 271 259 272 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 );264 273 for( i=0; i<size; ++i ) 265 274 freePeer( peers[i] ); 275 266 276 tr_ptrArrayFree( t->peers ); 267 277 tr_ptrArrayRemoveSorted( manager->torrents, t, torrentCompare ); 268 278 tr_free( t->blocks ); 269 279 tr_free( t ); 280 281 assert( getExistingTorrent( manager, hash ) == NULL ); 270 282 } 271 283 … … 495 507 const uint8_t * hash = NULL; 496 508 Torrent * t; 509 tr_handshake * ours; 497 510 498 511 assert( io != NULL ); 499 512 500 tr_ptrArrayRemoveSorted( manager->handshakes, handshake, tr_comparePointers ); 513 ours = tr_ptrArrayRemoveSorted( manager->handshakes, 514 handshake, 515 tr_comparePointers ); 516 assert( handshake == ours ); 501 517 502 518 in_addr = tr_peerIoGetAddress( io, &port ); … … 511 527 hash = tr_peerIoGetTorrentHash( io ); 512 528 t = getExistingTorrent( manager, hash ); 513 if( !t )529 if( !t || !t->isRunning ) 514 530 { 515 531 tr_peerIoFree( io ); … … 525 541 tr_peer * peer = getExistingPeer( t, in_addr ); 526 542 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; 527 545 if( peer ) { 528 546 tr_ptrArrayRemoveSorted( t->peers, peer, peerCompare ); 529 547 freePeer( peer ); 530 } else {531 tr_peerIoFree( io );532 548 } 533 --manager->connectionCount;534 549 return; 535 550 } 536 537 #if 0538 /* 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 #endif546 551 547 552 if( 1 ) { 548 553 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 568 static void 569 initiateHandshake( 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 ); 556 578 } 557 579 … … 561 583 int socket ) 562 584 { 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 ); 573 587 } 574 588 … … 576 590 maybeConnect( tr_peerMgr * manager, Torrent * t, tr_peer * peer ) 577 591 { 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 607 fprintf( 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 ); 594 614 } 595 615 … … 696 716 697 717 void 698 tr_peerMgrStartTorrent( tr_peerMgr * manager UNUSED, 699 const uint8_t * torrentHash UNUSED) 700 { 718 tr_peerMgrStartTorrent( tr_peerMgr * manager, 719 const uint8_t * torrentHash ) 720 { 721 int i, peerCount; 701 722 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 ) ); 703 729 } 704 730 … … 707 733 const uint8_t * torrentHash) 708 734 { 709 int i, size;735 int i, peerCount; 710 736 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 ) ); 717 743 } 718 744 -
branches/encryption/libtransmission/torrent.c
r3049 r3057 273 273 tor->startDate = tr_date(); 274 274 tr_trackerStart( tor->tracker ); 275 tr_peerMgrStartTorrent( tor->handle->peerMgr, tor->info.hash ); 275 276 } 276 277
Note: See TracChangeset
for help on using the changeset viewer.