Skip to content

Commit

Permalink
add a warning log message for data desync
Browse files Browse the repository at this point in the history
  • Loading branch information
radj307 committed Mar 2, 2024
1 parent ecee0fd commit 862e3db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 10 additions & 0 deletions ARRCON/ARRCON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -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';
Expand Down
14 changes: 12 additions & 2 deletions ARRCON/net/rcon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>() };
boost::asio::read(socket, boost::asio::buffer(p));
return p;
}

/**
Expand All @@ -322,6 +323,15 @@ namespace net {
{
socket.set_option(boost::asio::detail::socket_option::integer<SOL_SOCKET, SO_RCVTIMEO>{ 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();
}
};
}
}

0 comments on commit 862e3db

Please sign in to comment.