From 83287f53d40ea5da2e6de90953327f6e4ae04b4c Mon Sep 17 00:00:00 2001 From: Mohammad Nejati Date: Thu, 15 Feb 2024 06:54:43 +0000 Subject: [PATCH] Update websocket idle timer upon receiving a ping message --- include/boost/beast/websocket/impl/read.hpp | 2 ++ test/beast/websocket/ping.cpp | 33 +++++---------------- 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/include/boost/beast/websocket/impl/read.hpp b/include/boost/beast/websocket/impl/read.hpp index 131ea6e2dc..4dd5e7a75a 100644 --- a/include/boost/beast/websocket/impl/read.hpp +++ b/include/boost/beast/websocket/impl/read.hpp @@ -227,6 +227,8 @@ class stream::read_some_op // Handle ping frame if(impl.rd_fh.op == detail::opcode::ping) { + impl.update_timer(this->get_executor()); + if(impl.ctrl_cb) { if(! cont) diff --git a/test/beast/websocket/ping.cpp b/test/beast/websocket/ping.cpp index 113fdb9900..95e420865e 100644 --- a/test/beast/websocket/ping.cpp +++ b/test/beast/websocket/ping.cpp @@ -93,11 +93,9 @@ class ping_test : public websocket_test_suite { echo_server es{log}; stream ws{ioc_}; - - // We have an inactivity timeout of 2s, but don't send pings ws.set_option(stream_base::timeout{ stream_base::none(), - std::chrono::milliseconds(2000), + std::chrono::seconds(2), false }); ws.next_layer().connect(es.stream()); @@ -112,28 +110,11 @@ class ping_test : public websocket_test_suite system_error{ec}); got_timeout = true; }); - // We are connected, base state - BEAST_EXPECT(ws.impl_->idle_counter == 0); - - test::run_for(ioc_, std::chrono::milliseconds(1250)); - // After 1.25s idle, no timeout but idle counter is 1 - BEAST_EXPECT(ws.impl_->idle_counter == 1); - + test::run_for(ioc_, std::chrono::seconds(1)); es.async_ping(); - test::run_for(ioc_, std::chrono::milliseconds(500)); - // The server sent a ping at 1.25s mark, and we're now at 1.75s mark. - // We haven't hit the idle timer yet (happens at 1s, 2s, 3s) - BEAST_EXPECT(ws.impl_->idle_counter == 0); - BEAST_EXPECT(!got_timeout); - - test::run_for(ioc_, std::chrono::milliseconds(750)); - // At 2.5s total; should have triggered the idle timer - BEAST_EXPECT(ws.impl_->idle_counter == 1); + test::run_for(ioc_, std::chrono::seconds(1)); BEAST_EXPECT(!got_timeout); - - test::run_for(ioc_, std::chrono::milliseconds(750)); - // At 3s total; should have triggered the idle timer again without - // activity and triggered timeout. + test::run_for(ioc_, std::chrono::seconds(2)); BEAST_EXPECT(got_timeout); } @@ -143,7 +124,7 @@ class ping_test : public websocket_test_suite stream ws{ioc_}; ws.set_option(stream_base::timeout{ stream_base::none(), - std::chrono::milliseconds(600), + std::chrono::seconds(1), true }); unsigned n_pongs = 0; @@ -157,9 +138,9 @@ class ping_test : public websocket_test_suite flat_buffer b; ws.async_read(b, test::fail_handler(asio::error::operation_aborted)); // We are connected, base state - test::run_for(ioc_, std::chrono::seconds(1)); + test::run_for(ioc_, std::chrono::seconds(2)); // About a second later, we should have close to 5 pings/pongs, and no timeout - BEAST_EXPECTS(2 <= n_pongs && n_pongs <= 3, "Unexpected nr of pings: " + std::to_string(n_pongs)); + BEAST_EXPECTS(2 <= n_pongs && n_pongs <= 4, "Unexpected nr of pings: " + std::to_string(n_pongs)); } }