Skip to content

Commit 6188a91

Browse files
committed
Fixup choke handling
Peer::set_am_choking was not proper implemented, calling it would have no effect. Rectified by implementing it.
1 parent 956fe40 commit 6188a91

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/peer.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ void PeerConnection::stop() {
227227
void Peer::request(uint32_t index, uint32_t begin, uint32_t length) {
228228
m_logger->trace("Peer::request(index={}, begin={}, length={})", index, begin,
229229
length);
230+
if (m_am_choking) {
231+
m_logger->debug("{}: Choking peer, not sending blocks", str());
232+
return;
233+
}
234+
if (!m_interested) {
235+
m_logger->debug("{}: Peer not interested, not sending blocks", str());
236+
return;
237+
}
230238
auto piece = m_torrent.active_piece(index);
231239
if (!piece) {
232240
m_logger->warn("Requested non existing piece {}", index);
@@ -251,6 +259,24 @@ void Peer::request(uint32_t index, uint32_t begin, uint32_t length) {
251259
}
252260

253261
void Peer::set_am_choking(bool am_choking) {
262+
if (!m_am_choking && am_choking) {
263+
// Send UNCHOKE
264+
m_logger->debug("Sending CHOKE");
265+
string choke = {0, 0, 0, 1, static_cast<pwid_t>(peer_wire_id::CHOKE)};
266+
stringstream hs;
267+
hs.write(choke.c_str(), numeric_cast<std::streamsize>(choke.length()));
268+
m_connection->write(hs.str());
269+
}
270+
271+
if (m_am_choking && !am_choking) {
272+
// Send UNCHOKE
273+
m_logger->debug("Sending UNCHOKE");
274+
string unchoke = {0, 0, 0, 1, static_cast<pwid_t>(peer_wire_id::UNCHOKE)};
275+
stringstream hs;
276+
hs.write(unchoke.c_str(), numeric_cast<std::streamsize>(unchoke.length()));
277+
m_connection->write(hs.str());
278+
}
279+
254280
m_am_choking = am_choking;
255281
}
256282

@@ -287,7 +313,7 @@ void Peer::set_am_interested(bool am_interested) {
287313
std::size_t Peer::request_next_block(unsigned short count) {
288314
size_t requests = 0;
289315
if (!m_am_interested) {
290-
m_logger->trace(
316+
m_logger->debug(
291317
"{}: Peer not interested (no handshake), not requesting blocks", str());
292318
return requests;
293319
}
@@ -360,10 +386,7 @@ void Peer::set_choking(bool choking) {
360386
void Peer::set_interested(bool interested) {
361387
if (!m_interested && interested) {
362388
m_logger->info("Peer is Interested - sending unchoke");
363-
string unchoke = {0, 0, 0, 1, static_cast<pwid_t>(peer_wire_id::UNCHOKE)};
364-
stringstream hs;
365-
hs.write(unchoke.c_str(), numeric_cast<std::streamsize>(unchoke.length()));
366-
m_connection->write(hs.str());
389+
set_am_choking(false);
367390
}
368391
if (m_interested && !interested) {
369392
m_logger->info("Peer is Not interested");

tests/test_integrate.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,11 @@ TEST_F(Integrate, DISABLED_upload) {
440440
zit::Torrent torrent(torrent_file, data_dir, test_config);
441441
ASSERT_TRUE(torrent.done());
442442

443+
sigint_function = [&](int /*s*/) {
444+
spdlog::get("console")->warn("CTRL-C pressed. Stopping torrent...");
445+
torrent.stop();
446+
};
447+
443448
// Start a leecher that we will upload to
444449
auto target = tmp_dir() / "upload_test";
445450
auto leecher = start_leecher(target, torrent_file);

0 commit comments

Comments
 (0)