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
|
|
@ -1,25 +1,25 @@
|
|||
/*
|
||||
* Copyright (C) 2005-2008,2007 MaNGOS <http://getmangos.com/>
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
* Copyright (C) 2005-2008,2007 MaNGOS <http://getmangos.com/>
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/** \file WorldSocketMgr.cpp
|
||||
* \ingroup u2w
|
||||
* \author Derex <derex101@gmail.com>
|
||||
*/
|
||||
* \ingroup u2w
|
||||
* \author Derex <derex101@gmail.com>
|
||||
*/
|
||||
|
||||
#include "WorldSocketMgr.h"
|
||||
|
||||
|
|
@ -45,326 +45,324 @@
|
|||
#include "WorldSocket.h"
|
||||
|
||||
/**
|
||||
* This is a helper class to WorldSocketMgr ,that manages
|
||||
* network threads, and assigning connections from acceptor thread
|
||||
* to other network threads
|
||||
*/
|
||||
* This is a helper class to WorldSocketMgr ,that manages
|
||||
* network threads, and assigning connections from acceptor thread
|
||||
* to other network threads
|
||||
*/
|
||||
class ReactorRunnable : protected ACE_Task_Base
|
||||
{
|
||||
public:
|
||||
public:
|
||||
|
||||
ReactorRunnable () :
|
||||
m_ThreadId (-1),
|
||||
m_Connections (0),
|
||||
m_Reactor (0)
|
||||
{
|
||||
ACE_Reactor_Impl* imp = 0;
|
||||
ReactorRunnable () :
|
||||
m_ThreadId (-1),
|
||||
m_Connections (0),
|
||||
m_Reactor (0)
|
||||
{
|
||||
ACE_Reactor_Impl* imp = 0;
|
||||
|
||||
#if defined (ACE_HAS_EVENT_POLL) || defined (ACE_HAS_DEV_POLL)
|
||||
imp = new ACE_Dev_Poll_Reactor ();
|
||||
#if defined (ACE_HAS_EVENT_POLL) || defined (ACE_HAS_DEV_POLL)
|
||||
|
||||
imp->max_notify_iterations (128);
|
||||
imp->restart (1);
|
||||
#else
|
||||
imp = new ACE_TP_Reactor ();
|
||||
imp->max_notify_iterations (128);
|
||||
#endif
|
||||
imp = new ACE_Dev_Poll_Reactor ();
|
||||
|
||||
m_Reactor = new ACE_Reactor (imp, 1);
|
||||
}
|
||||
imp->max_notify_iterations (128);
|
||||
imp->restart (1);
|
||||
|
||||
virtual
|
||||
~ReactorRunnable ()
|
||||
{
|
||||
this->Stop ();
|
||||
this->Wait ();
|
||||
#else
|
||||
|
||||
if (m_Reactor)
|
||||
delete m_Reactor;
|
||||
}
|
||||
imp = new ACE_TP_Reactor ();
|
||||
imp->max_notify_iterations (128);
|
||||
|
||||
void
|
||||
Stop ()
|
||||
{
|
||||
m_Reactor->end_reactor_event_loop ();
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
Start ()
|
||||
{
|
||||
if (m_ThreadId != -1)
|
||||
return -1;
|
||||
m_Reactor = new ACE_Reactor (imp, 1);
|
||||
}
|
||||
|
||||
return (m_ThreadId = this->activate ());
|
||||
}
|
||||
virtual ~ReactorRunnable ()
|
||||
{
|
||||
this->Stop ();
|
||||
this->Wait ();
|
||||
|
||||
void
|
||||
Wait ()
|
||||
{
|
||||
ACE_Task_Base::wait ();
|
||||
}
|
||||
if (m_Reactor)
|
||||
delete m_Reactor;
|
||||
}
|
||||
|
||||
long
|
||||
Connections ()
|
||||
{
|
||||
return static_cast<long> (m_Connections.value ());
|
||||
}
|
||||
void Stop ()
|
||||
{
|
||||
m_Reactor->end_reactor_event_loop ();
|
||||
}
|
||||
|
||||
int
|
||||
AddSocket (WorldSocket* sock)
|
||||
{
|
||||
ACE_GUARD_RETURN (ACE_Thread_Mutex, Guard, m_NewSockets_Lock, -1);
|
||||
int Start ()
|
||||
{
|
||||
if (m_ThreadId != -1)
|
||||
return -1;
|
||||
|
||||
++m_Connections;
|
||||
sock->AddReference();
|
||||
sock->reactor (m_Reactor);
|
||||
m_NewSockets.insert (sock);
|
||||
return (m_ThreadId = this->activate ());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
void Wait () { ACE_Task_Base::wait (); }
|
||||
|
||||
ACE_Reactor* GetReactor ()
|
||||
{
|
||||
return m_Reactor;
|
||||
}
|
||||
long Connections ()
|
||||
{
|
||||
return static_cast<long> (m_Connections.value ());
|
||||
}
|
||||
|
||||
protected:
|
||||
int AddSocket (WorldSocket* sock)
|
||||
{
|
||||
ACE_GUARD_RETURN (ACE_Thread_Mutex, Guard, m_NewSockets_Lock, -1);
|
||||
|
||||
void
|
||||
AddNewSockets ()
|
||||
{
|
||||
ACE_GUARD (ACE_Thread_Mutex, Guard, m_NewSockets_Lock);
|
||||
++m_Connections;
|
||||
sock->AddReference();
|
||||
sock->reactor (m_Reactor);
|
||||
m_NewSockets.insert (sock);
|
||||
|
||||
if (m_NewSockets.empty ())
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (SocketSet::iterator i = m_NewSockets.begin (); i != m_NewSockets.end (); ++i)
|
||||
{
|
||||
WorldSocket* sock = (*i);
|
||||
ACE_Reactor* GetReactor ()
|
||||
{
|
||||
return m_Reactor;
|
||||
}
|
||||
|
||||
if (sock->IsClosed ())
|
||||
{
|
||||
sock->RemoveReference ();
|
||||
--m_Connections;
|
||||
}
|
||||
else
|
||||
m_Sockets.insert (sock);
|
||||
}
|
||||
protected:
|
||||
|
||||
m_NewSockets.clear ();
|
||||
}
|
||||
void AddNewSockets ()
|
||||
{
|
||||
ACE_GUARD (ACE_Thread_Mutex, Guard, m_NewSockets_Lock);
|
||||
|
||||
virtual int
|
||||
svc (void)
|
||||
{
|
||||
DEBUG_LOG ("Network Thread Starting");
|
||||
if (m_NewSockets.empty ())
|
||||
return;
|
||||
|
||||
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 ())
|
||||
{
|
||||
// dont be too smart to move this outside the loop
|
||||
// the run_reactor_event_loop will modify interval
|
||||
ACE_Time_Value interval (0, 10000);
|
||||
virtual int svc ()
|
||||
{
|
||||
DEBUG_LOG ("Network Thread Starting");
|
||||
|
||||
if (m_Reactor->run_reactor_event_loop (interval) == -1)
|
||||
break;
|
||||
WorldDatabase.ThreadStart ();
|
||||
|
||||
AddNewSockets ();
|
||||
ACE_ASSERT (m_Reactor);
|
||||
|
||||
for (i = m_Sockets.begin (); i != m_Sockets.end ();)
|
||||
{
|
||||
if ((*i)->Update () == -1)
|
||||
{
|
||||
t = i;
|
||||
i++;
|
||||
(*t)->CloseSocket ();
|
||||
(*t)->RemoveReference ();
|
||||
--m_Connections;
|
||||
m_Sockets.erase (t);
|
||||
}
|
||||
else
|
||||
i++;
|
||||
}
|
||||
}
|
||||
SocketSet::iterator i, t;
|
||||
|
||||
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:
|
||||
typedef ACE_Atomic_Op<ACE_SYNCH_MUTEX, long> AtomicInt;
|
||||
typedef std::set<WorldSocket*> SocketSet;
|
||||
for (i = m_Sockets.begin (); i != m_Sockets.end ();)
|
||||
{
|
||||
if ((*i)->Update () == -1)
|
||||
{
|
||||
t = i;
|
||||
i++;
|
||||
(*t)->CloseSocket ();
|
||||
(*t)->RemoveReference ();
|
||||
--m_Connections;
|
||||
m_Sockets.erase (t);
|
||||
}
|
||||
else
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
ACE_Reactor* m_Reactor;
|
||||
AtomicInt m_Connections;
|
||||
int m_ThreadId;
|
||||
WorldDatabase.ThreadEnd ();
|
||||
|
||||
SocketSet m_Sockets;
|
||||
DEBUG_LOG ("Network Thread Exitting");
|
||||
|
||||
SocketSet m_NewSockets;
|
||||
ACE_Thread_Mutex m_NewSockets_Lock;
|
||||
return 0;
|
||||
}
|
||||
|
||||
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 () :
|
||||
m_NetThreadsCount (0),
|
||||
m_NetThreads (0),
|
||||
m_SockOutKBuff (-1),
|
||||
m_SockOutUBuff (65536),
|
||||
m_UseNoDelay (true),
|
||||
m_Acceptor (0) {}
|
||||
m_NetThreadsCount (0),
|
||||
m_NetThreads (0),
|
||||
m_SockOutKBuff (-1),
|
||||
m_SockOutUBuff (65536),
|
||||
m_UseNoDelay (true),
|
||||
m_Acceptor (0)
|
||||
{
|
||||
}
|
||||
|
||||
WorldSocketMgr::~WorldSocketMgr ()
|
||||
{
|
||||
if (m_NetThreads)
|
||||
delete [] m_NetThreads;
|
||||
if (m_NetThreads)
|
||||
delete [] m_NetThreads;
|
||||
|
||||
if(m_Acceptor)
|
||||
delete m_Acceptor;
|
||||
if(m_Acceptor)
|
||||
delete m_Acceptor;
|
||||
}
|
||||
|
||||
int
|
||||
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");
|
||||
return -1;
|
||||
sLog.outError ("Network.Threads is wrong in your config file");
|
||||
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");
|
||||
return -1;
|
||||
sLog.outError ("Network.OutUBuff is wrong in your config file");
|
||||
return -1;
|
||||
}
|
||||
|
||||
WorldSocket::Acceptor *acc = new WorldSocket::Acceptor;
|
||||
m_Acceptor = acc;
|
||||
WorldSocket::Acceptor *acc = new WorldSocket::Acceptor;
|
||||
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");
|
||||
return -1;
|
||||
sLog.outError ("Failed to open acceptor ,check if the port is free");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
||||
m_NetThreads[i].Start ();
|
||||
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
||||
m_NetThreads[i].Start ();
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
WorldSocketMgr::StartNetwork (ACE_UINT16 port, const char* address)
|
||||
{
|
||||
if (!sLog.IsOutDebug ())
|
||||
ACE_Log_Msg::instance ()->priority_mask (LM_ERROR, ACE_Log_Msg::PROCESS);
|
||||
if (!sLog.IsOutDebug ())
|
||||
ACE_Log_Msg::instance ()->priority_mask (LM_ERROR, ACE_Log_Msg::PROCESS);
|
||||
|
||||
if (this->StartReactiveIO (port, address) == -1)
|
||||
return -1;
|
||||
if (this->StartReactiveIO (port, address) == -1)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
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)
|
||||
acc->close ();
|
||||
if (acc)
|
||||
acc->close ();
|
||||
}
|
||||
|
||||
if (m_NetThreadsCount != 0)
|
||||
if (m_NetThreadsCount != 0)
|
||||
{
|
||||
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
||||
m_NetThreads[i].Stop ();
|
||||
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
||||
m_NetThreads[i].Stop ();
|
||||
}
|
||||
|
||||
this->Wait ();
|
||||
this->Wait ();
|
||||
}
|
||||
|
||||
void
|
||||
WorldSocketMgr::Wait ()
|
||||
{
|
||||
if (m_NetThreadsCount != 0)
|
||||
if (m_NetThreadsCount != 0)
|
||||
{
|
||||
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
||||
m_NetThreads[i].Wait ();
|
||||
for (size_t i = 0; i < m_NetThreadsCount; ++i)
|
||||
m_NetThreads[i].Wait ();
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
WorldSocketMgr::OnSocketOpen (WorldSocket* sock)
|
||||
{
|
||||
// set some options here
|
||||
if (m_SockOutKBuff >= 0)
|
||||
if (sock->peer ().set_option (SOL_SOCKET,
|
||||
SO_SNDBUF,
|
||||
(void*) & m_SockOutKBuff,
|
||||
sizeof (int)) == -1 && errno != ENOTSUP)
|
||||
{
|
||||
sLog.outError ("WorldSocketMgr::OnSocketOpen set_option SO_SNDBUF");
|
||||
return -1;
|
||||
}
|
||||
// set some options here
|
||||
if (m_SockOutKBuff >= 0)
|
||||
{
|
||||
if (sock->peer ().set_option (SOL_SOCKET,
|
||||
SO_SNDBUF,
|
||||
(void*) & m_SockOutKBuff,
|
||||
sizeof (int)) == -1 && errno != ENOTSUP)
|
||||
{
|
||||
sLog.outError ("WorldSocketMgr::OnSocketOpen set_option SO_SNDBUF");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static const int ndoption = 1;
|
||||
static const int ndoption = 1;
|
||||
|
||||
// Set TCP_NODELAY.
|
||||
if (m_UseNoDelay)
|
||||
if (sock->peer ().set_option (ACE_IPPROTO_TCP,
|
||||
TCP_NODELAY,
|
||||
(void*) & ndoption,
|
||||
sizeof (int)) == -1)
|
||||
{
|
||||
sLog.outError ("WorldSocketMgr::OnSocketOpen: peer ().set_option TCP_NODELAY errno = %s", ACE_OS::strerror (errno));
|
||||
return -1;
|
||||
}
|
||||
// Set TCP_NODELAY.
|
||||
if (m_UseNoDelay)
|
||||
{
|
||||
if (sock->peer ().set_option (ACE_IPPROTO_TCP,
|
||||
TCP_NODELAY,
|
||||
(void*)&ndoption,
|
||||
sizeof (int)) == -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
|
||||
size_t min = 1;
|
||||
// we skip the Acceptor Thread
|
||||
size_t min = 1;
|
||||
|
||||
ACE_ASSERT (m_NetThreadsCount >= 1);
|
||||
ACE_ASSERT (m_NetThreadsCount >= 1);
|
||||
|
||||
for (size_t i = 1; i < m_NetThreadsCount; ++i)
|
||||
if (m_NetThreads[i].Connections () < m_NetThreads[min].Connections ())
|
||||
min = i;
|
||||
for (size_t i = 1; i < m_NetThreadsCount; ++i)
|
||||
if (m_NetThreads[i].Connections () < m_NetThreads[min].Connections ())
|
||||
min = i;
|
||||
|
||||
return m_NetThreads[min].AddSocket (sock);
|
||||
return m_NetThreads[min].AddSocket (sock);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
WorldSocketMgr*
|
||||
WorldSocketMgr::Instance ()
|
||||
{
|
||||
return ACE_Singleton<WorldSocketMgr,ACE_Thread_Mutex>::instance();
|
||||
return ACE_Singleton<WorldSocketMgr,ACE_Thread_Mutex>::instance();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue