1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

Guard against concurrent writes on same asio::socket instance

This commit is contained in:
Ivan Savenko 2024-02-19 13:47:06 +02:00
parent f620a07389
commit 779625415f
2 changed files with 2 additions and 0 deletions

View File

@ -88,6 +88,7 @@ void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
// 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::mutex::scoped_lock lock(writeMutex);
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 ); boost::asio::write(*socket, boost::asio::buffer(message), ec );

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 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;
boost::mutex writeMutex;
NetworkBuffer readBuffer; NetworkBuffer readBuffer;
INetworkConnectionListener & listener; INetworkConnectionListener & listener;