Explorar el Código

Guard against concurrent writes on same asio::socket instance

Ivan Savenko hace 1 año
padre
commit
779625415f
Se han modificado 2 ficheros con 2 adiciones y 0 borrados
  1. 1 0
      lib/network/NetworkConnection.cpp
  2. 1 0
      lib/network/NetworkConnection.h

+ 1 - 0
lib/network/NetworkConnection.cpp

@@ -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 );
 
 

+ 1 - 0
lib/network/NetworkConnection.h

@@ -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;