1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-17 11:56:46 +02:00

Add few more safety checks to network connection logic

This commit is contained in:
Ivan Savenko 2024-05-07 08:34:47 +00:00
parent ede92c1a9c
commit 66e2e13ba1
3 changed files with 8 additions and 3 deletions

View File

@ -71,7 +71,8 @@ void NetworkConnection::onHeaderReceived(const boost::system::error_code & ecHea
if (messageSize == 0)
{
listener.onDisconnected(shared_from_this(), "Zero-sized packet!");
// Zero-sized packet. Strange, but safe to ignore. Start reading next packet
start();
return;
}
@ -103,13 +104,16 @@ void NetworkConnection::onPacketReceived(const boost::system::error_code & ec, u
void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
{
std::lock_guard<std::mutex> lock(writeMutex);
boost::system::error_code ec;
// create array with single element - boost::asio::buffer can be constructed from containers, but not from plain integer
std::array<uint32_t, 1> messageSize{static_cast<uint32_t>(message.size())};
boost::asio::write(*socket, boost::asio::buffer(messageSize), ec );
boost::asio::write(*socket, boost::asio::buffer(message), ec );
if (message.size() > 0)
boost::asio::write(*socket, boost::asio::buffer(message), ec );
//Note: ignoring error code, intended
}

View File

@ -19,6 +19,7 @@ class NetworkConnection : public INetworkConnection, public std::enable_shared_f
static const int messageMaxSize = 64 * 1024 * 1024; // arbitrary size to prevent potential massive allocation if we receive garbage input
std::shared_ptr<NetworkSocket> socket;
std::mutex writeMutex;
NetworkBuffer readBuffer;
INetworkConnectionListener & listener;

View File

@ -30,7 +30,7 @@ struct DLL_LINKAGE CPack
template <typename Handler> void serialize(Handler &h)
{
logNetwork->error("CPack serialized... this should not happen!");
assert(false && "CPack serialized");
throw std::runtime_error("CPack serialized... this should not happen!");
}
void applyGs(CGameState * gs)