Changeset 1058 for trunk/libtorrent/src/protocol/peer_connection_leech.cc
- Timestamp:
- 05/05/08 15:16:26 (4 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtorrent/src/protocol/peer_connection_leech.cc
r1043 r1058 51 51 52 52 #include "extensions.h" 53 #include "initial_seed.h" 53 54 #include "peer_connection_leech.h" 54 55 … … 66 67 void 67 68 PeerConnection<type>::initialize_custom() { 69 if (type == Download::CONNECTION_INITIAL_SEED) { 70 if (m_download->initial_seeding() == NULL) { 71 // Can't throw close_connection or network_error here, we're still 72 // initializing. So close the socket and let that kill it later. 73 get_fd().close(); 74 return; 75 } 76 77 m_download->initial_seeding()->new_peer(this); 78 } 68 79 // if (m_download->content()->chunks_completed() != 0) { 69 80 // m_up->write_bitfield(m_download->file_list()->bitfield()->size_bytes()); … … 78 89 void 79 90 PeerConnection<type>::update_interested() { 80 if (type == Download::CONNECTION_SEED)91 if (type != Download::CONNECTION_LEECH) 81 92 return; 82 93 … … 118 129 } 119 130 120 if (type == Download::CONNECTION_SEED)131 if (type != Download::CONNECTION_LEECH) 121 132 return true; 122 133 … … 193 204 switch (buf->read_8()) { 194 205 case ProtocolBase::CHOKE: 195 if (type == Download::CONNECTION_SEED)206 if (type != Download::CONNECTION_LEECH) 196 207 return true; 197 208 … … 213 224 214 225 case ProtocolBase::UNCHOKE: 215 if (type == Download::CONNECTION_SEED)226 if (type != Download::CONNECTION_LEECH) 216 227 return true; 217 228 … … 259 270 260 271 case ProtocolBase::PIECE: 261 if (type == Download::CONNECTION_SEED)272 if (type != Download::CONNECTION_LEECH) 262 273 throw communication_error("Received a piece but the connection is strictly for seeding."); 263 274 … … 382 393 383 394 case ProtocolRead::READ_PIECE: 384 if (type == Download::CONNECTION_SEED)395 if (type != Download::CONNECTION_LEECH) 385 396 return; 386 397 … … 402 413 403 414 case ProtocolRead::READ_SKIP_PIECE: 404 if (type == Download::CONNECTION_SEED)415 if (type != Download::CONNECTION_LEECH) 405 416 return; 406 417 … … 490 501 // request has been received while uninterested. The problem arises 491 502 // as they send unchoke before receiving interested. 492 if (type != Download::CONNECTION_SEED&& m_sendInterested && m_up->can_write_interested()) {503 if (type == Download::CONNECTION_LEECH && m_sendInterested && m_up->can_write_interested()) { 493 504 m_up->write_interested(m_downInterested); 494 505 m_sendInterested = false; 495 506 } 496 507 497 if (type != Download::CONNECTION_SEED&& m_tryRequest) {508 if (type == Download::CONNECTION_LEECH && m_tryRequest) { 498 509 if (!(m_tryRequest = !should_request()) && 499 510 !(m_tryRequest = try_request_pieces()) && … … 509 520 DownloadMain::have_queue_type* haveQueue = m_download->have_queue(); 510 521 511 if (type != Download::CONNECTION_SEED&&522 if (type == Download::CONNECTION_LEECH && 512 523 !haveQueue->empty() && 513 524 m_peerChunks.have_timer() <= haveQueue->front().first && … … 523 534 } 524 535 525 while (type != Download::CONNECTION_SEED && !m_peerChunks.cancel_queue()->empty() && m_up->can_write_cancel()) { 536 if (type == Download::CONNECTION_INITIAL_SEED && m_up->can_write_have()) 537 offer_chunk(); 538 539 while (type == Download::CONNECTION_LEECH && !m_peerChunks.cancel_queue()->empty() && m_up->can_write_cancel()) { 526 540 m_up->write_cancel(m_peerChunks.cancel_queue()->front()); 527 541 m_peerChunks.cancel_queue()->pop_front(); … … 534 548 } else if (!m_upChoke.choked() && 535 549 !m_peerChunks.upload_queue()->empty() && 536 m_up->can_write_piece()) { 550 m_up->can_write_piece() && 551 (type != Download::CONNECTION_INITIAL_SEED || should_upload())) { 537 552 write_prepare_piece(); 538 553 } … … 639 654 m_download->chunk_statistics()->received_have_chunk(&m_peerChunks, index, m_download->file_list()->chunk_size()); 640 655 656 if (type == Download::CONNECTION_INITIAL_SEED) 657 m_download->initial_seeding()->chunk_seen(index, this); 658 659 // Disconnect seeds when we are seeding (but not for initial seeding 660 // so that we keep accurate chunk statistics until that is done). 641 661 if (m_peerChunks.bitfield()->is_all_set()) { 642 if (type == Download::CONNECTION_SEED || m_download->file_list()->is_done()) 662 if (type == Download::CONNECTION_SEED || 663 (type != Download::CONNECTION_INITIAL_SEED && m_download->file_list()->is_done())) 643 664 throw close_connection(); 644 665 … … 646 667 } 647 668 648 if (type == Download::CONNECTION_SEED|| m_download->file_list()->is_done())669 if (type != Download::CONNECTION_LEECH || m_download->file_list()->is_done()) 649 670 return; 650 671 … … 677 698 } 678 699 679 // Explicit instatiation of the member functions and vtable. 700 template<> 701 void 702 PeerConnection<Download::CONNECTION_INITIAL_SEED>::offer_chunk() { 703 // If bytes left to send in this chunk minus bytes about to be sent is zero, 704 // assume the peer will have got the chunk completely. In that case we may 705 // get another one to offer if not enough other peers are interested even 706 // if the peer would otherwise still be blocked. 707 uint32_t bytesLeft = m_data.bytesLeft; 708 if (!m_peerChunks.upload_queue()->empty() && m_peerChunks.upload_queue()->front().index() == m_data.lastIndex) 709 bytesLeft -= m_peerChunks.upload_queue()->front().length(); 710 711 uint32_t index = m_download->initial_seeding()->chunk_offer(this, bytesLeft == 0 ? m_data.lastIndex : InitialSeeding::no_offer); 712 713 if (index == InitialSeeding::no_offer || index == m_data.lastIndex) 714 return; 715 716 m_up->write_have(index); 717 m_data.lastIndex = index; 718 m_data.bytesLeft = m_download->file_list()->chunk_index_size(index); 719 } 720 721 template<> 722 bool 723 PeerConnection<Download::CONNECTION_INITIAL_SEED>::should_upload() { 724 // For initial seeding, check if chunk is well seeded now, and if so 725 // remove it from the queue to better use our bandwidth on rare chunks. 726 while (!m_peerChunks.upload_queue()->empty() && 727 !m_download->initial_seeding()->should_upload(m_peerChunks.upload_queue()->front().index())) 728 m_peerChunks.upload_queue()->pop_front(); 729 730 // If queue ends up empty, choke peer to let it know that it 731 // shouldn't wait for the cancelled pieces to be sent. 732 if (m_peerChunks.upload_queue()->empty()) { 733 m_download->upload_choke_manager()->set_not_queued(this, &m_upChoke); 734 m_download->upload_choke_manager()->set_queued(this, &m_upChoke); 735 736 // If we're sending the chunk we last offered, adjust bytes left in it. 737 } else if (m_peerChunks.upload_queue()->front().index() == m_data.lastIndex) { 738 m_data.bytesLeft -= m_peerChunks.upload_queue()->front().length(); 739 740 if (!m_data.bytesLeft) 741 m_data.lastIndex = InitialSeeding::no_offer; 742 } 743 744 return !m_peerChunks.upload_queue()->empty(); 745 } 746 747 // Explicit instantiation of the member functions and vtable. 680 748 template class PeerConnection<Download::CONNECTION_LEECH>; 681 749 template class PeerConnection<Download::CONNECTION_SEED>; 682 683 } 750 template class PeerConnection<Download::CONNECTION_INITIAL_SEED>; 751 752 }
