Skip to content

Commit

Permalink
More intelligent socket cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
namreeb committed Apr 11, 2016
1 parent 6be3223 commit 0fb722c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/game/WorldSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class WorldSocket : public MaNGOS::Socket
void FinalizeSession() { m_sessionFinalized = true; }

virtual bool Open() override;
virtual bool Deletable() const override { return m_sessionFinalized && Socket::Deletable(); }
virtual bool Deletable() const override { return (m_sessionFinalized || IsClosed()) && Socket::Deletable(); }

/// Return the session key
BigNumber &GetSessionKey() { return m_s; }
Expand Down
24 changes: 22 additions & 2 deletions src/shared/Network/NetworkThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace MaNGOS
class NetworkThread
{
private:
const int WorkDelay = 500;
static const int WorkDelay = 500;

boost::asio::io_service m_service;

Expand All @@ -47,6 +47,9 @@ namespace MaNGOS
boost::asio::io_service::work m_work;

std::mutex m_closingSocketLock;
// most people think that you should always use a vector rather than a list, but i believe that this is an exception
// because this collection can potentially get rather large and we will frequently be removing elements at an arbitrary
// position within it.
std::list<std::unique_ptr<SocketType>> m_closingSockets;

std::atomic<bool> m_pendingShutdown;
Expand All @@ -66,6 +69,16 @@ namespace MaNGOS

~NetworkThread()
{
// we do not lock the list here because Close() will call RemoveSocket which needs the lock
while (!m_sockets.empty())
{
// if it is already closed (which can happen with the placeholder socket for a pending accept), just remove it
if (m_sockets.front()->IsClosed())
m_sockets.erase(m_sockets.begin());
else
m_sockets.front()->Close();
}

m_pendingShutdown = true;
m_socketCleanupThread.join();
}
Expand All @@ -83,6 +96,7 @@ namespace MaNGOS
if (i->get() == socket)
{
m_closingSockets.push_front(std::move(*i));
m_sockets.erase(i);
return;
}
}
Expand All @@ -91,14 +105,20 @@ namespace MaNGOS
template <typename SocketType>
void NetworkThread<SocketType>::SocketCleanupWork()
{
while (!m_pendingShutdown)
while (!m_pendingShutdown || !m_closingSockets.empty())
{
{
std::lock_guard<std::mutex> guard(m_closingSocketLock);

for (auto i = m_closingSockets.begin(); i != m_closingSockets.end(); i++)
if (i->get()->Deletable())
{
i = m_closingSockets.erase(i);

// avoid incrementing the end iterator
if (i == m_closingSockets.end())
break;
}
}

std::this_thread::sleep_for(std::chrono::milliseconds(WorkDelay));
Expand Down
4 changes: 3 additions & 1 deletion src/shared/Network/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void Socket::OnRead(const boost::system::error_code &error, size_t length)
{
if (error)
{
m_readState = ReadState::Idle;
OnError(error);
return;
}
Expand Down Expand Up @@ -152,7 +153,8 @@ void Socket::OnRead(const boost::system::error_code &error, size_t length)
void Socket::OnError(const boost::system::error_code &error)
{
// skip logging this code because it happens whenever anyone disconnects. reduces spam.
if (error != boost::asio::error::eof)
if (error != boost::asio::error::eof &&
error != boost::asio::error::operation_aborted)
sLog.outBasic("Socket::OnError. %s. Connection closed.", error.message().c_str());

if (!IsClosed())
Expand Down
2 changes: 1 addition & 1 deletion src/shared/Network/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace MaNGOS
private:
// buffer timeout period, in milliseconds. higher values decrease responsiveness
// ingame but increase bandwidth efficiency by reducing tcp overhead.
const int BufferTimeout = 50;
static const int BufferTimeout = 50;

enum class WriteState
{
Expand Down

0 comments on commit 0fb722c

Please sign in to comment.