2007-02-16 00:03:30 +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. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <net/clientthread.h>
|
2007-02-17 23:44:45 +00:00
|
|
|
#include <net/clientstate.h>
|
2007-03-13 23:27:17 +00:00
|
|
|
#include <net/clientcontext.h>
|
2007-02-25 23:16:26 +00:00
|
|
|
#include <net/senderthread.h>
|
2007-03-13 02:23:12 +00:00
|
|
|
#include <net/receiverhelper.h>
|
2007-02-18 11:26:57 +00:00
|
|
|
#include <net/clientexception.h>
|
|
|
|
|
#include <net/socket_msg.h>
|
2007-04-27 21:35:18 +00:00
|
|
|
#include <gamedata.h>
|
2007-02-17 23:44:45 +00:00
|
|
|
|
|
|
|
|
#include <cassert>
|
2007-02-16 00:03:30 +00:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
class ClientSenderCallback : public SenderCallback
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ClientSenderCallback(ClientThread &client) : m_client(client) {}
|
|
|
|
|
virtual ~ClientSenderCallback() {}
|
|
|
|
|
|
|
|
|
|
virtual void SignalNetError(SOCKET sock, int errorID, int osErrorID)
|
|
|
|
|
{
|
|
|
|
|
// For now, we ignore the socket.
|
|
|
|
|
// Just signal the error.
|
|
|
|
|
// We assume that the client thread will be terminated.
|
2007-03-20 02:20:50 +00:00
|
|
|
m_client.GetCallback().SignalNetClientError(errorID, osErrorID);
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ClientThread &m_client;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2007-02-18 01:16:18 +00:00
|
|
|
ClientThread::ClientThread(ClientCallback &cb)
|
|
|
|
|
: m_curState(NULL), m_callback(cb)
|
2007-02-16 00:03:30 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
m_context.reset(new ClientContext);
|
|
|
|
|
m_senderCallback.reset(new ClientSenderCallback(*this));
|
2007-03-20 11:56:30 +00:00
|
|
|
m_sender.reset(new SenderThread(GetSenderCallback()));
|
|
|
|
|
m_receiver.reset(new ReceiverHelper);
|
2007-04-27 21:35:18 +00:00
|
|
|
m_gameData.reset(new GameData);
|
2007-02-16 00:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClientThread::~ClientThread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-04-08 16:18:20 +00:00
|
|
|
ClientThread::Init(
|
|
|
|
|
const string &serverAddress, unsigned serverPort, bool ipv6, const string &pwd, const string &playerName)
|
2007-02-16 00:03:30 +00:00
|
|
|
{
|
|
|
|
|
if (IsRunning())
|
|
|
|
|
return; // TODO: throw exception
|
2007-02-18 11:26:57 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
ClientContext &context = GetContext();
|
2007-02-18 11:26:57 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
context.SetAddrFamily(ipv6 ? AF_INET6 : AF_INET);
|
|
|
|
|
context.SetServerAddr(serverAddress);
|
|
|
|
|
context.SetServerPort(serverPort);
|
|
|
|
|
context.SetPassword(pwd);
|
2007-04-08 16:18:20 +00:00
|
|
|
context.SetPlayerName(playerName);
|
2007-03-13 23:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClientCallback &
|
|
|
|
|
ClientThread::GetCallback()
|
|
|
|
|
{
|
|
|
|
|
return m_callback;
|
2007-02-16 00:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::Main()
|
|
|
|
|
{
|
2007-02-18 11:26:57 +00:00
|
|
|
SetState(CLIENT_INITIAL_STATE::Instance());
|
2007-03-13 23:27:17 +00:00
|
|
|
|
|
|
|
|
GetSender().Run();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2007-02-18 11:26:57 +00:00
|
|
|
while (!ShouldTerminate())
|
|
|
|
|
{
|
|
|
|
|
int msg = GetState().Process(*this);
|
|
|
|
|
if (msg != MSG_SOCK_INTERNAL_PENDING)
|
2007-03-20 02:20:50 +00:00
|
|
|
{
|
|
|
|
|
if (msg <= MSG_SOCK_LIMIT_CONNECT)
|
|
|
|
|
GetCallback().SignalNetClientConnect(msg);
|
|
|
|
|
else
|
|
|
|
|
GetCallback().SignalNetClientGameInfo(msg);
|
2007-04-27 21:35:18 +00:00
|
|
|
|
|
|
|
|
// Additionally signal the start of the game.
|
2007-04-28 13:07:06 +00:00
|
|
|
if (msg == MSG_NET_GAME_START)
|
2007-04-27 21:35:18 +00:00
|
|
|
GetCallback().SignalNetClientGameStart(GetGameData());
|
2007-03-20 02:20:50 +00:00
|
|
|
}
|
2007-02-18 11:26:57 +00:00
|
|
|
}
|
2007-03-13 02:23:12 +00:00
|
|
|
} catch (const NetException &e)
|
2007-02-16 00:03:30 +00:00
|
|
|
{
|
2007-03-20 02:20:50 +00:00
|
|
|
GetCallback().SignalNetClientError(e.GetErrorId(), e.GetOsErrorCode());
|
2007-02-16 00:03:30 +00:00
|
|
|
}
|
2007-02-25 23:16:26 +00:00
|
|
|
GetSender().SignalTermination();
|
2007-03-13 23:27:17 +00:00
|
|
|
GetSender().Join(SENDER_THREAD_TERMINATE_TIMEOUT);
|
2007-02-16 00:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
const ClientContext &
|
|
|
|
|
ClientThread::GetContext() const
|
2007-02-17 23:44:45 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
assert(m_context.get());
|
|
|
|
|
return *m_context;
|
2007-02-17 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
ClientContext &
|
|
|
|
|
ClientThread::GetContext()
|
2007-02-17 23:44:45 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
assert(m_context.get());
|
|
|
|
|
return *m_context;
|
2007-02-17 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
2007-02-18 00:10:26 +00:00
|
|
|
ClientState &
|
|
|
|
|
ClientThread::GetState()
|
|
|
|
|
{
|
|
|
|
|
assert(m_curState);
|
|
|
|
|
return *m_curState;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-17 23:44:45 +00:00
|
|
|
void
|
|
|
|
|
ClientThread::SetState(ClientState &newState)
|
|
|
|
|
{
|
|
|
|
|
m_curState = &newState;
|
|
|
|
|
}
|
2007-02-25 23:16:26 +00:00
|
|
|
|
|
|
|
|
SenderThread &
|
|
|
|
|
ClientThread::GetSender()
|
|
|
|
|
{
|
|
|
|
|
assert(m_sender.get());
|
|
|
|
|
return *m_sender;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-13 02:23:12 +00:00
|
|
|
ReceiverHelper &
|
|
|
|
|
ClientThread::GetReceiver()
|
|
|
|
|
{
|
|
|
|
|
assert(m_receiver.get());
|
|
|
|
|
return *m_receiver;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-27 21:35:18 +00:00
|
|
|
const GameData &
|
|
|
|
|
ClientThread::GetGameData() const
|
|
|
|
|
{
|
|
|
|
|
assert(m_gameData.get());
|
|
|
|
|
return *m_gameData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ClientThread::SetGameData(const GameData &gameData)
|
|
|
|
|
{
|
|
|
|
|
assert(m_gameData.get());
|
|
|
|
|
*m_gameData = gameData;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
ClientSenderCallback &
|
|
|
|
|
ClientThread::GetSenderCallback()
|
|
|
|
|
{
|
|
|
|
|
assert(m_senderCallback.get());
|
|
|
|
|
return *m_senderCallback;
|
|
|
|
|
}
|
|
|
|
|
|