mirror of
https://github.com/vcmi/vcmi.git
synced 2025-06-21 00:19:29 +02:00
Add few more safety checks to network connection logic
This commit is contained in:
@ -71,7 +71,8 @@ void NetworkConnection::onHeaderReceived(const boost::system::error_code & ecHea
|
|||||||
|
|
||||||
if (messageSize == 0)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,13 +104,16 @@ void NetworkConnection::onPacketReceived(const boost::system::error_code & ec, u
|
|||||||
|
|
||||||
void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
|
void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(writeMutex);
|
||||||
|
|
||||||
boost::system::error_code ec;
|
boost::system::error_code ec;
|
||||||
|
|
||||||
// create array with single element - boost::asio::buffer can be constructed from containers, but not from plain integer
|
// 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())};
|
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(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
|
//Note: ignoring error code, intended
|
||||||
}
|
}
|
||||||
|
@ -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
|
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::shared_ptr<NetworkSocket> socket;
|
||||||
|
std::mutex writeMutex;
|
||||||
|
|
||||||
NetworkBuffer readBuffer;
|
NetworkBuffer readBuffer;
|
||||||
INetworkConnectionListener & listener;
|
INetworkConnectionListener & listener;
|
||||||
|
@ -30,7 +30,7 @@ struct DLL_LINKAGE CPack
|
|||||||
template <typename Handler> void serialize(Handler &h)
|
template <typename Handler> void serialize(Handler &h)
|
||||||
{
|
{
|
||||||
logNetwork->error("CPack serialized... this should not happen!");
|
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)
|
void applyGs(CGameState * gs)
|
||||||
|
Reference in New Issue
Block a user