From ca6adbb295b103d811ed2e01aefebdda7440a10a Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 18 Feb 2007 01:16:18 +0000 Subject: [PATCH] Added client callback interface. --- src/gui/guiinterface.h | 14 ------------- src/net/clientcallback.h | 33 +++++++++++++++++++++++++++++++ src/net/clientstate.h | 12 +++++------ src/net/clientthread.h | 6 +++--- src/net/common/clientcallback.cpp | 26 ++++++++++++++++++++++++ src/net/common/clientstate.cpp | 30 ++++++++++++++-------------- src/net/common/clientthread.cpp | 6 +++--- 7 files changed, 86 insertions(+), 41 deletions(-) create mode 100644 src/net/clientcallback.h create mode 100644 src/net/common/clientcallback.cpp diff --git a/src/gui/guiinterface.h b/src/gui/guiinterface.h index 25996fa2..fbf818ad 100644 --- a/src/gui/guiinterface.h +++ b/src/gui/guiinterface.h @@ -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 diff --git a/src/net/clientcallback.h b/src/net/clientcallback.h new file mode 100644 index 00000000..e57bcfc7 --- /dev/null +++ b/src/net/clientcallback.h @@ -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 diff --git a/src/net/clientstate.h b/src/net/clientstate.h index c0c1f386..bd780547 100644 --- a/src/net/clientstate.h +++ b/src/net/clientstate.h @@ -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: diff --git a/src/net/clientthread.h b/src/net/clientthread.h index 1a972bfd..cf4c1037 100644 --- a/src/net/clientthread.h +++ b/src/net/clientthread.h @@ -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 m_data; ClientState *m_curState; - GuiInterface &m_gui; + ClientCallback &m_callback; friend class ClientStateInit; diff --git a/src/net/common/clientcallback.cpp b/src/net/common/clientcallback.cpp new file mode 100644 index 00000000..0946c91f --- /dev/null +++ b/src/net/common/clientcallback.cpp @@ -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 + + +ClientCallback::~ClientCallback() +{ +} + diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index 6f457a08..a9f6e6e0 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include #include @@ -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); diff --git a/src/net/common/clientthread.cpp b/src/net/common/clientthread.cpp index e8ade884..8ac8d2f2 100644 --- a/src/net/common/clientthread.cpp +++ b/src/net/common/clientthread.cpp @@ -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); } }