From 47331081bca55e82fa5413ac9360dd20ff91055a Mon Sep 17 00:00:00 2001 From: Ruslan Tushov Date: Mon, 13 Feb 2023 09:05:30 +0300 Subject: [PATCH] Fix ws (#207) Signed-off-by: turuslan --- include/libp2p/layer/websocket/ws_connection.hpp | 11 +++++++++-- .../libp2p/layer/websocket/ws_connection_config.hpp | 1 + src/layer/websocket/ws_connection.cpp | 7 ++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/libp2p/layer/websocket/ws_connection.hpp b/include/libp2p/layer/websocket/ws_connection.hpp index 2553121e..f8667b89 100644 --- a/include/libp2p/layer/websocket/ws_connection.hpp +++ b/include/libp2p/layer/websocket/ws_connection.hpp @@ -20,11 +20,18 @@ namespace libp2p::layer { } // namespace libp2p::layer namespace libp2p { - inline void beast_close_socket(AsAsioReadWrite &stream) {} + inline void beast_close_socket(AsAsioReadWrite &stream) { + std::ignore = stream.impl->close(); + } template void async_teardown(boost::beast::role_type role, AsAsioReadWrite &stream, - Cb &&cb) {} + Cb &&cb) { + std::ignore = stream.impl->close(); + boost::asio::post(*stream.io, [cb{std::move(cb)}]() mutable { + cb(boost::system::error_code{}); + }); + } } // namespace libp2p namespace libp2p::connection { diff --git a/include/libp2p/layer/websocket/ws_connection_config.hpp b/include/libp2p/layer/websocket/ws_connection_config.hpp index bd557a51..85f49012 100644 --- a/include/libp2p/layer/websocket/ws_connection_config.hpp +++ b/include/libp2p/layer/websocket/ws_connection_config.hpp @@ -14,6 +14,7 @@ namespace libp2p::layer { */ struct WsConnectionConfig { std::chrono::milliseconds ping_interval{60'000}; + std::chrono::milliseconds ping_timeout{10'000}; }; } // namespace libp2p::layer diff --git a/src/layer/websocket/ws_connection.cpp b/src/layer/websocket/ws_connection.cpp index e702f064..3bd66832 100644 --- a/src/layer/websocket/ws_connection.cpp +++ b/src/layer/websocket/ws_connection.cpp @@ -63,8 +63,8 @@ namespace libp2p::connection { [](boost::system::error_code) {}); // Start timer of pong waiting - self->ping_timeout_handle_ = - self->scheduler_->scheduleWithHandle([wp] { + self->ping_timeout_handle_ = self->scheduler_->scheduleWithHandle( + [wp] { if (auto self = wp.lock()) { self->ws_.async_close( { @@ -73,7 +73,8 @@ namespace libp2p::connection { }, [](boost::system::error_code) {}); } - }); + }, + self->config_.ping_timeout); } }, config_.ping_interval);