@@ -227,6 +227,14 @@ void PeerConnection::stop() {
227227void 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
253261void 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) {
287313std::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) {
360386void 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" );
0 commit comments