From e57a63939eca1988df87ff6dbd72150ee1fdb950 Mon Sep 17 00:00:00 2001 From: Shauren Date: Wed, 17 Sep 2014 23:06:34 +0200 Subject: Core/NetworkIO: Fixed queued packets not being properly sent causing players to be stuck during loading Closes #13120 --- src/server/shared/Networking/Socket.h | 48 +++++++++++++++++------------------ 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'src/server/shared/Networking') diff --git a/src/server/shared/Networking/Socket.h b/src/server/shared/Networking/Socket.h index dfff60a380b..d3e29ceaaea 100644 --- a/src/server/shared/Networking/Socket.h +++ b/src/server/shared/Networking/Socket.h @@ -34,6 +34,7 @@ using boost::asio::ip::tcp; #define READ_BLOCK_SIZE 4096 +#define TC_SOCKET_USE_IOCP BOOST_ASIO_HAS_IOCP template class Socket : public std::enable_shared_from_this @@ -58,11 +59,15 @@ public: if (!IsOpen()) return false; -#ifndef BOOST_ASIO_HAS_IOCP +#ifndef TC_SOCKET_USE_IOCP + std::unique_lock guard(_writeLock, std::try_to_lock); + if (!guard) + return true; + if (_isWritingAsync || (!_writeBuffer.GetActiveSize() && _writeQueue.empty())) return true; - for (; WriteHandler(boost::system::error_code(), 0);) + for (; WriteHandler(guard);) ; #endif @@ -113,7 +118,7 @@ public: { _writeQueue.push(std::move(buffer)); -#ifdef BOOST_ASIO_HAS_IOCP +#ifdef TC_SOCKET_USE_IOCP AsyncProcessQueue(guard); #else (void)guard; @@ -145,24 +150,25 @@ protected: bool AsyncProcessQueue(std::unique_lock&) { if (_isWritingAsync) - return true; + return false; _isWritingAsync = true; -#ifdef BOOST_ASIO_HAS_IOCP +#ifdef TC_SOCKET_USE_IOCP MessageBuffer& buffer = _writeQueue.front(); _socket.async_write_some(boost::asio::buffer(buffer.GetReadPointer(), buffer.GetActiveSize()), std::bind(&Socket::WriteHandler, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2)); #else - _socket.async_write_some(boost::asio::null_buffers(), std::bind(&Socket::WriteHandler, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2)); + _socket.async_write_some(boost::asio::null_buffers(), std::bind(&Socket::WriteHandlerWrapper, + this->shared_from_this(), std::placeholders::_1, std::placeholders::_2)); #endif - return true; + return false; } std::mutex _writeLock; std::queue _writeQueue; -#ifndef BOOST_ASIO_HAS_IOCP +#ifndef TC_SOCKET_USE_IOCP MessageBuffer _writeBuffer; #endif @@ -179,7 +185,7 @@ private: ReadHandler(); } -#ifdef BOOST_ASIO_HAS_IOCP +#ifdef TC_SOCKET_USE_IOCP void WriteHandler(boost::system::error_code error, std::size_t transferedBytes) { @@ -203,12 +209,15 @@ private: #else - bool WriteHandler(boost::system::error_code /*error*/, std::size_t /*transferedBytes*/) + void WriteHandlerWrapper(boost::system::error_code /*error*/, std::size_t /*transferedBytes*/) { - std::unique_lock guard(_writeLock, std::try_to_lock); - if (!guard) - return false; + std::unique_lock guard(_writeLock); + _isWritingAsync = false; + WriteHandler(guard); + } + bool WriteHandler(std::unique_lock& guard) + { if (!IsOpen()) return false; @@ -229,7 +238,7 @@ private: } else if (bytesWritten == 0) return false; - else if (bytesWritten < bytesToSend) //now n > 0 + else if (bytesWritten < bytesToSend) { _writeBuffer.ReadCompleted(bytesWritten); _writeBuffer.Normalize(); @@ -245,10 +254,7 @@ private: bool HandleQueue(std::unique_lock& guard) { if (_writeQueue.empty()) - { - _isWritingAsync = false; return false; - } MessageBuffer& queuedMessage = _writeQueue.front(); @@ -277,13 +283,7 @@ private: } _writeQueue.pop(); - if (_writeQueue.empty()) - { - _isWritingAsync = false; - return false; - } - - return true; + return !_writeQueue.empty(); } #endif -- cgit v1.2.3