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