diff --git a/lib/network/NetworkConnection.cpp b/lib/network/NetworkConnection.cpp index e2239d8bd..7b31ca04b 100644 --- a/lib/network/NetworkConnection.cpp +++ b/lib/network/NetworkConnection.cpp @@ -88,7 +88,6 @@ void NetworkConnection::sendPacket(const std::vector & message) // create array with single element - boost::asio::buffer can be constructed from containers, but not from plain integer std::array messageSize{static_cast(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 ); diff --git a/lib/network/NetworkConnection.h b/lib/network/NetworkConnection.h index a6d3b721d..beaaba376 100644 --- a/lib/network/NetworkConnection.h +++ b/lib/network/NetworkConnection.h @@ -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 socket; - boost::mutex writeMutex; NetworkBuffer readBuffer; INetworkConnectionListener & listener; diff --git a/lib/serializer/Connection.cpp b/lib/serializer/Connection.cpp index c72921d2f..f76b77262 100644 --- a/lib/serializer/Connection.cpp +++ b/lib/serializer/Connection.cpp @@ -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) @@ -92,7 +94,13 @@ CPack * CConnection::retrievePack(const std::vector & data) *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; } diff --git a/lib/serializer/Connection.h b/lib/serializer/Connection.h index 3d14f261b..68d23c34e 100644 --- a/lib/serializer/Connection.h +++ b/lib/serializer/Connection.h @@ -32,6 +32,8 @@ class DLL_LINKAGE CConnection : boost::noncopyable std::unique_ptr deserializer; std::unique_ptr serializer; + boost::mutex writeMutex; + void disableStackSendingByID(); void enableStackSendingByID(); void disableSmartPointerSerialization();