2007-02-25 23:16:26 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* 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. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
2007-08-19 20:58:57 +00:00
|
|
|
#include <net/serveracceptthread.h>
|
2007-03-13 23:27:17 +00:00
|
|
|
#include <net/servercontext.h>
|
|
|
|
|
#include <net/connectdata.h>
|
2007-08-19 20:58:57 +00:00
|
|
|
#include <net/serverlobbythread.h>
|
2007-02-25 23:16:26 +00:00
|
|
|
#include <net/socket_helper.h>
|
2007-03-13 23:27:17 +00:00
|
|
|
#include <net/serverexception.h>
|
|
|
|
|
#include <net/socket_msg.h>
|
2007-10-08 18:25:40 +00:00
|
|
|
#include <net/socket_startup.h>
|
2007-10-23 21:26:07 +00:00
|
|
|
#include <core/loghelper.h>
|
2007-03-13 23:27:17 +00:00
|
|
|
|
|
|
|
|
#define ACCEPT_TIMEOUT_MSEC 50
|
|
|
|
|
#define NET_SERVER_LISTEN_BACKLOG 5
|
2007-02-25 23:16:26 +00:00
|
|
|
|
2007-06-07 21:19:06 +00:00
|
|
|
using namespace std;
|
2007-02-25 23:16:26 +00:00
|
|
|
|
2007-10-07 15:52:12 +00:00
|
|
|
ServerAcceptThread::ServerAcceptThread(GuiInterface &gui, ConfigFile *config, AvatarManager &avatarManager)
|
2007-05-13 22:25:59 +00:00
|
|
|
: m_gui(gui)
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
m_context.reset(new ServerContext);
|
2007-10-07 15:52:12 +00:00
|
|
|
m_lobbyThread.reset(new ServerLobbyThread(gui, config, avatarManager));
|
2007-02-25 23:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-15 13:36:29 +00:00
|
|
|
ServerAcceptThread::~ServerAcceptThread()
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-08-20 15:30:22 +00:00
|
|
|
ServerAcceptThread::Init(unsigned serverPort, bool ipv6, bool sctp, const std::string &pwd)
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
|
|
|
|
if (IsRunning())
|
2007-10-24 23:17:12 +00:00
|
|
|
{
|
|
|
|
|
assert(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2007-03-13 23:27:17 +00:00
|
|
|
|
|
|
|
|
ServerContext &context = GetContext();
|
|
|
|
|
|
2007-06-04 16:49:19 +00:00
|
|
|
context.SetProtocol(sctp ? SOCKET_IPPROTO_SCTP : 0);
|
2007-10-08 18:25:40 +00:00
|
|
|
// If a "dual stack" is available, the pokerth server always uses ipv6.
|
|
|
|
|
// In this case, ipv4 requests will be mapped to ipv6.
|
|
|
|
|
context.SetAddrFamily(socket_has_dual_stack() ? AF_INET6 : (ipv6 ? AF_INET6 : AF_INET));
|
2007-03-13 23:27:17 +00:00
|
|
|
context.SetServerPort(serverPort);
|
2007-04-26 23:07:08 +00:00
|
|
|
|
2007-08-15 13:36:29 +00:00
|
|
|
GetLobbyThread().Init(pwd);
|
2007-02-25 23:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-20 02:20:50 +00:00
|
|
|
ServerCallback &
|
2007-08-15 13:36:29 +00:00
|
|
|
ServerAcceptThread::GetCallback()
|
2007-03-20 02:20:50 +00:00
|
|
|
{
|
2007-05-13 22:25:59 +00:00
|
|
|
return m_gui;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GuiInterface &
|
2007-08-15 13:36:29 +00:00
|
|
|
ServerAcceptThread::GetGui()
|
2007-05-13 22:25:59 +00:00
|
|
|
{
|
|
|
|
|
return m_gui;
|
2007-03-20 02:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
2007-02-25 23:16:26 +00:00
|
|
|
void
|
2007-08-15 13:36:29 +00:00
|
|
|
ServerAcceptThread::Main()
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
try
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
Listen();
|
2007-08-15 13:36:29 +00:00
|
|
|
GetLobbyThread().Run();
|
2007-02-25 23:16:26 +00:00
|
|
|
|
2007-10-23 20:01:56 +00:00
|
|
|
while (!ShouldTerminate() && !GetLobbyThread().Join(0))
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
// The main server thread is simple. It only accepts connections.
|
|
|
|
|
AcceptLoop();
|
|
|
|
|
}
|
2007-10-18 20:36:49 +00:00
|
|
|
} catch (const PokerTHException &e)
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
2007-03-20 02:20:50 +00:00
|
|
|
GetCallback().SignalNetServerError(e.GetErrorId(), e.GetOsErrorCode());
|
2007-10-23 21:26:07 +00:00
|
|
|
LOG_ERROR(e.what());
|
2007-02-25 23:16:26 +00:00
|
|
|
}
|
2007-08-15 13:36:29 +00:00
|
|
|
GetLobbyThread().SignalTermination();
|
|
|
|
|
GetLobbyThread().Join(LOBBY_THREAD_TERMINATE_TIMEOUT);
|
2007-02-25 23:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
void
|
2007-08-15 13:36:29 +00:00
|
|
|
ServerAcceptThread::Listen()
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
ServerContext &context = GetContext();
|
|
|
|
|
|
2007-06-04 17:04:03 +00:00
|
|
|
if (context.GetServerPort() < 1024)
|
2007-10-18 20:36:49 +00:00
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_SOCK_INVALID_PORT, 0);
|
2007-03-13 23:27:17 +00:00
|
|
|
|
2007-06-04 16:49:19 +00:00
|
|
|
context.SetSocket(socket(context.GetAddrFamily(), SOCK_STREAM, context.GetProtocol()));
|
2007-03-13 23:27:17 +00:00
|
|
|
if (!IS_VALID_SOCKET(context.GetSocket()))
|
2007-10-18 20:36:49 +00:00
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
2007-03-13 23:27:17 +00:00
|
|
|
|
|
|
|
|
unsigned long mode = 1;
|
|
|
|
|
if (IOCTLSOCKET(context.GetSocket(), FIONBIO, &mode) == SOCKET_ERROR)
|
2007-10-18 20:36:49 +00:00
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
2007-03-13 23:27:17 +00:00
|
|
|
|
2007-10-08 18:25:40 +00:00
|
|
|
// The following three calls are optional. If they fail, we don't care.
|
2007-05-25 18:28:05 +00:00
|
|
|
int reuse = 1;
|
|
|
|
|
setsockopt(context.GetSocket(), SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
|
2007-06-07 14:06:22 +00:00
|
|
|
int nodelay = 1;
|
|
|
|
|
setsockopt(context.GetSocket(), SOL_SOCKET, TCP_NODELAY, (char *)&nodelay, sizeof(nodelay));
|
2007-10-08 18:25:40 +00:00
|
|
|
// Enable dual-stack socket on Windows Vista.
|
|
|
|
|
int ipv6only = 0;
|
|
|
|
|
setsockopt(context.GetSocket(), IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
|
2007-05-25 18:28:05 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
context.GetServerSockaddr()->ss_family = context.GetAddrFamily();
|
|
|
|
|
|
2007-05-14 20:48:13 +00:00
|
|
|
const char *localAddr = (context.GetAddrFamily() == AF_INET6) ? "::0" : "0.0.0.0";
|
2007-03-13 23:27:17 +00:00
|
|
|
if (!socket_string_to_addr(
|
2007-05-14 20:48:13 +00:00
|
|
|
localAddr,
|
2007-03-13 23:27:17 +00:00
|
|
|
context.GetAddrFamily(),
|
|
|
|
|
(struct sockaddr *)context.GetServerSockaddr(),
|
|
|
|
|
context.GetServerSockaddrSize()))
|
|
|
|
|
{
|
2007-10-18 20:36:49 +00:00
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_SOCK_SET_ADDR_FAILED, 0);
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
if (!socket_set_port(
|
|
|
|
|
context.GetServerPort(),
|
|
|
|
|
context.GetAddrFamily(),
|
|
|
|
|
(struct sockaddr *)context.GetServerSockaddr(),
|
|
|
|
|
context.GetServerSockaddrSize()))
|
|
|
|
|
{
|
2007-10-18 20:36:49 +00:00
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_SOCK_SET_PORT_FAILED, 0);
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!IS_VALID_BIND(bind(
|
|
|
|
|
context.GetSocket(),
|
|
|
|
|
(const struct sockaddr *)context.GetServerSockaddr(),
|
|
|
|
|
context.GetServerSockaddrSize())))
|
|
|
|
|
{
|
2007-10-18 20:36:49 +00:00
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_SOCK_BIND_FAILED, SOCKET_ERRNO());
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!IS_VALID_LISTEN(listen(context.GetSocket(), NET_SERVER_LISTEN_BACKLOG)))
|
|
|
|
|
{
|
2007-10-18 20:36:49 +00:00
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_SOCK_LISTEN_FAILED, SOCKET_ERRNO());
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-08-15 13:36:29 +00:00
|
|
|
ServerAcceptThread::AcceptLoop()
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
ServerContext &context = GetContext();
|
|
|
|
|
|
|
|
|
|
fd_set readSet;
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
|
|
FD_ZERO(&readSet);
|
|
|
|
|
FD_SET(context.GetSocket(), &readSet);
|
|
|
|
|
|
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
|
timeout.tv_usec = ACCEPT_TIMEOUT_MSEC * 1000;
|
|
|
|
|
int selectResult = select(context.GetSocket() + 1, &readSet, NULL, NULL, &timeout);
|
|
|
|
|
if (!IS_VALID_SELECT(selectResult))
|
|
|
|
|
{
|
2007-10-18 20:36:49 +00:00
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_SOCK_SELECT_FAILED, SOCKET_ERRNO());
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
if (selectResult > 0) // accept is possible
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<ConnectData> tmpData(new ConnectData);
|
2007-03-20 02:20:50 +00:00
|
|
|
tmpData->SetSocket(accept(context.GetSocket(), NULL, NULL));
|
2007-03-13 23:27:17 +00:00
|
|
|
|
|
|
|
|
if (!IS_VALID_SOCKET(tmpData->GetSocket()))
|
|
|
|
|
{
|
2007-10-18 20:36:49 +00:00
|
|
|
throw ServerException(__FILE__, __LINE__, ERR_SOCK_ACCEPT_FAILED, SOCKET_ERRNO());
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
2007-10-18 21:00:11 +00:00
|
|
|
// Optional call - don't check return value.
|
|
|
|
|
// Enable keepalive - won't be of much use but better than nothing.
|
|
|
|
|
int keepalive = 1;
|
|
|
|
|
setsockopt(tmpData->GetSocket(), SOL_SOCKET, SO_KEEPALIVE, (char *)&keepalive, sizeof(keepalive));
|
2007-03-13 23:27:17 +00:00
|
|
|
|
2007-08-15 13:36:29 +00:00
|
|
|
GetLobbyThread().AddConnection(tmpData);
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ServerContext &
|
2007-08-15 13:36:29 +00:00
|
|
|
ServerAcceptThread::GetContext() const
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
assert(m_context.get());
|
|
|
|
|
return *m_context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerContext &
|
2007-08-15 13:36:29 +00:00
|
|
|
ServerAcceptThread::GetContext()
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
assert(m_context.get());
|
|
|
|
|
return *m_context;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-15 13:36:29 +00:00
|
|
|
ServerLobbyThread &
|
|
|
|
|
ServerAcceptThread::GetLobbyThread()
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
2007-08-15 13:36:29 +00:00
|
|
|
assert(m_lobbyThread.get());
|
|
|
|
|
return *m_lobbyThread;
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|