mirror of https://github.com/aria2/aria2
				
				
				
			Fix frequent choke/unchoke message transmission
							parent
							
								
									360ca57231
								
							
						
					
					
						commit
						570d46725f
					
				| 
						 | 
				
			
			@ -60,29 +60,4 @@ void BtChokeMessage::doReceivedAction()
 | 
			
		|||
  getBtRequestFactory()->doChokedAction();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BtChokeMessage::sendPredicate() const { return !getPeer()->amChoking(); }
 | 
			
		||||
 | 
			
		||||
namespace {
 | 
			
		||||
struct ThisProgressUpdate : public ProgressUpdate {
 | 
			
		||||
  ThisProgressUpdate(std::shared_ptr<Peer> peer, BtMessageDispatcher* disp)
 | 
			
		||||
      : peer(std::move(peer)), disp(disp)
 | 
			
		||||
  {
 | 
			
		||||
  }
 | 
			
		||||
  virtual void update(size_t length, bool complete) CXX11_OVERRIDE
 | 
			
		||||
  {
 | 
			
		||||
    if (complete) {
 | 
			
		||||
      peer->amChoking(true);
 | 
			
		||||
      disp->doChokingAction();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  std::shared_ptr<Peer> peer;
 | 
			
		||||
  BtMessageDispatcher* disp;
 | 
			
		||||
};
 | 
			
		||||
} // namespace
 | 
			
		||||
 | 
			
		||||
std::unique_ptr<ProgressUpdate> BtChokeMessage::getProgressUpdate()
 | 
			
