aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2014-09-17 23:06:34 +0200
committerShauren <shauren.trinity@gmail.com>2014-09-17 23:06:34 +0200
commite57a63939eca1988df87ff6dbd72150ee1fdb950 (patch)
tree2624d57ce58a4ace91ccdf336c5eb59cb86580ac /src
parent86f9b1d3e24f38c6056d35908e49a06d340fdaae (diff)
Core/NetworkIO: Fixed queued packets not being properly sent causing players to be stuck during loading
Closes #13120
Diffstat (limited to 'src')
-rw-r--r--src/server/game/Server/WorldSocket.cpp2
-rw-r--r--src/server/shared/Networking/Socket.h48
2 files changed, 25 insertions, 25 deletions
diff --git a/src/server/game/Server/WorldSocket.cpp b/src/server/game/Server/WorldSocket.cpp
index ef92a59a73d..cf76812473e 100644
--- a/src/server/game/Server/WorldSocket.cpp
+++ b/src/server/game/Server/WorldSocket.cpp
@@ -211,7 +211,7 @@ void WorldSocket::SendPacket(WorldPacket& packet)
_authCrypt.EncryptSend(header.header, header.getHeaderLength());
-#ifndef BOOST_ASIO_HAS_IOCP
+#ifndef TC_SOCKET_USE_IOCP
if (_writeQueue.empty() && _writeBuffer.GetRemainingSpace() >= header.getHeaderLength() + packet.size())
{
_writeBuffer.Write(header.header, header.getHeaderLength());
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 T>
class Socket : public std::enable_shared_from_this<T>
@@ -58,11 +59,15 @@ public:
if (!IsOpen())
return false;
-#ifndef BOOST_ASIO_HAS_IOCP
+#ifndef TC_SOCKET_USE_IOCP
+ std::unique_lock<std::mutex> 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<std::mutex>&)
{
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<T>::WriteHandler,
this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
#else
- _socket.async_write_some(boost::asio::null_buffers(), std::bind(&Socket<T>::WriteHandler, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
+ _socket.async_write_some(boost::asio::null_buffers(), std::bind(&Socket<T>::WriteHandlerWrapper,
+ this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
#endif
- return true;
+ return false;
}
std::mutex _writeLock;
std::queue<MessageBuffer> _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<std::mutex> guard(_writeLock, std::try_to_lock);
- if (!guard)
- return false;
+ std::unique_lock<std::mutex> guard(_writeLock);
+ _isWritingAsync = false;
+ WriteHandler(guard);
+ }
+ bool WriteHandler(std::unique_lock<std::mutex>& 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<std::mutex>& 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