Added client callback interface.
This commit is contained in:
@@ -95,20 +95,6 @@ public:
|
||||
virtual void logPlayerActionMsg(std::string playName, int action, int setValue) =0;
|
||||
virtual void logNewGameHandMsg(int gameID, int HandID) =0;
|
||||
|
||||
//connectToServerDialog
|
||||
virtual void showActionConnectToServerDialog(int actionID) =0;
|
||||
virtual void showErrorConnectToServerDialog(int errorID, int osErrorID) =0;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/***************************************************************************
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
/* Network client thread. */
|
||||
|
||||
#ifndef _CLIENTCALLBACK_H_
|
||||
#define _CLIENTCALLBACK_H_
|
||||
|
||||
class ClientCallback
|
||||
{
|
||||
public:
|
||||
virtual ~ClientCallback();
|
||||
|
||||
virtual void SignalSuccess(int actionID) = 0;
|
||||
virtual void SignalError(int errorID, int osErrorID) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -27,7 +27,7 @@
|
||||
#define CLIENT_INITIAL_STATE ClientStateInit
|
||||
|
||||
class ClientThread;
|
||||
class GuiInterface;
|
||||
class ClientCallback;
|
||||
|
||||
class ClientState
|
||||
{
|
||||
@@ -35,7 +35,7 @@ public:
|
||||
virtual ~ClientState();
|
||||
|
||||
// Main processing function of the current state.
|
||||
virtual void Process(ClientThread &client, GuiInterface &gui) = 0;
|
||||
virtual void Process(ClientThread &client, ClientCallback &gui) = 0;
|
||||
};
|
||||
|
||||
// State: Initialization.
|
||||
@@ -48,7 +48,7 @@ public:
|
||||
virtual ~ClientStateInit();
|
||||
|
||||
// Some basic initialization (socket creation, basic checks).
|
||||
virtual void Process(ClientThread &client, GuiInterface &gui);
|
||||
virtual void Process(ClientThread &client, ClientCallback &gui);
|
||||
|
||||
protected:
|
||||
|
||||
@@ -66,7 +66,7 @@ public:
|
||||
virtual ~ClientStateResolve();
|
||||
|
||||
// "Poll" for the completion of the name resolution.
|
||||
virtual void Process(ClientThread &client, GuiInterface &gui);
|
||||
virtual void Process(ClientThread &client, ClientCallback &gui);
|
||||
|
||||
protected:
|
||||
|
||||
@@ -84,7 +84,7 @@ public:
|
||||
virtual ~ClientStateConnect();
|
||||
|
||||
// "Poll" for the completion of the TCP/IP connect call.
|
||||
virtual void Process(ClientThread &client, GuiInterface &gui);
|
||||
virtual void Process(ClientThread &client, ClientCallback &gui);
|
||||
|
||||
protected:
|
||||
|
||||
@@ -102,7 +102,7 @@ public:
|
||||
virtual ~ClientStateFinal();
|
||||
|
||||
// sleep.
|
||||
virtual void Process(ClientThread &client, GuiInterface &gui);
|
||||
virtual void Process(ClientThread &client, ClientCallback &gui);
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
@@ -27,12 +27,12 @@
|
||||
|
||||
class ClientData;
|
||||
class ClientState;
|
||||
class GuiInterface;
|
||||
class ClientCallback;
|
||||
|
||||
class ClientThread : public Thread
|
||||
{
|
||||
public:
|
||||
ClientThread(GuiInterface &gui);
|
||||
ClientThread(ClientCallback &gui);
|
||||
virtual ~ClientThread();
|
||||
|
||||
// Set the parameters. Does not do any error checking.
|
||||
@@ -55,7 +55,7 @@ private:
|
||||
|
||||
std::auto_ptr<ClientData> m_data;
|
||||
ClientState *m_curState;
|
||||
GuiInterface &m_gui;
|
||||
ClientCallback &m_callback;
|
||||
|
||||
|
||||
friend class ClientStateInit;
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/***************************************************************************
|
||||
* 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/clientcallback.h>
|
||||
|
||||
|
||||
ClientCallback::~ClientCallback()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
#include <net/clientthread.h>
|
||||
#include <net/clientdata.h>
|
||||
#include <net/socket_msg.h>
|
||||
#include <gui/guiinterface.h>
|
||||
#include <net/clientcallback.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
@@ -51,26 +51,26 @@ ClientStateInit::~ClientStateInit()
|
||||
}
|
||||
|
||||
void
|
||||
ClientStateInit::Process(ClientThread &client, GuiInterface &gui)
|
||||
ClientStateInit::Process(ClientThread &client, ClientCallback &cb)
|
||||
{
|
||||
ClientData &data = client.GetData();
|
||||
|
||||
if (data.serverAddr.empty())
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_SERVERADDR_NOT_SET, 0);
|
||||
cb.SignalError(ERR_SOCK_SERVERADDR_NOT_SET, 0);
|
||||
throw runtime_error("ClientStateInit"); // TODO: own exception
|
||||
}
|
||||
|
||||
if (data.serverPort < 1024)
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_INVALID_PORT, 0);
|
||||
cb.SignalError(ERR_SOCK_INVALID_PORT, 0);
|
||||
throw runtime_error("ClientStateInit");
|
||||
}
|
||||
|
||||
data.sockfd = socket(data.addrFamily, SOCK_STREAM, 0);
|
||||
if (!IS_VALID_SOCKET(data.sockfd))
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
||||
cb.SignalError(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
||||
throw runtime_error("ClientStateInit");
|
||||
}
|
||||
|
||||
@@ -78,12 +78,12 @@ ClientStateInit::Process(ClientThread &client, GuiInterface &gui)
|
||||
unsigned long mode = 1;
|
||||
if (IOCTLSOCKET(data.sockfd, FIONBIO, &mode) == SOCKET_ERROR)
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
||||
cb.SignalError(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
||||
throw runtime_error("ClientStateInit");
|
||||
}
|
||||
#endif
|
||||
|
||||
gui.showActionConnectToServerDialog(MSG_SOCK_INIT_DONE);
|
||||
cb.SignalSuccess(MSG_SOCK_INIT_DONE);
|
||||
client.SetState(ClientStateResolve::Instance());
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ ClientStateResolve::~ClientStateResolve()
|
||||
}
|
||||
|
||||
void
|
||||
ClientStateResolve::Process(ClientThread &client, GuiInterface &gui)
|
||||
ClientStateResolve::Process(ClientThread &client, ClientCallback &cb)
|
||||
{
|
||||
ClientData &data = client.GetData();
|
||||
|
||||
@@ -117,7 +117,7 @@ ClientStateResolve::Process(ClientThread &client, GuiInterface &gui)
|
||||
// Set the port.
|
||||
if (!socket_set_port(data.serverPort, data.addrFamily, (struct sockaddr *)&data.clientAddr, data.GetServerAddrSize()))
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_SET_PORT_FAILED, 0);
|
||||
cb.SignalError(ERR_SOCK_SET_PORT_FAILED, 0);
|
||||
throw runtime_error("ClientStateResolve");
|
||||
}
|
||||
}
|
||||
@@ -128,11 +128,11 @@ ClientStateResolve::Process(ClientThread &client, GuiInterface &gui)
|
||||
tmpStr << data.serverPort;
|
||||
if (!socket_resolve(data.serverAddr.c_str(), tmpStr.str().c_str(), data.addrFamily, SOCK_STREAM, 0, (struct sockaddr *)&data.clientAddr, data.GetServerAddrSize()))
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_RESOLVE_FAILED, 0); // TODO: use errno value
|
||||
cb.SignalError(ERR_SOCK_RESOLVE_FAILED, 0); // TODO: use errno value
|
||||
throw runtime_error("ClientStateResolve");
|
||||
}
|
||||
}
|
||||
gui.showActionConnectToServerDialog(MSG_SOCK_RESOLVE_DONE);
|
||||
cb.SignalSuccess(MSG_SOCK_RESOLVE_DONE);
|
||||
client.SetState(ClientStateConnect::Instance());
|
||||
}
|
||||
|
||||
@@ -154,16 +154,16 @@ ClientStateConnect::~ClientStateConnect()
|
||||
}
|
||||
|
||||
void
|
||||
ClientStateConnect::Process(ClientThread &client, GuiInterface &gui)
|
||||
ClientStateConnect::Process(ClientThread &client, ClientCallback &cb)
|
||||
{
|
||||
ClientData &data = client.GetData();
|
||||
|
||||
if (!IS_VALID_CONNECT(connect(data.sockfd, (struct sockaddr *)&data.clientAddr, data.GetServerAddrSize())))
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_CONNECT_FAILED, SOCKET_ERRNO());
|
||||
cb.SignalError(ERR_SOCK_CONNECT_FAILED, SOCKET_ERRNO());
|
||||
throw runtime_error("ClientStateResolve");
|
||||
}
|
||||
gui.showActionConnectToServerDialog(MSG_SOCK_RESOLVE_DONE);
|
||||
cb.SignalSuccess(MSG_SOCK_RESOLVE_DONE);
|
||||
client.SetState(ClientStateFinal::Instance());
|
||||
}
|
||||
|
||||
@@ -185,7 +185,7 @@ ClientStateFinal::~ClientStateFinal()
|
||||
}
|
||||
|
||||
void
|
||||
ClientStateFinal::Process(ClientThread &client, GuiInterface &gui)
|
||||
ClientStateFinal::Process(ClientThread &client, ClientCallback &cb)
|
||||
{
|
||||
boost::xtime t;
|
||||
boost::xtime_get(&t, boost::TIME_UTC);
|
||||
|
||||
@@ -26,8 +26,8 @@
|
||||
using namespace std;
|
||||
|
||||
|
||||
ClientThread::ClientThread(GuiInterface &gui)
|
||||
: m_curState(NULL), m_gui(gui)
|
||||
ClientThread::ClientThread(ClientCallback &cb)
|
||||
: m_curState(NULL), m_callback(cb)
|
||||
{
|
||||
m_data.reset(new ClientData);
|
||||
m_curState = &CLIENT_INITIAL_STATE::Instance();
|
||||
@@ -53,7 +53,7 @@ ClientThread::Main()
|
||||
{
|
||||
while (!ShouldTerminate())
|
||||
{
|
||||
GetState().Process(*this, m_gui);
|
||||
GetState().Process(*this, m_callback);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user