mirror of
https://github.com/vcmi/vcmi.git
synced 2025-03-25 21:38:59 +02:00
Fix possible thread race on sending packet from two threads
This commit is contained in:
parent
8647abb94b
commit
6901945b6e
@ -88,7 +88,6 @@ 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
|
||||
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(message), ec );
|
||||
|
||||
|
@ -19,7 +19,6 @@ 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;
|
||||
boost::mutex writeMutex;
|
||||
|
||||
NetworkBuffer readBuffer;
|
||||
INetworkConnectionListener & listener;
|
||||
|
@ -70,6 +70,8 @@ CConnection::~CConnection() = default;
|
||||
|
||||
void CConnection::sendPack(const CPack * pack)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(writeMutex);
|
||||
|
||||
auto connectionPtr = networkConnection.lock();
|
||||
|
||||
if (!connectionPtr)
|
||||
|
@ -32,6 +32,8 @@ class DLL_LINKAGE CConnection : boost::noncopyable
|
||||
std::unique_ptr<BinaryDeserializer> deserializer;
|
||||
std::unique_ptr<BinarySerializer> serializer;
|
||||
|
||||
boost::mutex writeMutex;
|
||||
|
||||
void disableStackSendingByID();
|
||||
void enableStackSendingByID();
|
||||
void disableSmartPointerSerialization();
|
||||
|
Loading…
x
Reference in New Issue
Block a user