Browse Source

bugfix: reap() refcatored

cl-refactor
Vlad Gluhovsky 10 years ago
parent
commit
8493348667
  1. 23
      libp2p/Common.cpp
  2. 9
      libp2p/Common.h
  3. 3
      test/libp2p/peer.cpp

23
libp2p/Common.cpp

@ -176,6 +176,29 @@ void NodeIPEndpoint::interpretRLP(RLP const& _r)
tcpPort = _r[2].toInt<uint16_t>(); tcpPort = _r[2].toInt<uint16_t>();
} }
void DeadlineOps::reap()
{
if (m_stopped)
return;
Guard l(x_timers);
std::vector<DeadlineOp>::iterator t = m_timers.begin();
while (t != m_timers.end())
if (t->expired())
{
t->wait();
t = m_timers.erase(t);
}
else
t++;
m_timers.emplace_back(m_io, m_reapIntervalMs, [this](boost::system::error_code const& ec)
{
if (!ec && !m_stopped)
reap();
});
}
namespace dev { namespace dev {
std::ostream& operator<<(std::ostream& _out, dev::p2p::NodeIPEndpoint const& _ep) std::ostream& operator<<(std::ostream& _out, dev::p2p::NodeIPEndpoint const& _ep)

9
libp2p/Common.h

@ -237,14 +237,13 @@ class DeadlineOps
public: public:
DeadlineOps(ba::io_service& _io, unsigned _reapIntervalMs = 100): m_io(_io), m_reapIntervalMs(_reapIntervalMs), m_stopped({false}) { reap(); } DeadlineOps(ba::io_service& _io, unsigned _reapIntervalMs = 100): m_io(_io), m_reapIntervalMs(_reapIntervalMs), m_stopped({false}) { reap(); }
~DeadlineOps() { stop(); } ~DeadlineOps() { stop(); }
void schedule(unsigned _msInFuture, std::function<void(boost::system::error_code const&)> const& _f) { if (m_stopped) return; DEV_GUARDED(x_timers) m_timers.emplace_back(m_io, _msInFuture, _f); } void schedule(unsigned _msInFuture, std::function<void(boost::system::error_code const&)> const& _f) { if (m_stopped) return; DEV_GUARDED(x_timers) m_timers.emplace_back(m_io, _msInFuture, _f); }
void stop() { m_stopped = true; DEV_GUARDED(x_timers) m_timers.clear(); } void stop() { m_stopped = true; DEV_GUARDED(x_timers) m_timers.clear(); }
protected: protected:
void reap() { Guard l(x_timers); auto t = m_timers.begin(); while (t != m_timers.end()) if (t->expired()) { t->wait(); m_timers.erase(t); } else t++; m_timers.emplace_back(m_io, m_reapIntervalMs, [this](boost::system::error_code const& ec){ if (!ec) reap(); }); } void reap();
private: private:
ba::io_service& m_io; ba::io_service& m_io;
unsigned m_reapIntervalMs; unsigned m_reapIntervalMs;

3
test/libp2p/peer.cpp

@ -40,6 +40,7 @@ BOOST_FIXTURE_TEST_SUITE(p2p, P2PFixture)
BOOST_AUTO_TEST_CASE(host) BOOST_AUTO_TEST_CASE(host)
{ {
cnote << "p2p/host...";
if (test::Options::get().nonetwork) if (test::Options::get().nonetwork)
return; return;
@ -73,6 +74,7 @@ BOOST_AUTO_TEST_CASE(host)
BOOST_AUTO_TEST_CASE(networkConfig) BOOST_AUTO_TEST_CASE(networkConfig)
{ {
cnote << "p2p/networkConfig...";
if (test::Options::get().nonetwork) if (test::Options::get().nonetwork)
return; return;
@ -85,6 +87,7 @@ BOOST_AUTO_TEST_CASE(networkConfig)
BOOST_AUTO_TEST_CASE(saveNodes) BOOST_AUTO_TEST_CASE(saveNodes)
{ {
cnote << "p2p/saveNodes...";
if (test::Options::get().nonetwork) if (test::Options::get().nonetwork)
return; return;

Loading…
Cancel
Save