Files
pokerth/src/net/common/clientthread.cpp
T

162 lines
4.2 KiB
C++
Raw Normal View History

/***************************************************************************
* 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>
#include <net/clientstate.h>
2007-03-13 23:27:17 +00:00
#include <net/clientcontext.h>
#include <net/senderthread.h>
#include <net/receiverhelper.h>
#include <net/clientexception.h>
#include <net/socket_msg.h>
#include <cassert>
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.
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-03-13 23:27:17 +00:00
m_context.reset(new ClientContext);
m_senderCallback.reset(new ClientSenderCallback(*this));
}
ClientThread::~ClientThread()
{
}
void
ClientThread::Init(const string &serverAddress, unsigned serverPort, bool ipv6, const string &pwd)
{
if (IsRunning())
return; // TODO: throw exception
2007-03-13 23:27:17 +00:00
ClientContext &context = GetContext();
2007-03-13 23:27:17 +00:00
context.SetAddrFamily(ipv6 ? AF_INET6 : AF_INET);
context.SetServerAddr(serverAddress);
context.SetServerPort(serverPort);
context.SetPassword(pwd);
}
ClientCallback &
ClientThread::GetCallback()
{
return m_callback;
}
void
ClientThread::Main()
{
SetState(CLIENT_INITIAL_STATE::Instance());
2007-03-13 23:27:17 +00:00
m_sender.reset(new SenderThread(GetSenderCallback()));
m_receiver.reset(new ReceiverHelper);
GetSender().Run();
try
{
while (!ShouldTerminate())
{
int msg = GetState().Process(*this);
if (msg != MSG_SOCK_INTERNAL_PENDING)
{
if (msg <= MSG_SOCK_LIMIT_CONNECT)
GetCallback().SignalNetClientConnect(msg);
else
GetCallback().SignalNetClientGameInfo(msg);
}
}
} catch (const NetException &e)
{
GetCallback().SignalNetClientError(e.GetErrorId(), e.GetOsErrorCode());
}
GetSender().SignalTermination();
2007-03-13 23:27:17 +00:00
GetSender().Join(SENDER_THREAD_TERMINATE_TIMEOUT);
}
2007-03-13 23:27:17 +00:00
const ClientContext &
ClientThread::GetContext() const
{
2007-03-13 23:27:17 +00:00
assert(m_context.get());
return *m_context;
}
2007-03-13 23:27:17 +00:00
ClientContext &
ClientThread::GetContext()
{
2007-03-13 23:27:17 +00:00
assert(m_context.get());
return *m_context;
}
ClientState &
ClientThread::GetState()
{
assert(m_curState);
return *m_curState;
}
void
ClientThread::SetState(ClientState &newState)
{
m_curState = &newState;
}
SenderThread &
ClientThread::GetSender()
{
assert(m_sender.get());
return *m_sender;
}
ReceiverHelper &
ClientThread::GetReceiver()
{
assert(m_receiver.get());
return *m_receiver;
}
2007-03-13 23:27:17 +00:00
ClientSenderCallback &
ClientThread::GetSenderCallback()
{
assert(m_senderCallback.get());
return *m_senderCallback;
}