From be25e61cb7a5dc92d2ea29e6cd93ac0f38bf9fda Mon Sep 17 00:00:00 2001 From: Mokhtar Naamani Date: Fri, 5 Jan 2018 14:07:14 +0200 Subject: [PATCH] handle case where normal peer sends a piece before joystream peer Fixes issue #35 --- sources/src/TorrentPlugin.cpp | 37 ++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/sources/src/TorrentPlugin.cpp b/sources/src/TorrentPlugin.cpp index 5152892..52afcd8 100644 --- a/sources/src/TorrentPlugin.cpp +++ b/sources/src/TorrentPlugin.cpp @@ -735,22 +735,27 @@ protocol_session::RemovedConnectionCallbackHandler TorrentP protocol_session::FullPieceArrived TorrentPlugin::fullPieceArrived() { return [this](const libtorrent::peer_id & peerId, const protocol_wire::PieceData & pieceData, int index) -> void { - - // Make sure no outstanding calls exist for this index - assert(!_outstandingFullPieceArrivedCalls.count(index)); - - _outstandingFullPieceArrivedCalls[index] = peerId; - - // Tell libtorrent to validate piece - // last argument is a flag which presently seems to only test - // flags & torrent::overwrite_existing, which seems to be whether - // the piece should be overwritten if it is already present - // - // libtorrent::torrent_plugin::on_piece_pass() - // libtorrent::torrent_plugin::on_piece_failed() - // processes result of checking - - torrent()->add_piece(index, pieceData.piece().get(), 0); + if (!torrent()->have_piece(index)) { + // Make sure no outstanding calls exist for this index + assert(!_outstandingFullPieceArrivedCalls.count(index)); + + _outstandingFullPieceArrivedCalls[index] = peerId; + + // Tell libtorrent to validate piece + // last argument is a flag which presently seems to only test + // flags & torrent::overwrite_existing, which seems to be whether + // the piece should be overwritten if it is already present + // + // libtorrent::torrent_plugin::on_piece_pass() + // libtorrent::torrent_plugin::on_piece_failed() + // processes result of checking + torrent()->add_piece(index, pieceData.piece().get(), 0); + } else { + // We already received the piece from another peer (most likely a non joystream peer) + // For now we ignore the validity of the piece data + // tell session about endpoint and piece + _session.validPieceReceivedOnConnection(peerId, index); + } }; }