		||||
{
 | 
			
		||||
  return make_unique<ThisProgressUpdate>(getPeer(), getBtMessageDispatcher());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace aria2
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -51,10 +51,6 @@ public:
 | 
			
		|||
 | 
			
		||||
  static std::unique_ptr<BtChokeMessage> create(const unsigned char* data,
 | 
			
		||||
                                                size_t dataLength);
 | 
			
		||||
 | 
			
		||||
  virtual bool sendPredicate() const CXX11_OVERRIDE;
 | 
			
		||||
 | 
			
		||||
  virtual std::unique_ptr<ProgressUpdate> getProgressUpdate() CXX11_OVERRIDE;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace aria2
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -56,24 +56,4 @@ void BtUnchokeMessage::doReceivedAction()
 | 
			
		|||
  getPeer()->peerChoking(false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BtUnchokeMessage::sendPredicate() const { return getPeer()->amChoking(); }
 | 
			
		||||
 | 
			
		||||
namespace {
 | 
			
		||||
struct ThisProgressUpdate : public ProgressUpdate {
 | 
			
		||||
  ThisProgressUpdate(std::shared_ptr<Peer> peer) : peer(std::move(peer)) {}
 | 
			
		||||
  virtual void update(size_t length, bool complete) CXX11_OVERRIDE
 | 
			
		||||
  {
 | 
			
		||||
    if (complete) {
 | 
			
		||||
      peer->amChoking(false);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  std::shared_ptr<Peer> peer;
 | 
			
		||||
};
 | 
			
		||||
} // namespace
 | 
			
		||||
 | 
			
		||||
std::unique_ptr<ProgressUpdate> BtUnchokeMessage::getProgressUpdate()
 | 
			
		||||
{
 | 
			
		||||
  return make_unique<ThisProgressUpdate>(getPeer());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace aria2
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -54,10 +54,6 @@ public:
 | 
			
		|||
                                                  size_t dataLength);
 | 
			
		||||
 | 
			
		||||
  virtual void doReceivedAction() CXX11_OVERRIDE;
 | 
			
		||||
 | 
			
		||||
  virtual bool sendPredicate() const CXX11_OVERRIDE;
 | 
			
		||||
 | 
			
		||||
  virtual std::unique_ptr<ProgressUpdate> getProgressUpdate() CXX11_OVERRIDE;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace aria2
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -251,11 +251,14 @@ void DefaultBtInteractive::decideChoking()
 | 
			
		|||
{
 | 
			
		||||
  if (peer_->shouldBeChoking()) {
 | 
			
		||||
    if (!peer_->amChoking()) {
 | 
			
		||||
      peer_->amChoking(true);
 | 
			
		||||
      dispatcher_->doChokingAction();
 | 
			
		||||
      dispatcher_->addMessageToQueue(messageFactory_->createChokeMessage());
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  else {
 | 
			
		||||
    if (peer_->amChoking()) {
 | 
			
		||||
      peer_->amChoking(false);
 | 
			
		||||
      dispatcher_->addMessageToQueue(messageFactory_->createUnchokeMessage());
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,7 +19,6 @@ class BtChokeMessageTest : public CppUnit::TestFixture {
 | 
			
		|||
  CPPUNIT_TEST(testCreate);
 | 
			
		||||
  CPPUNIT_TEST(testCreateMessage);
 | 
			
		||||
  CPPUNIT_TEST(testDoReceivedAction);
 | 
			
		||||
  CPPUNIT_TEST(testOnSendComplete);
 | 
			
		||||
  CPPUNIT_TEST(testToString);
 | 
			
		||||
  CPPUNIT_TEST_SUITE_END();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -36,7 +35,6 @@ public:
 | 
			
		|||
  void testCreate();
 | 
			
		||||
  void testCreateMessage();
 | 
			
		||||
  void testDoReceivedAction();
 | 
			
		||||
  void testOnSendComplete();
 | 
			
		||||
  void testToString();
 | 
			
		||||
 | 
			
		||||
  class MockBtMessageDispatcher2 : public MockBtMessageDispatcher {
 | 
			
		||||
| 
						 | 
				
			
			@ -130,21 +128,6 @@ void BtChokeMessageTest::testDoReceivedAction()
 | 
			
		|||
  CPPUNIT_ASSERT(peer->peerChoking());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BtChokeMessageTest::testOnSendComplete()
 | 
			
		||||
{
 | 
			
		||||
  BtChokeMessage msg;
 | 
			
		||||
  msg.setPeer(peer);
 | 
			
		||||
 | 
			
		||||
  auto dispatcher = make_unique<MockBtMessageDispatcher2>();
 | 
			
		||||
  msg.setBtMessageDispatcher(dispatcher.get());
 | 
			
		||||
 | 
			
		||||
  auto pu = std::unique_ptr<ProgressUpdate>{msg.getProgressUpdate()};
 | 
			
		||||
  pu->update(0, true);
 | 
			
		||||
 | 
			
		||||
  CPPUNIT_ASSERT(dispatcher->doChokingActionCalled);
 | 
			
		||||
  CPPUNIT_ASSERT(peer->amChoking());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BtChokeMessageTest::testToString()
 | 
			
		||||
{
 | 
			
		||||
  BtChokeMessage msg;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,7 +15,6 @@ class BtUnchokeMessageTest : public CppUnit::TestFixture {
 | 
			
		|||
  CPPUNIT_TEST(testCreate);
 | 
			
		||||
  CPPUNIT_TEST(testCreateMessage);
 | 
			
		||||
  CPPUNIT_TEST(testDoReceivedAction);
 | 
			
		||||
  CPPUNIT_TEST(testOnSendComplete);
 | 
			
		||||
  CPPUNIT_TEST(testToString);
 | 
			
		||||
  CPPUNIT_TEST_SUITE_END();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -26,7 +25,6 @@ public:
 | 
			
		|||
  void testCreate();
 | 
			
		||||
  void testCreateMessage();
 | 
			
		||||
  void testDoReceivedAction();
 | 
			
		||||
  void testOnSendComplete();
 | 
			
		||||
  void testToString();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -82,20 +80,6 @@ void BtUnchokeMessageTest::testDoReceivedAction()
 | 
			
		|||
  CPPUNIT_ASSERT(!peer->peerChoking());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BtUnchokeMessageTest::testOnSendComplete()
 | 
			
		||||
{
 | 
			
		||||
  std::shared_ptr<Peer> peer(new Peer("host", 6969));
 | 
			
		||||
  peer->allocateSessionResource(1_k, 1_m);
 | 
			
		||||
  peer->amChoking(true);
 | 
			
		||||
  BtUnchokeMessage msg;
 | 
			
		||||
  msg.setPeer(peer);
 | 
			
		||||
 | 
			
		||||
  CPPUNIT_ASSERT(peer->amChoking());
 | 
			
		||||
  std::shared_ptr<ProgressUpdate> pu(msg.getProgressUpdate());
 | 
			
		||||
  pu->update(0, true);
 | 
			
		||||
  CPPUNIT_ASSERT(!peer->amChoking());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BtUnchokeMessageTest::testToString()
 | 
			
		||||
{
 | 
			
		||||
  BtUnchokeMessage msg;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue