Use server manager to allow SCTP and TCP at the same time.
This commit is contained in:
@@ -35,11 +35,10 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
ServerAcceptThread::ServerAcceptThread(GuiInterface &gui, ConfigFile *config, AvatarManager &avatarManager)
|
||||
: m_gui(gui)
|
||||
ServerAcceptThread::ServerAcceptThread(ServerCallback &serverCallback)
|
||||
: m_serverCallback(serverCallback)
|
||||
{
|
||||
m_context.reset(new ServerContext);
|
||||
m_lobbyThread.reset(new ServerLobbyThread(gui, config, avatarManager));
|
||||
}
|
||||
|
||||
ServerAcceptThread::~ServerAcceptThread()
|
||||
@@ -47,7 +46,7 @@ ServerAcceptThread::~ServerAcceptThread()
|
||||
}
|
||||
|
||||
void
|
||||
ServerAcceptThread::Init(unsigned serverPort, bool ipv6, bool sctp, const string &pwd, const string &logDir, boost::shared_ptr<IrcThread> ircThread)
|
||||
ServerAcceptThread::Init(unsigned serverPort, bool ipv6, bool sctp, const string &pwd, const string &logDir, boost::shared_ptr<ServerLobbyThread> lobbyThread)
|
||||
{
|
||||
if (IsRunning())
|
||||
{
|
||||
@@ -63,82 +62,14 @@ ServerAcceptThread::Init(unsigned serverPort, bool ipv6, bool sctp, const string
|
||||
context.SetAddrFamily(socket_has_dual_stack() ? AF_INET6 : (ipv6 ? AF_INET6 : AF_INET));
|
||||
context.SetServerPort(serverPort);
|
||||
|
||||
m_lobbyThread = lobbyThread;
|
||||
GetLobbyThread().Init(pwd, logDir);
|
||||
m_ircThread = ircThread;
|
||||
}
|
||||
|
||||
ServerCallback &
|
||||
ServerAcceptThread::GetCallback()
|
||||
{
|
||||
return m_gui;
|
||||
}
|
||||
|
||||
GuiInterface &
|
||||
ServerAcceptThread::GetGui()
|
||||
{
|
||||
return m_gui;
|
||||
}
|
||||
|
||||
void
|
||||
ServerAcceptThread::SignalIrcConnect(const std::string &server)
|
||||
{
|
||||
LOG_MSG("Connected to IRC server " << server << ".");
|
||||
}
|
||||
|
||||
void
|
||||
ServerAcceptThread::SignalIrcSelfJoined(const std::string &nickName, const std::string &channel)
|
||||
{
|
||||
LOG_MSG("Joined IRC channel " << channel << " as user " << nickName << ".");
|
||||
m_ircNick = nickName;
|
||||
}
|
||||
|
||||
void
|
||||
ServerAcceptThread::SignalIrcChatMsg(const std::string &nickName, const std::string &msg)
|
||||
{
|
||||
if (m_ircThread)
|
||||
{
|
||||
istringstream msgStream(msg);
|
||||
string target;
|
||||
msgStream >> target;
|
||||
if (boost::algorithm::iequals(target, m_ircNick + ":"))
|
||||
{
|
||||
string command;
|
||||
msgStream >> command;
|
||||
if (command == "kick")
|
||||
{
|
||||
while (msgStream.peek() == ' ')
|
||||
msgStream.get();
|
||||
string playerName(msgStream.str().substr(msgStream.tellg()));
|
||||
if (!playerName.empty())
|
||||
{
|
||||
if (GetLobbyThread().KickPlayerByName(playerName))
|
||||
m_ircThread->SendChatMessage(nickName + ": Successfully kicked player \"" + playerName + "\" from the server.");
|
||||
else
|
||||
m_ircThread->SendChatMessage(nickName + ": Player \"" + playerName + "\" was not found on the server.");
|
||||
}
|
||||
}
|
||||
else if (command == "stat")
|
||||
{
|
||||
ServerStats tmpStats = GetLobbyThread().GetStats();
|
||||
ostringstream statStream;
|
||||
statStream
|
||||
<< "Players on Server: " << tmpStats.numberOfPlayersOnServer;
|
||||
m_ircThread->SendChatMessage(statStream.str());
|
||||
}
|
||||
else
|
||||
m_ircThread->SendChatMessage(nickName + ": Invalid command \"" + command + "\".");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ServerAcceptThread::SignalIrcError(int errorCode)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
ServerAcceptThread::SignalIrcServerError(int errorCode)
|
||||
{
|
||||
return m_serverCallback;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -147,11 +78,8 @@ ServerAcceptThread::Main()
|
||||
try
|
||||
{
|
||||
Listen();
|
||||
if (m_ircThread)
|
||||
m_ircThread->Run();
|
||||
GetLobbyThread().Run();
|
||||
|
||||
while (!ShouldTerminate() && !GetLobbyThread().Join(0))
|
||||
while (!ShouldTerminate())
|
||||
{
|
||||
// The main server thread is simple. It only accepts connections.
|
||||
AcceptLoop();
|
||||
@@ -161,14 +89,6 @@ ServerAcceptThread::Main()
|
||||
GetCallback().SignalNetServerError(e.GetErrorId(), e.GetOsErrorCode());
|
||||
LOG_ERROR(e.what());
|
||||
}
|
||||
GetLobbyThread().SignalTermination();
|
||||
GetLobbyThread().Join(LOBBY_THREAD_TERMINATE_TIMEOUT_MSEC);
|
||||
|
||||
if (m_ircThread)
|
||||
{
|
||||
m_ircThread->SignalTermination();
|
||||
m_ircThread->Join(ADMIN_IRC_TERMINATE_TIMEOUT_MSEC);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <net/servermanager.h>
|
||||
#include <net/servercontext.h>
|
||||
#include <net/ircthread.h>
|
||||
#include <net/connectdata.h>
|
||||
#include <net/serverlobbythread.h>
|
||||
#include <net/serveracceptthread.h>
|
||||
#include <net/socket_helper.h>
|
||||
#include <net/serverexception.h>
|
||||
#include <net/socket_msg.h>
|
||||
#include <net/socket_startup.h>
|
||||
#include <core/loghelper.h>
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ServerManager::ServerManager(GuiInterface &gui, ConfigFile *config, AvatarManager &avatarManager)
|
||||
: m_gui(gui), m_playerConfig(config), m_avatarManager(avatarManager)
|
||||
{
|
||||
}
|
||||
|
||||
ServerManager::~ServerManager()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
ServerManager::Init(unsigned serverPort, bool ipv6, ServerNetworkMode mode, const string &pwd, const string &logDir, boost::shared_ptr<IrcThread> ircThread)
|
||||
{
|
||||
m_lobbyThread.reset(new ServerLobbyThread(GetGui(), m_playerConfig, m_avatarManager));
|
||||
GetLobbyThread().Init(pwd, logDir);
|
||||
|
||||
if (mode & NETWORK_MODE_TCP)
|
||||
{
|
||||
boost::shared_ptr<ServerAcceptThread> tcpAcceptThread(new ServerAcceptThread(GetGui()));
|
||||
tcpAcceptThread->Init(serverPort, ipv6, false, pwd, logDir, m_lobbyThread);
|
||||
m_acceptThreadPool.push_back(tcpAcceptThread);
|
||||
}
|
||||
if (mode & NETWORK_MODE_SCTP)
|
||||
{
|
||||
boost::shared_ptr<ServerAcceptThread> sctpAcceptThread(new ServerAcceptThread(GetGui()));
|
||||
sctpAcceptThread->Init(serverPort, ipv6, true, pwd, logDir, m_lobbyThread);
|
||||
m_acceptThreadPool.push_back(sctpAcceptThread);
|
||||
}
|
||||
m_ircThread = ircThread;
|
||||
}
|
||||
|
||||
GuiInterface &
|
||||
ServerManager::GetGui()
|
||||
{
|
||||
return m_gui;
|
||||
}
|
||||
|
||||
void
|
||||
ServerManager::SignalIrcConnect(const std::string &server)
|
||||
{
|
||||
LOG_MSG("Connected to IRC server " << server << ".");
|
||||
}
|
||||
|
||||
void
|
||||
ServerManager::SignalIrcSelfJoined(const std::string &nickName, const std::string &channel)
|
||||
{
|
||||
LOG_MSG("Joined IRC channel " << channel << " as user " << nickName << ".");
|
||||
m_ircNick = nickName;
|
||||
}
|
||||
|
||||
void
|
||||
ServerManager::SignalIrcChatMsg(const std::string &nickName, const std::string &msg)
|
||||
{
|
||||
if (m_ircThread)
|
||||
{
|
||||
istringstream msgStream(msg);
|
||||
string target;
|
||||
msgStream >> target;
|
||||
if (boost::algorithm::iequals(target, m_ircNick + ":"))
|
||||
{
|
||||
string command;
|
||||
msgStream >> command;
|
||||
if (command == "kick")
|
||||
{
|
||||
while (msgStream.peek() == ' ')
|
||||
msgStream.get();
|
||||
string playerName(msgStream.str().substr(msgStream.tellg()));
|
||||
if (!playerName.empty())
|
||||
{
|
||||
if (GetLobbyThread().KickPlayerByName(playerName))
|
||||
m_ircThread->SendChatMessage(nickName + ": Successfully kicked player \"" + playerName + "\" from the server.");
|
||||
else
|
||||
m_ircThread->SendChatMessage(nickName + ": Player \"" + playerName + "\" was not found on the server.");
|
||||
}
|
||||
}
|
||||
else if (command == "stat")
|
||||
{
|
||||
ServerStats tmpStats = GetLobbyThread().GetStats();
|
||||
ostringstream statStream;
|
||||
statStream
|
||||
<< "Players on Server: " << tmpStats.numberOfPlayersOnServer;
|
||||
m_ircThread->SendChatMessage(statStream.str());
|
||||
}
|
||||
else
|
||||
m_ircThread->SendChatMessage(nickName + ": Invalid command \"" + command + "\".");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ServerManager::SignalIrcError(int errorCode)
|
||||
{
|
||||
LOG_MSG("IRC error " << errorCode << ".");
|
||||
}
|
||||
|
||||
void
|
||||
ServerManager::SignalIrcServerError(int errorCode)
|
||||
{
|
||||
LOG_MSG("IRC server error " << errorCode << ".");
|
||||
}
|
||||
|
||||
void
|
||||
ServerManager::RunAll()
|
||||
{
|
||||
if (m_ircThread)
|
||||
m_ircThread->Run();
|
||||
GetLobbyThread().Run();
|
||||
for_each(m_acceptThreadPool.begin(), m_acceptThreadPool.end(), boost::mem_fn(&ServerAcceptThread::Run));
|
||||
}
|
||||
|
||||
void
|
||||
ServerManager::SignalTerminationAll()
|
||||
{
|
||||
if (m_ircThread)
|
||||
m_ircThread->SignalTermination();
|
||||
GetLobbyThread().SignalTermination();
|
||||
for_each(m_acceptThreadPool.begin(), m_acceptThreadPool.end(), boost::mem_fn(&ServerAcceptThread::SignalTermination));
|
||||
}
|
||||
|
||||
bool
|
||||
ServerManager::JoinAll(bool wait)
|
||||
{
|
||||
if (m_ircThread)
|
||||
m_ircThread->Join(wait ? NET_ADMIN_IRC_TERMINATE_TIMEOUT_MSEC : 0);
|
||||
bool lobbyThreadTerminated = GetLobbyThread().Join(wait ? NET_LOBBY_THREAD_TERMINATE_TIMEOUT_MSEC : 0);
|
||||
bool allAcceptThreadsTerminated = true;
|
||||
AcceptThreadList::iterator i = m_acceptThreadPool.begin();
|
||||
AcceptThreadList::iterator end = m_acceptThreadPool.end();
|
||||
while (i != end)
|
||||
{
|
||||
if (!(*i)->Join(wait ? NET_ACCEPT_THREAD_TERMINATE_TIMEOUT_MSEC : 0))
|
||||
allAcceptThreadsTerminated = false;
|
||||
++i;
|
||||
}
|
||||
return lobbyThreadTerminated || allAcceptThreadsTerminated;
|
||||
}
|
||||
|
||||
ServerLobbyThread &
|
||||
ServerManager::GetLobbyThread()
|
||||
{
|
||||
assert(m_lobbyThread.get());
|
||||
return *m_lobbyThread;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user