Files
TCPlayerbotCore/src/server/game/Server/WorldSocketMgr.cpp
T

400 lines
9.9 KiB
C++
Raw Normal View History

2008-10-02 16:23:55 -05:00
/*
2008-12-08 16:34:03 -06:00
* Copyright (C) 2005-2008,2007 MaNGOS <http://getmangos.com/>
*
2010-01-16 20:19:18 +03:00
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
*
* 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
2008-12-08 16:34:03 -06:00
* 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
2008-12-08 16:34:03 -06:00
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2009-10-17 15:51:44 -07:00
/** \file WorldSocketMgr.cpp
* \ingroup u2w
* \author Derex <[email protected]>
*/
2009-10-17 15:51:44 -07:00
#include "WorldSocketMgr.h"
2009-10-17 15:51:44 -07:00
#include <ace/ACE.h>
#include <ace/Log_Msg.h>
#include <ace/Reactor.h>
#include <ace/Reactor_Impl.h>
#include <ace/TP_Reactor.h>
#include <ace/Dev_Poll_Reactor.h>
#include <ace/Guard_T.h>
#include <ace/Atomic_Op.h>
#include <ace/os_include/arpa/os_inet.h>
#include <ace/os_include/netinet/os_tcp.h>
#include <ace/os_include/sys/os_types.h>
#include <ace/os_include/sys/os_socket.h>
2009-10-17 15:51:44 -07:00
#include <set>
2009-10-17 15:51:44 -07:00
#include "Log.h"
2008-10-02 16:23:55 -05:00
#include "Common.h"
#include "Config.h"
#include "DatabaseEnv.h"
2008-10-02 16:23:55 -05:00
#include "WorldSocket.h"
#include "ScriptMgr.h"
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
/**
* 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
{
2008-12-08 16:34:03 -06:00
public:
2009-10-17 15:51:44 -07:00
ReactorRunnable() :
2010-08-23 14:10:24 +02:00
m_Reactor(0),
m_Connections(0),
2010-08-23 14:10:24 +02:00
m_ThreadId(-1)
2008-12-08 16:34:03 -06:00
{
ACE_Reactor_Impl* imp = 0;
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
#if defined (ACE_HAS_EVENT_POLL) || defined (ACE_HAS_DEV_POLL)
2009-10-17 15:51:44 -07:00
imp = new ACE_Dev_Poll_Reactor();
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
imp->max_notify_iterations (128);
imp->restart (1);
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
#else
2009-10-17 15:51:44 -07:00
imp = new ACE_TP_Reactor();
2008-12-08 16:34:03 -06:00
imp->max_notify_iterations (128);
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
#endif
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
m_Reactor = new ACE_Reactor (imp, 1);
}
2009-10-17 15:51:44 -07:00
virtual ~ReactorRunnable()
2008-12-08 16:34:03 -06:00
{
Stop();
Wait();
2009-10-17 15:51:44 -07:00
delete m_Reactor;
2008-12-08 16:34:03 -06:00
}
2009-10-17 15:51:44 -07:00
void Stop()
2008-12-08 16:34:03 -06:00
{
m_Reactor->end_reactor_event_loop();
2008-12-08 16:34:03 -06:00
}
2009-10-17 15:51:44 -07:00
int Start()
2008-12-08 16:34:03 -06:00
{
if (m_ThreadId != -1)
return -1;
2009-10-17 15:51:44 -07:00
return (m_ThreadId = activate());
2008-12-08 16:34:03 -06:00
}
2009-10-17 15:51:44 -07:00
void Wait() { ACE_Task_Base::wait(); }
2009-10-17 15:51:44 -07:00
long Connections()
2008-12-08 16:34:03 -06:00
{
return static_cast<long> (m_Connections.value());
2008-12-08 16:34:03 -06:00
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
int AddSocket (WorldSocket* sock)
{
ACE_GUARD_RETURN (ACE_Thread_Mutex, Guard, m_NewSockets_Lock, -1);
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
++m_Connections;
sock->AddReference();
sock->reactor (m_Reactor);
m_NewSockets.insert (sock);
2009-10-17 15:51:44 -07:00
sScriptMgr.OnSocketOpen(sock);
2008-12-08 16:34:03 -06:00
return 0;
}
2009-10-17 15:51:44 -07:00
ACE_Reactor* GetReactor()
2008-12-08 16:34:03 -06:00
{
return m_Reactor;
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
protected:
2009-10-17 15:51:44 -07:00
void AddNewSockets()
2008-12-08 16:34:03 -06:00
{
ACE_GUARD (ACE_Thread_Mutex, Guard, m_NewSockets_Lock);
2009-10-17 15:51:44 -07:00
if (m_NewSockets.empty())
2008-12-08 16:34:03 -06:00
return;
2009-10-17 15:51:44 -07:00
for (SocketSet::const_iterator i = m_NewSockets.begin(); i != m_NewSockets.end(); ++i)
2008-12-08 16:34:03 -06:00
{
WorldSocket* sock = (*i);
2009-10-17 15:51:44 -07:00
if (sock->IsClosed())
2008-12-08 16:34:03 -06:00
{
sScriptMgr.OnSocketClose(sock, true);
sock->RemoveReference();
2008-12-08 16:34:03 -06:00
--m_Connections;
}
else
m_Sockets.insert (sock);
}
2009-10-17 15:51:44 -07:00
m_NewSockets.clear();
2008-12-08 16:34:03 -06:00
}
2009-10-17 15:51:44 -07:00
virtual int svc()
2008-12-08 16:34:03 -06:00
{
sLog.outStaticDebug ("Network Thread Starting");
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
bool needInit = true;
if (!(sWorld.getConfig(CONFIG_MYSQL_BUNDLE_LOGINDB) & MYSQL_BUNDLE_RA))
{
LoginDatabase.Init_MySQL_Connection();
needInit = false;
}
if (!(sWorld.getConfig(CONFIG_MYSQL_BUNDLE_CHARDB) & MYSQL_BUNDLE_RA))
{
CharacterDatabase.Init_MySQL_Connection();
needInit = false;
}
if (needInit)
MySQL::Thread_Init();
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
ACE_ASSERT (m_Reactor);
2009-10-17 15:51:44 -07:00
SocketSet::iterator i, t;
2009-10-17 15:51:44 -07:00
while (!m_Reactor->reactor_event_loop_done())
2008-12-08 16:34:03 -06:00
{
// dont be too smart to move this outside the loop
// the run_reactor_event_loop will modify interval
ACE_Time_Value interval (0, 10000);
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
if (m_Reactor->run_reactor_event_loop (interval) == -1)
break;
2009-10-17 15:51:44 -07:00
AddNewSockets();
2009-10-17 15:51:44 -07:00
for (i = m_Sockets.begin(); i != m_Sockets.end();)
2008-12-08 16:34:03 -06:00
{
if ((*i)->Update() == -1)
2008-12-08 16:34:03 -06:00
{
t = i;
++i;
(*t)->CloseSocket();
sScriptMgr.OnSocketClose((*t), false);
(*t)->RemoveReference();
2008-12-08 16:34:03 -06:00
--m_Connections;
m_Sockets.erase (t);
}
else
++i;
2008-12-08 16:34:03 -06:00
}
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
///- Free MySQL thread resources and deallocate lingering connections
if (!(sWorld.getConfig(CONFIG_MYSQL_BUNDLE_LOGINDB) & MYSQL_BUNDLE_RA))
LoginDatabase.End_MySQL_Connection();
if (!(sWorld.getConfig(CONFIG_MYSQL_BUNDLE_CHARDB) & MYSQL_BUNDLE_RA))
CharacterDatabase.End_MySQL_Connection();
if (needInit)
MySQL::Thread_End();
2009-10-17 15:51:44 -07:00
sLog.outStaticDebug ("Network Thread Exitting");
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
return 0;
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
private:
typedef ACE_Atomic_Op<ACE_SYNCH_MUTEX, long> AtomicInt;
typedef std::set<WorldSocket*> SocketSet;
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
ACE_Reactor* m_Reactor;
AtomicInt m_Connections;
int m_ThreadId;
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
SocketSet m_Sockets;
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
SocketSet m_NewSockets;
ACE_Thread_Mutex m_NewSockets_Lock;
};
2009-10-17 15:51:44 -07:00
WorldSocketMgr::WorldSocketMgr() :
m_NetThreads(0),
2010-08-21 20:08:47 +02:00
m_NetThreadsCount(0),
m_SockOutKBuff(-1),
m_SockOutUBuff(65536),
m_UseNoDelay(true),
2008-12-08 16:34:03 -06:00
m_Acceptor (0)
{
}
2009-10-17 15:51:44 -07:00
WorldSocketMgr::~WorldSocketMgr()
2008-10-02 16:23:55 -05:00
{
delete [] m_NetThreads;
delete m_Acceptor;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
int
WorldSocketMgr::StartReactiveIO (ACE_UINT16 port, const char* address)
2008-10-02 16:23:55 -05:00
{
2008-12-08 16:34:03 -06:00
m_UseNoDelay = sConfig.GetBoolDefault ("Network.TcpNodelay", true);
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
int num_threads = sConfig.GetIntDefault ("Network.Threads", 1);
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
if (num_threads <= 0)
{
sLog.outError ("Network.Threads is wrong in your config file");
return -1;
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
m_NetThreadsCount = static_cast<size_t> (num_threads + 1);
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
m_NetThreads = new ReactorRunnable[m_NetThreadsCount];
2009-10-17 15:51:44 -07:00
sLog.outBasic ("Max allowed socket connections %d", ACE::max_handles());
2008-12-08 16:34:03 -06:00
// -1 means use default
m_SockOutKBuff = sConfig.GetIntDefault ("Network.OutKBuff", -1);
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
m_SockOutUBuff = sConfig.GetIntDefault ("Network.OutUBuff", 65536);
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
if (m_SockOutUBuff <= 0)
2008-12-08 16:34:03 -06:00
{
sLog.outError ("Network.OutUBuff is wrong in your config file");
return -1;
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
WorldSocket::Acceptor *acc = new WorldSocket::Acceptor;
m_Acceptor = acc;
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
ACE_INET_Addr listen_addr (port, address);
2009-10-17 15:51:44 -07:00
if (acc->open(listen_addr, m_NetThreads[0].GetReactor(), ACE_NONBLOCK) == -1)
2008-12-08 16:34:03 -06:00
{
sLog.outError ("Failed to open acceptor ,check if the port is free");
return -1;
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
for (size_t i = 0; i < m_NetThreadsCount; ++i)
m_NetThreads[i].Start();
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
return 0;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
int
WorldSocketMgr::StartNetwork (ACE_UINT16 port, const char* address)
2008-10-02 16:23:55 -05:00
{
if (!sLog.IsOutDebug())
ACE_Log_Msg::instance()->priority_mask (LM_ERROR, ACE_Log_Msg::PROCESS);
2009-10-17 15:51:44 -07:00
if (StartReactiveIO(port, address) == -1)
2008-12-08 16:34:03 -06:00
return -1;
2009-10-17 15:51:44 -07:00
sScriptMgr.OnNetworkStart();
2008-12-08 16:34:03 -06:00
return 0;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
void
WorldSocketMgr::StopNetwork()
2008-10-02 16:23:55 -05:00
{
2008-12-08 16:34:03 -06:00
if (m_Acceptor)
{
WorldSocket::Acceptor* acc = dynamic_cast<WorldSocket::Acceptor*> (m_Acceptor);
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
if (acc)
acc->close();
2008-12-08 16:34:03 -06:00
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
if (m_NetThreadsCount != 0)
{
for (size_t i = 0; i < m_NetThreadsCount; ++i)
m_NetThreads[i].Stop();
2008-12-08 16:34:03 -06:00
}
2009-10-17 15:51:44 -07:00
Wait();
sScriptMgr.OnNetworkStop();
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
void
WorldSocketMgr::Wait()
{
2008-12-08 16:34:03 -06:00
if (m_NetThreadsCount != 0)
{
for (size_t i = 0; i < m_NetThreadsCount; ++i)
m_NetThreads[i].Wait();
2008-12-08 16:34:03 -06:00
}
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
int
WorldSocketMgr::OnSocketOpen (WorldSocket* sock)
{
2008-12-08 16:34:03 -06:00
// set some options here
if (m_SockOutKBuff >= 0)
{
if (sock->peer().set_option (SOL_SOCKET,
2008-12-08 16:34:03 -06:00
SO_SNDBUF,
(void*) & m_SockOutKBuff,
sizeof (int)) == -1 && errno != ENOTSUP)
{
sLog.outError ("WorldSocketMgr::OnSocketOpen set_option SO_SNDBUF");
return -1;
}
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
static const int ndoption = 1;
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
// Set TCP_NODELAY.
if (m_UseNoDelay)
{
if (sock->peer().set_option (ACE_IPPROTO_TCP,
2008-12-08 16:34:03 -06:00
TCP_NODELAY,
(void*)&ndoption,
sizeof (int)) == -1)
{
sLog.outError ("WorldSocketMgr::OnSocketOpen: peer().set_option TCP_NODELAY errno = %s", ACE_OS::strerror (errno));
2008-12-08 16:34:03 -06:00
return -1;
}
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
sock->m_OutBufferSize = static_cast<size_t> (m_SockOutUBuff);
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
// we skip the Acceptor Thread
size_t min = 1;
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
ACE_ASSERT (m_NetThreadsCount >= 1);
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
for (size_t i = 1; i < m_NetThreadsCount; ++i)
if (m_NetThreads[i].Connections() < m_NetThreads[min].Connections())
2008-12-08 16:34:03 -06:00
min = i;
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
return m_NetThreads[min].AddSocket (sock);
}
2009-10-17 15:51:44 -07:00
2008-12-08 16:34:03 -06:00
WorldSocketMgr*
WorldSocketMgr::Instance()
{
2008-12-08 16:34:03 -06:00
return ACE_Singleton<WorldSocketMgr,ACE_Thread_Mutex>::instance();
2008-10-02 16:23:55 -05:00
}
2009-02-17 20:07:49 -06:00