Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit bf7b9ae

Browse files
committed
Call expires_after and async_wait inside Host::createTimer
1 parent 467c8c6 commit bf7b9ae

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

libp2p/Host.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,3 +1200,11 @@ void Host::forEachPeer(
12001200
return;
12011201
}
12021202

1203+
std::unique_ptr<ba::steady_timer> Host::createTimer(std::chrono::seconds const& _expiryTime,
1204+
std::function<void(const boost::system::error_code& error)>&& _f)
1205+
{
1206+
std::unique_ptr<ba::steady_timer> timer{new ba::steady_timer{m_ioContext}};
1207+
timer->expires_after(_expiryTime);
1208+
timer->async_wait(_f);
1209+
return timer;
1210+
}

libp2p/Host.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ class Host: public Worker
266266

267267
std::shared_ptr<CapabilityHostFace> capabilityHost() const { return m_capabilityHost; }
268268

269-
std::shared_ptr<ba::steady_timer> createTimer()
270-
{
271-
return std::make_shared<ba::steady_timer>(m_ioContext);
272-
}
269+
/// Execute work on the network thread after @a _expiryTime delay.
270+
/// Returned timer should be kept alive until delay is over.
271+
std::unique_ptr<ba::steady_timer> createTimer(std::chrono::seconds const& _expiryTime,
272+
std::function<void(const boost::system::error_code& error)>&& _f);
273273

274274
protected:
275275
/*

libp2p/Session.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -299,23 +299,24 @@ void Session::disconnect(DisconnectReason _reason)
299299
RLPStream s;
300300
prep(s, DisconnectPacket, 1) << (int)_reason;
301301
sealAndSend(s);
302-
auto disconnectTimer = m_server->createTimer();
302+
303303
auto self(shared_from_this());
304-
disconnectTimer->expires_after(std::chrono::seconds(2));
305-
disconnectTimer->async_wait([self, this, _reason](boost::system::error_code) {
306-
bi::tcp::socket& socket = m_socket->ref();
307-
if (socket.is_open())
308-
try
309-
{
310-
boost::system::error_code ec;
311-
LOG(m_netLoggerDetail) << "Closing (" << reasonOf(_reason) << ") connection with";
312-
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
313-
socket.close();
314-
}
315-
catch (...)
316-
{
317-
}
318-
});
304+
m_disconnectTimer = m_server->createTimer(
305+
std::chrono::seconds(2), [self, this, _reason](boost::system::error_code) {
306+
bi::tcp::socket& socket = m_socket->ref();
307+
if (socket.is_open())
308+
try
309+
{
310+
boost::system::error_code ec;
311+
LOG(m_netLoggerDetail)
312+
<< "Closing (" << reasonOf(_reason) << ") connection with";
313+
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
314+
socket.close();
315+
}
316+
catch (...)
317+
{
318+
}
319+
});
319320

320321
drop(_reason);
321322
}

libp2p/Session.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ class Session: public SessionFace, public std::enable_shared_from_this<SessionFa
177177

178178
std::set<std::string> m_disabledCapabilities;
179179

180+
std::unique_ptr<ba::steady_timer> m_disconnectTimer;
181+
180182
std::string m_logSuffix;
181183

182184
Logger m_netLogger{createLogger(VerbosityDebug, "net")};

0 commit comments

Comments
 (0)