mirror of
https://github.com/mangosfour/server.git
synced 2025-12-16 04:37:00 +00:00
* Remove unused now TcpNoDelay config option. Patch provided by TonyMontana5000.
* Fixed: move includes to prevent fail rebuild WorldSocket.cpp. * Apply code style to WorldSocket.* and WorldSocketMgr.cpp
This commit is contained in:
parent
245f868521
commit
398b5535b7
6 changed files with 343 additions and 350 deletions
|
|
@ -558,7 +558,6 @@ void World::LoadConfigSettings(bool reload)
|
||||||
else
|
else
|
||||||
m_configs[CONFIG_SOCKET_SELECTTIME] = sConfig.GetIntDefault("SocketSelectTime", DEFAULT_SOCKET_SELECT_TIME);
|
m_configs[CONFIG_SOCKET_SELECTTIME] = sConfig.GetIntDefault("SocketSelectTime", DEFAULT_SOCKET_SELECT_TIME);
|
||||||
|
|
||||||
m_configs[CONFIG_TCP_NO_DELAY] = sConfig.GetBoolDefault("TcpNoDelay", false);
|
|
||||||
m_configs[CONFIG_GROUP_XP_DISTANCE] = sConfig.GetIntDefault("MaxGroupXPDistance", 74);
|
m_configs[CONFIG_GROUP_XP_DISTANCE] = sConfig.GetIntDefault("MaxGroupXPDistance", 74);
|
||||||
/// \todo Add MonsterSight and GuarderSight (with meaning) in mangosd.conf or put them as define
|
/// \todo Add MonsterSight and GuarderSight (with meaning) in mangosd.conf or put them as define
|
||||||
m_configs[CONFIG_SIGHT_MONSTER] = sConfig.GetIntDefault("MonsterSight", 50);
|
m_configs[CONFIG_SIGHT_MONSTER] = sConfig.GetIntDefault("MonsterSight", 50);
|
||||||
|
|
|
||||||
|
|
@ -146,7 +146,6 @@ enum WorldConfigs
|
||||||
CONFIG_SILENTLY_GM_JOIN_TO_CHANNEL,
|
CONFIG_SILENTLY_GM_JOIN_TO_CHANNEL,
|
||||||
CONFIG_TALENTS_INSPECTING,
|
CONFIG_TALENTS_INSPECTING,
|
||||||
CONFIG_CHAT_FAKE_MESSAGE_PREVENTING,
|
CONFIG_CHAT_FAKE_MESSAGE_PREVENTING,
|
||||||
CONFIG_TCP_NO_DELAY,
|
|
||||||
CONFIG_CORPSE_DECAY_NORMAL,
|
CONFIG_CORPSE_DECAY_NORMAL,
|
||||||
CONFIG_CORPSE_DECAY_RARE,
|
CONFIG_CORPSE_DECAY_RARE,
|
||||||
CONFIG_CORPSE_DECAY_ELITE,
|
CONFIG_CORPSE_DECAY_ELITE,
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,6 @@
|
||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Common.h"
|
|
||||||
#include "WorldSocket.h"
|
|
||||||
#include <ace/Message_Block.h>
|
#include <ace/Message_Block.h>
|
||||||
#include <ace/OS_NS_string.h>
|
#include <ace/OS_NS_string.h>
|
||||||
#include <ace/OS_NS_unistd.h>
|
#include <ace/OS_NS_unistd.h>
|
||||||
|
|
@ -29,6 +27,9 @@
|
||||||
#include <ace/Reactor.h>
|
#include <ace/Reactor.h>
|
||||||
#include <ace/Auto_Ptr.h>
|
#include <ace/Auto_Ptr.h>
|
||||||
|
|
||||||
|
#include "WorldSocket.h"
|
||||||
|
#include "Common.h"
|
||||||
|
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "World.h"
|
#include "World.h"
|
||||||
#include "WorldPacket.h"
|
#include "WorldPacket.h"
|
||||||
|
|
|
||||||
|
|
@ -88,144 +88,144 @@ typedef ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> WorldHandler;
|
||||||
*/
|
*/
|
||||||
class WorldSocket : protected WorldHandler
|
class WorldSocket : protected WorldHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Declare some friends
|
/// Declare some friends
|
||||||
friend class ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR >;
|
friend class ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR >;
|
||||||
friend class WorldSocketMgr;
|
friend class WorldSocketMgr;
|
||||||
friend class ReactorRunnable;
|
friend class ReactorRunnable;
|
||||||
|
|
||||||
/// Declare the acceptor for this class
|
/// Declare the acceptor for this class
|
||||||
typedef ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR > Acceptor;
|
typedef ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR > Acceptor;
|
||||||
|
|
||||||
/// Mutex type used for various syncronizations.
|
/// Mutex type used for various syncronizations.
|
||||||
typedef ACE_Thread_Mutex LockType;
|
typedef ACE_Thread_Mutex LockType;
|
||||||
typedef ACE_Guard<LockType> GuardType;
|
typedef ACE_Guard<LockType> GuardType;
|
||||||
|
|
||||||
/// Queue for storing packets for which there is no space.
|
/// Queue for storing packets for which there is no space.
|
||||||
typedef ACE_Unbounded_Queue< WorldPacket* > PacketQueueT;
|
typedef ACE_Unbounded_Queue< WorldPacket* > PacketQueueT;
|
||||||
|
|
||||||
/// Check if socket is closed.
|
/// Check if socket is closed.
|
||||||
bool IsClosed (void) const;
|
bool IsClosed (void) const;
|
||||||
|
|
||||||
/// Close the socket.
|
/// Close the socket.
|
||||||
void CloseSocket (void);
|
void CloseSocket (void);
|
||||||
|
|
||||||
/// Get address of connected peer.
|
/// Get address of connected peer.
|
||||||
const std::string& GetRemoteAddress (void) const;
|
const std::string& GetRemoteAddress (void) const;
|
||||||
|
|
||||||
/// Send A packet on the socket, this function is reentrant.
|
/// Send A packet on the socket, this function is reentrant.
|
||||||
/// @param pct packet to send
|
/// @param pct packet to send
|
||||||
/// @return -1 of failure
|
/// @return -1 of failure
|
||||||
int SendPacket (const WorldPacket& pct);
|
int SendPacket (const WorldPacket& pct);
|
||||||
|
|
||||||
/// Add refference to this object.
|
/// Add refference to this object.
|
||||||
long AddReference (void);
|
long AddReference (void);
|
||||||
|
|
||||||
/// Remove refference to this object.
|
/// Remove refference to this object.
|
||||||
long RemoveReference (void);
|
long RemoveReference (void);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/// things called by ACE framework.
|
/// things called by ACE framework.
|
||||||
WorldSocket (void);
|
WorldSocket (void);
|
||||||
virtual ~WorldSocket (void);
|
virtual ~WorldSocket (void);
|
||||||
|
|
||||||
/// Called on open ,the void* is the acceptor.
|
/// Called on open ,the void* is the acceptor.
|
||||||
virtual int open (void *);
|
virtual int open (void *);
|
||||||
|
|
||||||
/// Called on failures inside of the acceptor, don't call from your code.
|
/// Called on failures inside of the acceptor, don't call from your code.
|
||||||
virtual int close (int);
|
virtual int close (int);
|
||||||
|
|
||||||
/// Called when we can read from the socket.
|
/// Called when we can read from the socket.
|
||||||
virtual int handle_input (ACE_HANDLE = ACE_INVALID_HANDLE);
|
virtual int handle_input (ACE_HANDLE = ACE_INVALID_HANDLE);
|
||||||
|
|
||||||
/// Called when the socket can write.
|
/// Called when the socket can write.
|
||||||
virtual int handle_output (ACE_HANDLE = ACE_INVALID_HANDLE);
|
virtual int handle_output (ACE_HANDLE = ACE_INVALID_HANDLE);
|
||||||
|
|
||||||
/// Called when connection is closed or error happens.
|
/// Called when connection is closed or error happens.
|
||||||
virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE,
|
virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE,
|
||||||
ACE_Reactor_Mask = ACE_Event_Handler::ALL_EVENTS_MASK);
|
ACE_Reactor_Mask = ACE_Event_Handler::ALL_EVENTS_MASK);
|
||||||
|
|
||||||
/// Called by WorldSocketMgr/ReactorRunnable.
|
/// Called by WorldSocketMgr/ReactorRunnable.
|
||||||
int Update (void);
|
int Update (void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Helper functions for processing incoming data.
|
/// Helper functions for processing incoming data.
|
||||||
int handle_input_header (void);
|
int handle_input_header (void);
|
||||||
int handle_input_payload (void);
|
int handle_input_payload (void);
|
||||||
int handle_input_missing_data (void);
|
int handle_input_missing_data (void);
|
||||||
|
|
||||||
/// Help functions to mark/unmark the socket for output.
|
/// Help functions to mark/unmark the socket for output.
|
||||||
/// @param g the guard is for m_OutBufferLock, the function will release it
|
/// @param g the guard is for m_OutBufferLock, the function will release it
|
||||||
int cancel_wakeup_output (GuardType& g);
|
int cancel_wakeup_output (GuardType& g);
|
||||||
int schedule_wakeup_output (GuardType& g);
|
int schedule_wakeup_output (GuardType& g);
|
||||||
|
|
||||||
/// process one incoming packet.
|
/// process one incoming packet.
|
||||||
/// @param new_pct received packet ,note that you need to delete it.
|
/// @param new_pct received packet ,note that you need to delete it.
|
||||||
int ProcessIncoming (WorldPacket* new_pct);
|
int ProcessIncoming (WorldPacket* new_pct);
|
||||||
|
|
||||||
/// Called by ProcessIncoming() on CMSG_AUTH_SESSION.
|
/// Called by ProcessIncoming() on CMSG_AUTH_SESSION.
|
||||||
int HandleAuthSession (WorldPacket& recvPacket);
|
int HandleAuthSession (WorldPacket& recvPacket);
|
||||||
|
|
||||||
/// Called by ProcessIncoming() on CMSG_PING.
|
/// Called by ProcessIncoming() on CMSG_PING.
|
||||||
int HandlePing (WorldPacket& recvPacket);
|
int HandlePing (WorldPacket& recvPacket);
|
||||||
|
|
||||||
/// Try to write WorldPacket to m_OutBuffer ,return -1 if no space
|
/// Try to write WorldPacket to m_OutBuffer ,return -1 if no space
|
||||||
/// Need to be called with m_OutBufferLock lock held
|
/// Need to be called with m_OutBufferLock lock held
|
||||||
int iSendPacket (const WorldPacket& pct);
|
int iSendPacket (const WorldPacket& pct);
|
||||||
|
|
||||||
/// Flush m_PacketQueue if there are packets in it
|
/// Flush m_PacketQueue if there are packets in it
|
||||||
/// Need to be called with m_OutBufferLock lock held
|
/// Need to be called with m_OutBufferLock lock held
|
||||||
/// @return true if it wrote to the buffer ( AKA you need
|
/// @return true if it wrote to the buffer ( AKA you need
|
||||||
/// to mark the socket for output ).
|
/// to mark the socket for output ).
|
||||||
bool iFlushPacketQueue ();
|
bool iFlushPacketQueue ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Time in which the last ping was received
|
/// Time in which the last ping was received
|
||||||
ACE_Time_Value m_LastPingTime;
|
ACE_Time_Value m_LastPingTime;
|
||||||
|
|
||||||
/// Keep track of overspeed pings ,to prevent ping flood.
|
/// Keep track of overspeed pings ,to prevent ping flood.
|
||||||
uint32 m_OverSpeedPings;
|
uint32 m_OverSpeedPings;
|
||||||
|
|
||||||
/// Address of the remote peer
|
/// Address of the remote peer
|
||||||
std::string m_Address;
|
std::string m_Address;
|
||||||
|
|
||||||
/// Class used for managing encryption of the headers
|
/// Class used for managing encryption of the headers
|
||||||
AuthCrypt m_Crypt;
|
AuthCrypt m_Crypt;
|
||||||
|
|
||||||
/// Mutex lock to protect m_Session
|
/// Mutex lock to protect m_Session
|
||||||
LockType m_SessionLock;
|
LockType m_SessionLock;
|
||||||
|
|
||||||
/// Session to which recieved packets are routed
|
/// Session to which recieved packets are routed
|
||||||
WorldSession* m_Session;
|
WorldSession* m_Session;
|
||||||
|
|
||||||
/// here are stored the fragmens of the recieved data
|
/// here are stored the fragmens of the recieved data
|
||||||
WorldPacket* m_RecvWPct;
|
WorldPacket* m_RecvWPct;
|
||||||
|
|
||||||
/// This block actually refers to m_RecvWPct contents,
|
/// This block actually refers to m_RecvWPct contents,
|
||||||
/// which alows easy and safe writing to it.
|
/// which alows easy and safe writing to it.
|
||||||
/// It wont free memory when its deleted. m_RecvWPct takes care of freeing.
|
/// It wont free memory when its deleted. m_RecvWPct takes care of freeing.
|
||||||
ACE_Message_Block m_RecvPct;
|
ACE_Message_Block m_RecvPct;
|
||||||
|
|
||||||
/// Fragment of the recieved header.
|
/// Fragment of the recieved header.
|
||||||
ACE_Message_Block m_Header;
|
ACE_Message_Block m_Header;
|
||||||
|
|
||||||
/// Mutex for protecting otuput related data.
|
/// Mutex for protecting otuput related data.
|
||||||
LockType m_OutBufferLock;
|
LockType m_OutBufferLock;
|
||||||
|
|
||||||
/// Buffer used for writing output.
|
/// Buffer used for writing output.
|
||||||
ACE_Message_Block *m_OutBuffer;
|
ACE_Message_Block *m_OutBuffer;
|
||||||
|
|
||||||
/// Size of the m_OutBuffer.
|
/// Size of the m_OutBuffer.
|
||||||
size_t m_OutBufferSize;
|
size_t m_OutBufferSize;
|
||||||
|
|
||||||
/// Here are stored packets for which there was no space on m_OutBuffer,
|
/// Here are stored packets for which there was no space on m_OutBuffer,
|
||||||
/// this alows not-to kick player if its buffer is overflowed.
|
/// this alows not-to kick player if its buffer is overflowed.
|
||||||
PacketQueueT m_PacketQueue;
|
PacketQueueT m_PacketQueue;
|
||||||
|
|
||||||
/// True if the socket is registered with the reactor for output
|
/// True if the socket is registered with the reactor for output
|
||||||
bool m_OutActive;
|
bool m_OutActive;
|
||||||
|
|
||||||
uint32 m_Seed;
|
uint32 m_Seed;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* _WORLDSOCKET_H */
|
#endif /* _WORLDSOCKET_H */
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,25 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2005-2008,2007 MaNGOS <http://getmangos.com/>
|
* Copyright (C) 2005-2008,2007 MaNGOS <http://getmangos.com/>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation; either version 2 of the License, or
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program; if not, write to the Free Software
|
* along with this program; if not, write to the Free Software
|
||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** \file WorldSocketMgr.cpp
|
/** \file WorldSocketMgr.cpp
|
||||||
* \ingroup u2w
|
* \ingroup u2w
|
||||||
* \author Derex <derex101@gmail.com>
|
* \author Derex <derex101@gmail.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "WorldSocketMgr.h"
|
#include "WorldSocketMgr.h"
|
||||||
|
|
||||||
|
|
@ -45,326 +45,324 @@
|
||||||
#include "WorldSocket.h"
|
#include "WorldSocket.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a helper class to WorldSocketMgr ,that manages
|
* This is a helper class to WorldSocketMgr ,that manages
|
||||||
* network threads, and assigning connections from acceptor thread
|
* network threads, and assigning connections from acceptor thread
|
||||||
* to other network threads
|
* to other network threads
|
||||||
*/
|
*/
|
||||||
class ReactorRunnable : protected ACE_Task_Base
|
class ReactorRunnable : protected ACE_Task_Base
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
ReactorRunnable () :
|
ReactorRunnable () :
|
||||||
m_ThreadId (-1),
|
m_ThreadId (-1),
|
||||||
m_Connections (0),
|
m_Connections (0),
|
||||||
m_Reactor (0)
|
m_Reactor (0)
|
||||||
{
|
{
|
||||||
ACE_Reactor_Impl* imp = 0;
|
ACE_Reactor_Impl* imp = 0;
|
||||||
|
|
||||||
#if defined (ACE_HAS_EVENT_POLL) || defined (ACE_HAS_DEV_POLL)
|
#if defined (ACE_HAS_EVENT_POLL) || defined (ACE_HAS_DEV_POLL)
|
||||||
imp = new ACE_Dev_Poll_Reactor ();
|
|
||||||
|
|
||||||
imp->max_notify_iterations (128);
|
imp = new ACE_Dev_Poll_Reactor ();
|
||||||
imp->restart (1);
|
|
||||||
#else
|
|
||||||
imp = new ACE_TP_Reactor ();
|
|
||||||
imp->max_notify_iterations (128);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
m_Reactor = new ACE_Reactor (imp, 1);
|
imp->max_notify_iterations (128);
|
||||||
}
|
imp->restart (1);
|
||||||
|
|
||||||
virtual
|
#else
|
||||||
~ReactorRunnable ()
|
|
||||||
{
|
|
||||||
this->Stop ();
|
|
||||||
this->Wait ();
|
|
||||||
|
|
||||||
if (m_Reactor)
|
imp = new ACE_TP_Reactor ();
|
||||||
delete m_Reactor;
|
imp->max_notify_iterations (128);
|
||||||
}
|
|
||||||
|
|
||||||
void
|
#endif
|
||||||
Stop ()
|
|
||||||
{
|
|
||||||
m_Reactor->end_reactor_event_loop ();
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
m_Reactor = new ACE_Reactor (imp, 1);
|
||||||
Start ()
|
}
|
||||||
{
|
|
||||||
if (m_ThreadId != -1)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return (m_ThreadId = this->activate ());
|
virtual ~ReactorRunnable ()
|
||||||
}
|
{
|
||||||
|
this->Stop ();
|
||||||
|
this->Wait ();
|
||||||
|
|
||||||
void
|
if (m_Reactor)
|
||||||
Wait ()
|
delete m_Reactor;
|
||||||
{
|
}
|
||||||
ACE_Task_Base::wait ();
|
|
||||||
}
|
|
||||||
|
|
||||||
long
|
void Stop ()
|
||||||
Connections ()
|
{
|
||||||
{
|
m_Reactor->end_reactor_event_loop ();
|
||||||
return static_cast<long> (m_Connections.value ());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int Start ()
|
||||||
AddSocket (WorldSocket* sock)
|
{
|
||||||
{
|
if (m_ThreadId != -1)
|
||||||
ACE_GUARD_RETURN (ACE_Thread_Mutex, Guard, m_NewSockets_Lock, -1);
|
return -1;
|
||||||
|
|
||||||
++m_Connections;
|
return (m_ThreadId = this->activate ());
|
||||||
sock->AddReference();
|
}
|
||||||
sock->reactor (m_Reactor);
|
|
||||||
m_NewSockets.insert (sock);
|
|
||||||
|
|
||||||
return 0;
|
void Wait () { ACE_Task_Base::wait (); }
|
||||||
}
|
|
||||||
|
|
||||||
ACE_Reactor* GetReactor ()
|
long Connections ()
|
||||||
{
|
{
|
||||||
return m_Reactor;
|
return static_cast<long> (m_Connections.value ());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
int AddSocket (WorldSocket* sock)
|
||||||
|
{
|
||||||
|
ACE_GUARD_RETURN (ACE_Thread_Mutex, Guard, m_NewSockets_Lock, -1);
|
||||||
|
|
||||||
void
|
++m_Connections;
|
||||||
AddNewSockets ()
|
sock->AddReference();
|
||||||
{
|
sock->reactor (m_Reactor);
|
||||||
ACE_GUARD (ACE_Thread_Mutex, Guard, m_NewSockets_Lock);
|
m_NewSockets.insert (sock);
|
||||||
|
|
||||||
if (m_NewSockets.empty ())
|
return 0;
|
||||||
return;
|
}
|
||||||
|
|
||||||
for (SocketSet::iterator i = m_NewSockets.begin (); i != m_NewSockets.end (); ++i)
|
ACE_Reactor* GetReactor ()
|
||||||
{
|
{
|
||||||
WorldSocket* sock = (*i);
|
return m_Reactor;
|
||||||
|
}
|
||||||
|
|
||||||
if (sock->IsClosed ())
|
protected:
|
||||||
{
|
|
||||||
sock->RemoveReference ();
|
|
||||||
--m_Connections;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
m_Sockets.insert (sock);
|
|
||||||
}
|
|
||||||
|
|
||||||
m_NewSockets.clear ();
|
void AddNewSockets ()
|
||||||
}
|
{
|
||||||
|
ACE_GUARD (ACE_Thread_Mutex, Guard, m_NewSockets_Lock);
|
||||||
|
|
||||||
virtual int
|
if (m_NewSockets.empty ())
|
||||||
svc (void)
|
return;
|
||||||
{
|
|
||||||
DEBUG_LOG ("Network Thread Starting");
|
|
||||||
|
|
||||||
WorldDatabase.ThreadStart ();
|
for (SocketSet::iterator i = m_NewSockets.begin (); i != m_NewSockets.end (); ++i)
|
||||||
|
{
|
||||||
|
WorldSocket* sock = (*i);
|
||||||
|
|
||||||
ACE_ASSERT (m_Reactor);
|
if (sock->IsClosed ())
|
||||||
|
{
|
||||||
|
sock->RemoveReference ();
|
||||||
|
--m_Connections;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_Sockets.insert (sock);
|
||||||
|
}
|
||||||
|
|
||||||
SocketSet::iterator i, t;
|
m_NewSockets.clear ();
|
||||||
|
}
|
||||||
|
|
||||||
while (!m_Reactor->reactor_event_loop_done ())
|
virtual int svc ()
|
||||||
{
|
{
|
||||||
// dont be too smart to move this outside the loop
|
DEBUG_LOG ("Network Thread Starting");
|
||||||
// the run_reactor_event_loop will modify interval
|
|
||||||
ACE_Time_Value interval (0, 10000);
|
|
||||||
|
|
||||||
if (m_Reactor->run_reactor_event_loop (interval) == -1)
|
WorldDatabase.ThreadStart ();
|
||||||
break;
|
|
||||||
|
|
||||||
AddNewSockets ();
|
ACE_ASSERT (m_Reactor);
|
||||||
|
|
||||||
for (i = m_Sockets.begin (); i != m_Sockets.end ();)
|
SocketSet::iterator i, t;
|
||||||
{
|
|
||||||
if ((*i)->Update () == -1)
|
|
||||||
{
|
|
||||||
t = i;
|
|
||||||
i++;
|
|
||||||
(*t)->CloseSocket ();
|
|
||||||
(*t)->RemoveReference ();
|
|
||||||
--m_Connections;
|
|
||||||
m_Sockets.erase (t);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
WorldDatabase.ThreadEnd ();
|
while (!m_Reactor->reactor_event_loop_done ())
|
||||||
|
{
|
||||||
|
// dont be too smart to move this outside the loop
|
||||||
|
// the run_reactor_event_loop will modify interval
|
||||||
|
ACE_Time_Value interval (0, 10000);
|
||||||
|
|
||||||
DEBUG_LOG ("Network Thread Exitting");
|
if (m_Reactor->run_reactor_event_loop (interval) == -1)
|
||||||
|
break;
|
||||||
|
|
||||||
return 0;
|
AddNewSockets ();
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
for (i = m_Sockets.begin (); i != m_Sockets.end ();)
|
||||||
typedef ACE_Atomic_Op<ACE_SYNCH_MUTEX, long> AtomicInt;
|
{
|
||||||
typedef std::set<WorldSocket*> SocketSet;
|
if ((*i)->Update () == -1)
|
||||||
|
{
|
||||||
|
t = i;
|
||||||
|
i++;
|
||||||
|
(*t)->CloseSocket ();
|
||||||
|
(*t)->RemoveReference ();
|
||||||
|
--m_Connections;
|
||||||
|
m_Sockets.erase (t);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ACE_Reactor* m_Reactor;
|
WorldDatabase.ThreadEnd ();
|
||||||
AtomicInt m_Connections;
|
|
||||||
int m_ThreadId;
|
|
||||||
|
|
||||||
SocketSet m_Sockets;
|
DEBUG_LOG ("Network Thread Exitting");
|
||||||
|
|
||||||
SocketSet m_NewSockets;
|
return 0;
|
||||||
ACE_Thread_Mutex m_NewSockets_Lock;
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
typedef ACE_Atomic_Op<ACE_SYNCH_MUTEX, long> AtomicInt;
|
||||||
|
typedef std::set<WorldSocket*> SocketSet;
|
||||||
|
|
||||||
|
ACE_Reactor* m_Reactor;
|
||||||
|
AtomicInt m_Connections;
|
||||||
|
int m_ThreadId;
|
||||||
|
|
||||||
|
SocketSet m_Sockets;
|
||||||
|
|
||||||
|
SocketSet m_NewSockets;
|
||||||
|
ACE_Thread_Mutex m_NewSockets_Lock;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
WorldSocketMgr::WorldSocketMgr () :
|
WorldSocketMgr::WorldSocketMgr () :
|
||||||
m_NetThreadsCount (0),
|
m_NetThreadsCount (0),
|
||||||
m_NetThreads (0),
|
m_NetThreads (0),
|
||||||
m_SockOutKBuff (-1),
|
m_SockOutKBuff (-1),
|
||||||
m_SockOutUBuff (65536),
|
m_SockOutUBuff (65536),
|
||||||
m_UseNoDelay (true),
|
m_UseNoDelay (true),
|
||||||
m_Acceptor (0) {}
|
m_Acceptor (0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
WorldSocketMgr::~WorldSocketMgr ()
|
WorldSocketMgr::~WorldSocketMgr ()
|
||||||
{
|
{
|
||||||
if (m_NetThreads)
|
if (m_NetThreads)
|
||||||
delete [] m_NetThreads;
|
delete [] m_NetThreads;
|
||||||
|
|
||||||
if(m_Acceptor)
|
if(m_Acceptor)
|
||||||
delete m_Acceptor;
|
delete m_Acceptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
WorldSocketMgr::StartReactiveIO (ACE_UINT16 port, const char* address)
|
WorldSocketMgr::StartReactiveIO (ACE_UINT16 port, const char* address)
|
||||||
{
|
{
|
||||||
m_UseNoDelay = sConfig.GetBoolDefault ("Network.TcpNodelay", true);
|
m_UseNoDelay = sConfig.GetBoolDefault ("Network.TcpNodelay", true);
|
||||||
|
|
||||||
int num_threads = sConfig.GetIntDefault ("Network.Threads", 1);
|
int num_threads = sConfig.GetIntDefault ("Network.Threads", 1);
|
||||||
|
|
||||||
if (num_threads <= 0)
|
if (num_threads <= 0)
|
||||||
{
|
{
|
||||||
sLog.outError ("Network.Threads is wrong in your config file");
|
sLog.outError ("Network.Threads is wrong in your config file");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_NetThreadsCount = static_cast<size_t> (num_threads + 1);
|
m_NetThreadsCount = static_cast<size_t> (num_threads + 1);
|
||||||
|
|
||||||
m_NetThreads = new ReactorRunnable[m_NetThreadsCount];
|
m_NetThreads = new ReactorRunnable[m_NetThreadsCount];
|
||||||
|
|
||||||
sLog.outBasic ("Max alowed socket connections %d",ACE::max_handles ());
|
sLog.outBasic ("Max alowed socket connections %d",ACE::max_handles ());
|
||||||
|
|
||||||
m_SockOutKBuff = sConfig.GetIntDefault ("Network.OutKBuff", -1); // -1 means use default
|
// -1 means use default
|
||||||
|
m_SockOutKBuff = sConfig.GetIntDefault ("Network.OutKBuff", -1);
|
||||||
|
|
||||||
m_SockOutUBuff = sConfig.GetIntDefault ("Network.OutUBuff", 65536);
|
m_SockOutUBuff = sConfig.GetIntDefault ("Network.OutUBuff", 65536);
|
||||||
|
|
||||||
if ( m_SockOutUBuff <= 0 )
|
if ( m_SockOutUBuff <= 0 )
|
||||||
{
|
{
|
||||||
sLog.outError ("Network.OutUBuff is wrong in your config file");
|
sLog.outError ("Network.OutUBuff is wrong in your config file");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
WorldSocket::Acceptor *acc = new WorldSocket::Acceptor;
|
WorldSocket::Acceptor *acc = new WorldSocket::Acceptor;
|
||||||
m_Acceptor = acc;
|
m_Acceptor = acc;
|
||||||
|
|
||||||
ACE_INET_Addr listen_addr (port, address);
|
ACE_INET_Addr listen_addr (port, address);
|
||||||
|
|
||||||
if (acc->open (listen_addr, m_NetThreads[0].GetReactor (), ACE_NONBLOCK) == -1)
|
if (acc->open (listen_addr, m_NetThreads[0].GetReactor (), ACE_NONBLOCK) == -1)
|
||||||
{
|
{
|
||||||
sLog.outError ("Failed to open acceptor ,check if the port is free");
|
sLog.outError ("Failed to open acceptor ,check if the port is free");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
||||||
m_NetThreads[i].Start ();
|
m_NetThreads[i].Start ();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
WorldSocketMgr::StartNetwork (ACE_UINT16 port, const char* address)
|
WorldSocketMgr::StartNetwork (ACE_UINT16 port, const char* address)
|
||||||
{
|
{
|
||||||
if (!sLog.IsOutDebug ())
|
if (!sLog.IsOutDebug ())
|
||||||
ACE_Log_Msg::instance ()->priority_mask (LM_ERROR, ACE_Log_Msg::PROCESS);
|
ACE_Log_Msg::instance ()->priority_mask (LM_ERROR, ACE_Log_Msg::PROCESS);
|
||||||
|
|
||||||
if (this->StartReactiveIO (port, address) == -1)
|
if (this->StartReactiveIO (port, address) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
WorldSocketMgr::StopNetwork ()
|
WorldSocketMgr::StopNetwork ()
|
||||||
{
|
{
|
||||||
if (m_Acceptor)
|
if (m_Acceptor)
|
||||||
{
|
{
|
||||||
WorldSocket::Acceptor* acc = dynamic_cast<WorldSocket::Acceptor*> (m_Acceptor);
|
WorldSocket::Acceptor* acc = dynamic_cast<WorldSocket::Acceptor*> (m_Acceptor);
|
||||||
|
|
||||||
if (acc)
|
if (acc)
|
||||||
acc->close ();
|
acc->close ();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_NetThreadsCount != 0)
|
if (m_NetThreadsCount != 0)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
||||||
m_NetThreads[i].Stop ();
|
m_NetThreads[i].Stop ();
|
||||||
}
|
}
|
||||||
|
|
||||||
this->Wait ();
|
this->Wait ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
WorldSocketMgr::Wait ()
|
WorldSocketMgr::Wait ()
|
||||||
{
|
{
|
||||||
if (m_NetThreadsCount != 0)
|
if (m_NetThreadsCount != 0)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
||||||
m_NetThreads[i].Wait ();
|
m_NetThreads[i].Wait ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
WorldSocketMgr::OnSocketOpen (WorldSocket* sock)
|
WorldSocketMgr::OnSocketOpen (WorldSocket* sock)
|
||||||
{
|
{
|
||||||
// set some options here
|
// set some options here
|
||||||
if (m_SockOutKBuff >= 0)
|
if (m_SockOutKBuff >= 0)
|
||||||
if (sock->peer ().set_option (SOL_SOCKET,
|
{
|
||||||
SO_SNDBUF,
|
if (sock->peer ().set_option (SOL_SOCKET,
|
||||||
(void*) & m_SockOutKBuff,
|
SO_SNDBUF,
|
||||||
sizeof (int)) == -1 && errno != ENOTSUP)
|
(void*) & m_SockOutKBuff,
|
||||||
{
|
sizeof (int)) == -1 && errno != ENOTSUP)
|
||||||
sLog.outError ("WorldSocketMgr::OnSocketOpen set_option SO_SNDBUF");
|
{
|
||||||
return -1;
|
sLog.outError ("WorldSocketMgr::OnSocketOpen set_option SO_SNDBUF");
|
||||||
}
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static const int ndoption = 1;
|
static const int ndoption = 1;
|
||||||
|
|
||||||
// Set TCP_NODELAY.
|
// Set TCP_NODELAY.
|
||||||
if (m_UseNoDelay)
|
if (m_UseNoDelay)
|
||||||
if (sock->peer ().set_option (ACE_IPPROTO_TCP,
|
{
|
||||||
TCP_NODELAY,
|
if (sock->peer ().set_option (ACE_IPPROTO_TCP,
|
||||||
(void*) & ndoption,
|
TCP_NODELAY,
|
||||||
sizeof (int)) == -1)
|
(void*)&ndoption,
|
||||||
{
|
sizeof (int)) == -1)
|
||||||
sLog.outError ("WorldSocketMgr::OnSocketOpen: peer ().set_option TCP_NODELAY errno = %s", ACE_OS::strerror (errno));
|
{
|
||||||
return -1;
|
sLog.outError ("WorldSocketMgr::OnSocketOpen: peer ().set_option TCP_NODELAY errno = %s", ACE_OS::strerror (errno));
|
||||||
}
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sock->m_OutBufferSize = static_cast<size_t> (m_SockOutUBuff);
|
sock->m_OutBufferSize = static_cast<size_t> (m_SockOutUBuff);
|
||||||
|
|
||||||
// we skip the Acceptor Thread
|
// we skip the Acceptor Thread
|
||||||
size_t min = 1;
|
size_t min = 1;
|
||||||
|
|
||||||
ACE_ASSERT (m_NetThreadsCount >= 1);
|
ACE_ASSERT (m_NetThreadsCount >= 1);
|
||||||
|
|
||||||
for (size_t i = 1; i < m_NetThreadsCount; ++i)
|
for (size_t i = 1; i < m_NetThreadsCount; ++i)
|
||||||
if (m_NetThreads[i].Connections () < m_NetThreads[min].Connections ())
|
if (m_NetThreads[i].Connections () < m_NetThreads[min].Connections ())
|
||||||
min = i;
|
min = i;
|
||||||
|
|
||||||
return m_NetThreads[min].AddSocket (sock);
|
return m_NetThreads[min].AddSocket (sock);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
WorldSocketMgr*
|
WorldSocketMgr*
|
||||||
WorldSocketMgr::Instance ()
|
WorldSocketMgr::Instance ()
|
||||||
{
|
{
|
||||||
return ACE_Singleton<WorldSocketMgr,ACE_Thread_Mutex>::instance();
|
return ACE_Singleton<WorldSocketMgr,ACE_Thread_Mutex>::instance();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -70,11 +70,6 @@ BindIP = "0.0.0.0"
|
||||||
# Default: 1 (speed)
|
# Default: 1 (speed)
|
||||||
# 9 (best compression)
|
# 9 (best compression)
|
||||||
#
|
#
|
||||||
# TcpNoDelay
|
|
||||||
# TCP Nagle algorithm setting
|
|
||||||
# Default: 0 (enable Nagle algorithm, less traffic, more latency)
|
|
||||||
# 1 (TCP_NO_DELAY, disable Nagle algorithm, more traffic but less latency)
|
|
||||||
#
|
|
||||||
# PlayerLimit
|
# PlayerLimit
|
||||||
# Maximum number of players in the world. Excluding Mods, GM's and Admins
|
# Maximum number of players in the world. Excluding Mods, GM's and Admins
|
||||||
# Default: 100
|
# Default: 100
|
||||||
|
|
@ -1026,23 +1021,24 @@ Death.CorpseReclaimDelay.PvE = 1
|
||||||
|
|
||||||
###################################################################################################################
|
###################################################################################################################
|
||||||
#
|
#
|
||||||
# Network config
|
# NETWORK CONFIG
|
||||||
#
|
#
|
||||||
# Threads: Number of threads for network, recommend 1 thread per 1000 connections.
|
# Network.Threads
|
||||||
# Default: 1
|
# Number of threads for network, recommend 1 thread per 1000 connections.
|
||||||
|
# Default: 1
|
||||||
#
|
#
|
||||||
# OutKBuff: The size of the output kernel buffer used ( SO_SNDBUF socket option, tcp manual ).
|
# Network.OutKBuff
|
||||||
# Default: -1 (Use system default setting)
|
# The size of the output kernel buffer used ( SO_SNDBUF socket option, tcp manual ).
|
||||||
#
|
# Default: -1 (Use system default setting)
|
||||||
# OutUBuff: Userspace buffer for output. This is amount of memory reserved per each connection.
|
|
||||||
# Default: 65536
|
|
||||||
#
|
|
||||||
# TcpNoDelay:
|
|
||||||
# TCP Nagle algorithm setting
|
|
||||||
# Default: 0 (enable Nagle algorithm, less traffic, more latency)
|
|
||||||
# 1 (TCP_NO_DELAY, disable Nagle algorithm, more traffic but less latency)
|
|
||||||
#
|
#
|
||||||
|
# Network.OutUBuff
|
||||||
|
# Userspace buffer for output. This is amount of memory reserved per each connection.
|
||||||
|
# Default: 65536
|
||||||
#
|
#
|
||||||
|
# Network.TcpNoDelay:
|
||||||
|
# TCP Nagle algorithm setting
|
||||||
|
# Default: 0 (enable Nagle algorithm, less traffic, more latency)
|
||||||
|
# 1 (TCP_NO_DELAY, disable Nagle algorithm, more traffic but less latency)
|
||||||
#
|
#
|
||||||
###################################################################################################################
|
###################################################################################################################
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue