Skip to content

Commit

Permalink
Disconnect idle connections that don't login.
Browse files Browse the repository at this point in the history
  • Loading branch information
ratkosrb committed Feb 8, 2025
1 parent b2e78c0 commit 7f01436
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/framework/Network/MangosSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ class MangosSocket : public WorldHandler
virtual int close(int);

// Get address of connected peer.
const std::string& GetRemoteAddress () const { return m_address; }
std::string const& GetRemoteAddress () const { return m_address; }

// Send A packet on the socket, this function is reentrant.
// @param pct packet to send
// @return -1 of failure
int SendPacket (const WorldPacket& pct);
int SendPacket (WorldPacket const& pct);

// Add reference to this object.
long AddReference() { return static_cast<long>(add_reference()); }
Expand Down Expand Up @@ -179,7 +179,10 @@ class MangosSocket : public WorldHandler
// to mark the socket for output ).
bool iFlushPacketQueue ();

// Time in which the last ping was received
// Time at which the socket was created.
ACE_Time_Value m_createTime;

// Time at which the last ping was received.
ACE_Time_Value m_lastPingTime;

// Keep track of over-speed pings ,to prevent ping flood.
Expand Down
10 changes: 9 additions & 1 deletion src/framework/Network/MangosSocketImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
template <typename SessionType, typename SocketName, typename Crypt>
MangosSocket<SessionType, SocketName, Crypt>::MangosSocket() :
WorldHandler(),
m_createTime(ACE_OS::gettimeofday()),
m_lastPingTime(ACE_Time_Value::zero),
m_overSpeedPings(0),
m_session(0),
Expand Down Expand Up @@ -77,7 +78,7 @@ void MangosSocket<SessionType, SocketName, Crypt>::CloseSocket(void)
}

template <typename SessionType, typename SocketName, typename Crypt>
int MangosSocket<SessionType, SocketName, Crypt>::SendPacket(const WorldPacket& pct)
int MangosSocket<SessionType, SocketName, Crypt>::SendPacket(WorldPacket const& pct)
{
GuardType lock(m_outBufferLock);

Expand Down Expand Up @@ -283,6 +284,13 @@ int MangosSocket<SessionType, SocketName, Crypt>::Update(void)
{
if (closing_)
return -1;

if (!m_session && m_isServerSocket && ((ACE_OS::gettimeofday() - m_createTime) > ACE_Time_Value(10)))
{
sLog.Out(LOG_BASIC, LOG_LVL_DETAIL, "Disconnecting idle connection from %s.", m_address.c_str());
CloseSocket();
return -1;
}

if (m_outActive || m_outBuffer->length() == 0)
return 0;
Expand Down

0 comments on commit 7f01436

Please sign in to comment.