From 1a1deb03b7f02e277c4071893960169fa8518055 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Fri, 24 Jan 2025 16:57:34 -0800 Subject: [PATCH] remove the "write_timer" --- connection.cpp | 51 +++++++++++++++++--------------------------------- connection.hpp | 5 ++--- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/connection.cpp b/connection.cpp index fa30e1e..b29cb70 100644 --- a/connection.cpp +++ b/connection.cpp @@ -12,13 +12,13 @@ using namespace std::literals; Connection::Connection(boost::asio::io_context & io) : stream_{io} -, write_timer_{io, std::chrono::steady_clock::time_point::max()} , watchdog_timer_{io} +, write_posted_{false} , stalled_{false} { } -auto Connection::writer_immediate() -> void +auto Connection::write_buffers() -> void { std::vector buffers; buffers.reserve(write_strings_.size()); @@ -31,36 +31,15 @@ auto Connection::writer_immediate() -> void buffers, [this, strings = std::move(write_strings_)](boost::system::error_code const& error, std::size_t) { - if (not error) - { - writer(); - } - }); - write_strings_.clear(); -} - -auto Connection::writer() -> void -{ - if (write_strings_.empty()) - { - write_timer_.async_wait([weak = weak_from_this()](auto){ - // This wait will always trigger on a cancellation. That - // cancellation might be from write_line or it might be from - // the connection being destroyed. The weak pointer will fail - // to lock in the case that the object is being destructed. - if (auto self = weak.lock()) - { - if (not self->write_strings_.empty()) - { - self->writer_immediate(); + if (not error) { + if (write_strings_.empty()) { + write_posted_ = false; + } else { + write_buffers(); } } }); - } - else - { - writer_immediate(); - } + write_strings_.clear(); } auto Connection::connect( @@ -83,7 +62,6 @@ auto Connection::connect( } // Start the queue writer after connection - writer(); watchdog(); for(LineBuffer buffer{32'768};;) @@ -178,11 +156,16 @@ auto Connection::write_line(std::string message) -> void { BOOST_LOG_TRIVIAL(debug) << "SEND: " << message; message += "\r\n"; - auto const need_cancel = write_strings_.empty(); write_strings_.push_back(std::move(message)); - if (need_cancel) - { - write_timer_.cancel_one(); + + if (not write_posted_) { + write_posted_ = true; + boost::asio::post(stream_.get_executor(), [weak = weak_from_this()](){ + if (auto self = weak.lock()) + { + self->write_buffers(); + } + }); } } diff --git a/connection.hpp b/connection.hpp index 162fce4..e5dded5 100644 --- a/connection.hpp +++ b/connection.hpp @@ -15,16 +15,15 @@ class Connection : public std::enable_shared_from_this { private: boost::asio::ip::tcp::socket stream_; - boost::asio::steady_timer write_timer_; boost::asio::steady_timer watchdog_timer_; std::list write_strings_; + bool write_posted_; // Set true when watchdog triggers. // Set false when message received. bool stalled_; - auto writer() -> void; - auto writer_immediate() -> void; + auto write_buffers() -> void; auto dispatch_line(char * line) -> void; static constexpr std::chrono::seconds watchdog_duration = std::chrono::seconds{30};