mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-15 20:03:15 +02:00
Merge pull request #3640 from IvanSavenko/fix_thread_race
Fix thread race
This commit is contained in:
@@ -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
|
// 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 );
|
||||||
|
|
||||||
|
@@ -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
|
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;
|
||||||
|
@@ -70,6 +70,8 @@ CConnection::~CConnection() = default;
|
|||||||
|
|
||||||
void CConnection::sendPack(const CPack * pack)
|
void CConnection::sendPack(const CPack * pack)
|
||||||
{
|
{
|
||||||
|
boost::mutex::scoped_lock lock(writeMutex);
|
||||||
|
|
||||||
auto connectionPtr = networkConnection.lock();
|
auto connectionPtr = networkConnection.lock();
|
||||||
|
|
||||||
if (!connectionPtr)
|
if (!connectionPtr)
|
||||||
@@ -92,7 +94,13 @@ CPack * CConnection::retrievePack(const std::vector<std::byte> & data)
|
|||||||
|
|
||||||
*deserializer & result;
|
*deserializer & result;
|
||||||
|
|
||||||
logNetwork->trace("Received CPack of type %s", (result ? typeid(*result).name() : "nullptr"));
|
if (result == nullptr)
|
||||||
|
throw std::runtime_error("Failed to retrieve pack!");
|
||||||
|
|
||||||
|
if (packReader->position != data.size())
|
||||||
|
throw std::runtime_error("Failed to retrieve pack! Not all data has been read!");
|
||||||
|
|
||||||
|
logNetwork->trace("Received CPack of type %s", typeid(*result).name());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -32,6 +32,8 @@ class DLL_LINKAGE CConnection : boost::noncopyable
|
|||||||
std::unique_ptr<BinaryDeserializer> deserializer;
|
std::unique_ptr<BinaryDeserializer> deserializer;
|
||||||
std::unique_ptr<BinarySerializer> serializer;
|
std::unique_ptr<BinarySerializer> serializer;
|
||||||
|
|
||||||
|
boost::mutex writeMutex;
|
||||||
|
|
||||||
void disableStackSendingByID();
|
void disableStackSendingByID();
|
||||||
void enableStackSendingByID();
|
void enableStackSendingByID();
|
||||||
void disableSmartPointerSerialization();
|
void disableSmartPointerSerialization();
|
||||||
|
Reference in New Issue
Block a user