remove the "write_timer"

This commit is contained in:
Eric Mertens 2025-01-24 16:57:34 -08:00
parent 3b48ff7c7e
commit 1a1deb03b7
2 changed files with 19 additions and 37 deletions

View File

@ -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<boost::asio::const_buffer> 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();
}
});
}
}

View File

@ -15,16 +15,15 @@ class Connection : public std::enable_shared_from_this<Connection>
{
private:
boost::asio::ip::tcp::socket stream_;
boost::asio::steady_timer write_timer_;
boost::asio::steady_timer watchdog_timer_;
std::list<std::string> 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};