Core/NetworkIO: Fixed queued packets not being properly sent causing players to be stuck during loading

Closes #13120
This commit is contained in:
Shauren
2014-09-17 23:06:34 +02:00
parent 86f9b1d3e2
commit e57a63939e
2 changed files with 25 additions and 25 deletions

View File

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

View File

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