From 862e3db19ee190752c92275370bdc540fc41d137 Mon Sep 17 00:00:00 2001 From: radj307 Date: Sat, 2 Mar 2024 15:24:02 -0500 Subject: [PATCH] add a warning log message for data desync --- ARRCON/ARRCON.cpp | 10 ++++++++++ ARRCON/net/rcon.hpp | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ARRCON/ARRCON.cpp b/ARRCON/ARRCON.cpp index 4927145..ae11e67 100644 --- a/ARRCON/ARRCON.cpp +++ b/ARRCON/ARRCON.cpp @@ -264,6 +264,8 @@ int main_impl(const int argc, char** argv) // validate & log the target host information std::clog << MessageHeader(LogLevel::Info) << "Target Host: \"" << target.host << ':' << target.port << '\"' << std::endl; + // TODO: add a check for blank passwords + // initialize and connect the client net::rcon::RconClient client{ target.host, target.port }; @@ -343,6 +345,14 @@ int main_impl(const int argc, char** argv) std::string str; std::getline(std::cin, str); + // check for buffered data + if (const auto& buffer_size{ client.buffer_size() }; buffer_size > 0) { + std::clog << MessageHeader(LogLevel::Warning) << "The buffer contains " << buffer_size << " unexpected bytes! Dumping the buffer to STDOUT." << std::endl; + + // print the buffered data before continuing + std::cout << str::trim(net::rcon::bytes_to_string(client.flush())) << std::endl; + } + // validate the input if (!allowEmptyCommands && str::trim(str).empty()) { std::cerr << csync(color::cyan) << "[not sent: empty]" << csync() << '\n'; diff --git a/ARRCON/net/rcon.hpp b/ARRCON/net/rcon.hpp index 42a54a1..2dd4aa7 100644 --- a/ARRCON/net/rcon.hpp +++ b/ARRCON/net/rcon.hpp @@ -304,14 +304,15 @@ namespace net { else return true; } - /// @brief Empties the buffer and discards its contents. - void flush() + /// @brief Empties the buffer and returns its contents. + buffer flush() { const auto bytes{ socket.available() }; if (bytes == 0) return; buffer p{ bytes, 0, std::allocator() }; boost::asio::read(socket, boost::asio::buffer(p)); + return p; } /** @@ -322,6 +323,15 @@ namespace net { { socket.set_option(boost::asio::detail::socket_option::integer{ timeout_ms }); } + + /** + * @brief Gets the current size of the socket's data buffer. + * @returns The number of bytes that haven't been read from the buffer yet. + */ + size_t buffer_size() + { + return socket.available(); + } }; } }