2007-03-13 23:27:17 +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/serverlobbythread.h>
|
2007-03-13 23:27:17 +00:00
|
|
|
#include <net/serverexception.h>
|
|
|
|
|
#include <net/senderthread.h>
|
|
|
|
|
#include <net/sendercallback.h>
|
|
|
|
|
#include <net/receiverhelper.h>
|
|
|
|
|
#include <net/socket_msg.h>
|
2007-08-15 13:28:28 +00:00
|
|
|
#include <core/rand.h>
|
2007-04-26 23:07:08 +00:00
|
|
|
|
2007-05-08 06:45:46 +00:00
|
|
|
#include <boost/lambda/lambda.hpp>
|
|
|
|
|
|
2007-05-06 23:27:08 +00:00
|
|
|
#define SERVER_CLOSE_SESSION_DELAY_SEC 10
|
2007-08-15 13:28:28 +00:00
|
|
|
#define SERVER_MAX_NUM_SESSIONS 64 // Maximum number of idle users in lobby.
|
|
|
|
|
|
|
|
|
|
#define SERVER_COMPUTER_PLAYER_NAME "Computer"
|
2007-05-06 23:27:08 +00:00
|
|
|
|
2007-04-26 23:07:08 +00:00
|
|
|
using namespace std;
|
2007-03-13 23:27:17 +00:00
|
|
|
|
2007-05-12 22:00:59 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
class ServerSenderCallback : public SenderCallback
|
|
|
|
|
{
|
|
|
|
|
public:
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerSenderCallback(ServerLobbyThread &server) : m_server(server) {}
|
2007-03-13 23:27:17 +00:00
|
|
|
virtual ~ServerSenderCallback() {}
|
|
|
|
|
|
|
|
|
|
virtual void SignalNetError(SOCKET sock, int errorID, int osErrorID)
|
|
|
|
|
{
|
2007-05-05 21:14:23 +00:00
|
|
|
// We just ignore send errors for now, on server side.
|
2007-08-15 13:28:28 +00:00
|
|
|
// A serious send error should trigger a read error or a read
|
2007-05-05 21:14:23 +00:00
|
|
|
// returning 0 afterwards, and we will handle this error.
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread &m_server;
|
2007-03-13 23:27:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::ServerLobbyThread(GuiInterface &gui, ConfigFile *playerConfig)
|
|
|
|
|
: m_gui(gui), m_playerConfig(playerConfig)
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
m_senderCallback.reset(new ServerSenderCallback(*this));
|
2007-03-20 11:56:30 +00:00
|
|
|
m_sender.reset(new SenderThread(GetSenderCallback()));
|
|
|
|
|
m_receiver.reset(new ReceiverHelper);
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::~ServerLobbyThread()
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
2007-03-20 11:56:30 +00:00
|
|
|
CleanupConnectQueue();
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
2007-04-26 23:07:08 +00:00
|
|
|
void
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::Init(const string &pwd)
|
2007-04-26 23:07:08 +00:00
|
|
|
{
|
|
|
|
|
m_password = pwd;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
void
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::AddConnection(boost::shared_ptr<ConnectData> data)
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_connectQueueMutex);
|
|
|
|
|
m_connectQueue.push_back(data);
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 15:30:22 +00:00
|
|
|
void
|
|
|
|
|
ServerLobbyThread::CloseSessionDelayed(SessionWrapper session)
|
|
|
|
|
{
|
|
|
|
|
m_sessionManager.RemoveSession(session.sessionData->GetSocket());
|
|
|
|
|
|
|
|
|
|
boost::microsec_timer closeTimer;
|
|
|
|
|
closeTimer.start();
|
|
|
|
|
CloseSessionList::value_type closeSessionData(closeTimer, session.sessionData);
|
|
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_closeSessionListMutex);
|
|
|
|
|
m_closeSessionList.push_back(closeSessionData);
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
u_int32_t
|
|
|
|
|
ServerLobbyThread::GetNextUniquePlayerId()
|
2007-03-20 14:05:16 +00:00
|
|
|
{
|
2007-08-15 13:28:28 +00:00
|
|
|
return m_curUniquePlayerId++;
|
2007-03-20 14:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
void
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::Main()
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
GetSender().Run();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
while (!ShouldTerminate())
|
|
|
|
|
{
|
|
|
|
|
{
|
2007-03-20 14:05:16 +00:00
|
|
|
// Handle one incoming connection at a time.
|
2007-03-13 23:27:17 +00:00
|
|
|
boost::shared_ptr<ConnectData> tmpData;
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_connectQueueMutex);
|
|
|
|
|
if (!m_connectQueue.empty())
|
|
|
|
|
{
|
|
|
|
|
tmpData = m_connectQueue.front();
|
|
|
|
|
m_connectQueue.pop_front();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (tmpData.get())
|
2007-08-15 13:28:28 +00:00
|
|
|
HandleNewConnection(tmpData);
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
2007-08-15 13:28:28 +00:00
|
|
|
// Process loop.
|
|
|
|
|
ProcessLoop();
|
2007-05-06 23:27:08 +00:00
|
|
|
// Close sessions.
|
|
|
|
|
CloseSessionLoop();
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
2007-06-10 23:28:25 +00:00
|
|
|
} catch (const NetException &e)
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
2007-06-10 23:28:25 +00:00
|
|
|
GetCallback().SignalNetServerError(e.GetErrorId(), e.GetOsErrorCode());
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
GetSender().SignalTermination();
|
|
|
|
|
GetSender().Join(SENDER_THREAD_TERMINATE_TIMEOUT);
|
2007-03-20 02:20:50 +00:00
|
|
|
|
2007-03-20 11:56:30 +00:00
|
|
|
CleanupConnectQueue();
|
2007-08-15 13:28:28 +00:00
|
|
|
m_sessionManager.Clear();
|
2007-03-20 02:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-20 14:05:16 +00:00
|
|
|
void
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::ProcessLoop()
|
2007-03-20 14:05:16 +00:00
|
|
|
{
|
2007-08-15 13:28:28 +00:00
|
|
|
// Wait for data.
|
|
|
|
|
SessionWrapper session = m_sessionManager.Select(RECV_TIMEOUT_MSEC);
|
2007-03-20 14:05:16 +00:00
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
if (session.sessionData.get())
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> packet;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Receive the next packet.
|
|
|
|
|
packet = GetReceiver().Recv(session.sessionData->GetSocket());
|
|
|
|
|
} catch (const NetException &)
|
|
|
|
|
{
|
|
|
|
|
// On error: Close this session.
|
|
|
|
|
CloseSessionDelayed(session);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (packet.get())
|
|
|
|
|
{
|
|
|
|
|
if (packet->ToNetPacketInit())
|
|
|
|
|
{
|
|
|
|
|
// Session should be in initial state.
|
|
|
|
|
if (session.sessionData->GetState() != SessionData::Init)
|
|
|
|
|
SessionError(session, ERR_SOCK_INVALID_STATE);
|
|
|
|
|
else
|
|
|
|
|
HandleNetPacketInit(session, *packet->ToNetPacketInit());
|
|
|
|
|
}
|
|
|
|
|
// Session should be established.
|
|
|
|
|
else if (session.sessionData->GetState() != SessionData::Established)
|
|
|
|
|
SessionError(session, ERR_SOCK_INVALID_STATE);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (packet->ToNetPacketCreateGame())
|
|
|
|
|
HandleNetPacketCreateGame(session, *packet->ToNetPacketCreateGame());
|
|
|
|
|
else if (packet->ToNetPacketJoinGame())
|
|
|
|
|
HandleNetPacketJoinGame(session, *packet->ToNetPacketJoinGame());
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-03-20 14:05:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-06 23:27:08 +00:00
|
|
|
void
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::HandleNetPacketInit(SessionWrapper session, const NetPacketInit &tmpPacket)
|
|
|
|
|
{
|
|
|
|
|
NetPacketInit::Data initData;
|
|
|
|
|
tmpPacket.GetData(initData);
|
|
|
|
|
|
|
|
|
|
// Check the protocol version.
|
|
|
|
|
if (initData.versionMajor != NET_VERSION_MAJOR)
|
|
|
|
|
{
|
|
|
|
|
SessionError(session, ERR_NET_VERSION_NOT_SUPPORTED);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check the server password.
|
|
|
|
|
if (!CheckPassword(initData.password))
|
|
|
|
|
{
|
|
|
|
|
SessionError(session, ERR_NET_INVALID_PASSWORD);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check whether the player name is correct.
|
|
|
|
|
// Partly, this is also done in netpacket.
|
|
|
|
|
// However, some disallowed names are checked only here.
|
|
|
|
|
if (initData.playerName.empty() || initData.playerName.size() > MAX_NAME_SIZE
|
|
|
|
|
|| initData.playerName.substr(0, sizeof(SERVER_COMPUTER_PLAYER_NAME) - 1) == SERVER_COMPUTER_PLAYER_NAME)
|
|
|
|
|
{
|
|
|
|
|
SessionError(session, ERR_NET_INVALID_PLAYER_NAME);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check whether this player is already connected.
|
2007-08-20 15:30:22 +00:00
|
|
|
if (m_sessionManager.IsPlayerConnected(initData.playerName))
|
2007-08-15 13:28:28 +00:00
|
|
|
{
|
|
|
|
|
SessionError(session, ERR_NET_PLAYER_NAME_IN_USE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create player data object.
|
|
|
|
|
boost::shared_ptr<PlayerData> tmpPlayerData(
|
|
|
|
|
new PlayerData(GetNextUniquePlayerId(), 0, PLAYER_TYPE_HUMAN, PLAYER_RIGHTS_NORMAL));
|
|
|
|
|
tmpPlayerData->SetName(initData.playerName);
|
|
|
|
|
tmpPlayerData->SetNetSessionData(session.sessionData);
|
|
|
|
|
|
|
|
|
|
// Send ACK to client.
|
|
|
|
|
boost::shared_ptr<NetPacket> initAck(new NetPacketInitAck);
|
|
|
|
|
NetPacketInitAck::Data initAckData;
|
|
|
|
|
initAckData.sessionId = session.sessionData->GetId(); // TODO: currently unused.
|
|
|
|
|
initAckData.playerId = tmpPlayerData->GetUniqueId();
|
|
|
|
|
static_cast<NetPacketInitAck *>(initAck.get())->SetData(initAckData);
|
|
|
|
|
GetSender().Send(session.sessionData->GetSocket(), initAck);
|
|
|
|
|
|
|
|
|
|
// Send the game list to the client.
|
|
|
|
|
/*GameThreadList::iterator game_i = m_gameList.begin();
|
|
|
|
|
GameThreadList::iterator game_end = m_gameList.end();
|
|
|
|
|
while (game_i != game_end)
|
|
|
|
|
{
|
|
|
|
|
GetSender().Send(session.sessionData->GetSocket(), CreateNetPacketGameListUpdate(*(*game_i)));
|
|
|
|
|
++game_i;
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
// Set player data for session.
|
|
|
|
|
m_sessionManager.SetSessionPlayerData(session.sessionData->GetSocket(), tmpPlayerData);
|
|
|
|
|
|
|
|
|
|
// Session is now established.
|
|
|
|
|
session.sessionData->SetState(SessionData::Established);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServerLobbyThread::HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServerLobbyThread::HandleNetPacketJoinGame(SessionWrapper session, const NetPacketJoinGame &tmpPacket)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServerLobbyThread::CloseSessionLoop()
|
2007-05-06 23:27:08 +00:00
|
|
|
{
|
2007-08-20 15:30:22 +00:00
|
|
|
boost::mutex::scoped_lock lock(m_closeSessionListMutex);
|
|
|
|
|
|
2007-05-06 23:27:08 +00:00
|
|
|
CloseSessionList::iterator i = m_closeSessionList.begin();
|
|
|
|
|
CloseSessionList::iterator end = m_closeSessionList.end();
|
|
|
|
|
|
|
|
|
|
while (i != end)
|
|
|
|
|
{
|
|
|
|
|
CloseSessionList::iterator cur = i++;
|
|
|
|
|
|
2007-06-14 20:29:59 +00:00
|
|
|
if (cur->first.elapsed().total_seconds() >= SERVER_CLOSE_SESSION_DELAY_SEC)
|
2007-05-06 23:27:08 +00:00
|
|
|
m_closeSessionList.erase(cur);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
void
|
|
|
|
|
ServerLobbyThread::HandleNewConnection(boost::shared_ptr<ConnectData> connData)
|
2007-03-20 02:20:50 +00:00
|
|
|
{
|
2007-08-15 13:28:28 +00:00
|
|
|
if (m_sessionManager.GetRawSessionCount() <= SERVER_MAX_NUM_SESSIONS)
|
2007-03-20 11:56:30 +00:00
|
|
|
{
|
2007-08-15 13:28:28 +00:00
|
|
|
// Create a random session id.
|
|
|
|
|
// This id can be used to reconnect to the server if the connection was lost.
|
|
|
|
|
unsigned sessionId;
|
|
|
|
|
RandomBytes((unsigned char *)&sessionId, sizeof(sessionId)); // TODO: check for collisions.
|
2007-03-20 11:56:30 +00:00
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
// Create a new session.
|
|
|
|
|
boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), sessionId));
|
|
|
|
|
m_sessionManager.AddSession(sessionData);
|
2007-03-20 02:20:50 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2007-08-15 13:28:28 +00:00
|
|
|
// Server is full.
|
|
|
|
|
// Create a generic session with Id 0.
|
|
|
|
|
boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), 0));
|
|
|
|
|
// Gracefully close this session.
|
|
|
|
|
SessionError(SessionWrapper(sessionData, boost::shared_ptr<PlayerData>()), ERR_NET_SERVER_FULL);
|
2007-03-20 02:20:50 +00:00
|
|
|
}
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-20 11:56:30 +00:00
|
|
|
void
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::CleanupConnectQueue()
|
2007-03-20 11:56:30 +00:00
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_connectQueueMutex);
|
|
|
|
|
|
|
|
|
|
// Sockets will be closed automatically.
|
|
|
|
|
m_connectQueue.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::SessionError(SessionWrapper session, int errorCode)
|
2007-05-05 21:40:24 +00:00
|
|
|
{
|
2007-08-02 20:52:13 +00:00
|
|
|
if (session.sessionData.get())
|
|
|
|
|
{
|
|
|
|
|
SendError(session.sessionData->GetSocket(), errorCode);
|
|
|
|
|
CloseSessionDelayed(session);
|
|
|
|
|
}
|
2007-05-08 06:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-23 08:57:34 +00:00
|
|
|
void
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::SendError(SOCKET s, int errorCode)
|
2007-05-08 06:45:46 +00:00
|
|
|
{
|
|
|
|
|
boost::shared_ptr<NetPacket> packet(new NetPacketError);
|
|
|
|
|
NetPacketError::Data errorData;
|
|
|
|
|
errorData.errorCode = errorCode;
|
|
|
|
|
static_cast<NetPacketError *>(packet.get())->SetData(errorData);
|
|
|
|
|
GetSender().Send(s, packet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerCallback &
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::GetCallback()
|
2007-05-08 06:45:46 +00:00
|
|
|
{
|
2007-05-13 22:25:59 +00:00
|
|
|
return m_gui;
|
2007-05-08 06:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
SenderThread &
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::GetSender()
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
assert(m_sender.get());
|
|
|
|
|
return *m_sender;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReceiverHelper &
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::GetReceiver()
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
assert(m_receiver.get());
|
|
|
|
|
return *m_receiver;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-26 23:07:08 +00:00
|
|
|
bool
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::CheckPassword(const string &password) const
|
2007-04-26 23:07:08 +00:00
|
|
|
{
|
|
|
|
|
return (password == m_password);
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
ServerSenderCallback &
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::GetSenderCallback()
|
2007-03-13 23:27:17 +00:00
|
|
|
{
|
|
|
|
|
assert(m_senderCallback.get());
|
|
|
|
|
return *m_senderCallback;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-13 22:25:59 +00:00
|
|
|
GuiInterface &
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread::GetGui()
|
2007-05-13 22:25:59 +00:00
|
|
|
{
|
|
|
|
|
return m_gui;
|
|
|
|
|
}
|
|
|
|
|
|