/*************************************************************************** * 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. * ***************************************************************************/ /* State of network client. */ #ifndef _CLIENTSTATE_H_ #define _CLIENTSTATE_H_ #include #include #include #define CLIENT_INITIAL_STATE ClientStateInit #define CLIENT_FINAL_STATE ClientStateFinal class ClientThread; class ClientCallback; class ResolverThread; class Game; class NetPacket; class DownloadHelper; class ClientState { public: virtual ~ClientState(); virtual void Enter(boost::shared_ptr client) = 0; virtual void Exit(boost::shared_ptr client) = 0; virtual void HandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket) = 0; }; // State: Initialization. class ClientStateInit : public ClientState { public: // Access the state singleton. static ClientStateInit &Instance(); virtual ~ClientStateInit(); // Some basic initialization (socket creation, basic checks). virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} protected: // Protected constructor - this is a singleton. ClientStateInit(); }; // State: Starting name resolution. class ClientStateStartResolve : public ClientState { public: // Access the state singleton. static ClientStateStartResolve &Instance(); virtual ~ClientStateStartResolve(); // Initiate the name resolution. virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} protected: // Protected constructor - this is a singleton. ClientStateStartResolve(); void HandleResolve( const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator, boost::shared_ptr client); }; // State: Start download of the server list. class ClientStateStartServerListDownload : public ClientState { public: // Access the state singleton. static ClientStateStartServerListDownload &Instance(); virtual ~ClientStateStartServerListDownload(); // Initiate the name resolution. virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} protected: // Protected constructor - this is a singleton. ClientStateStartServerListDownload(); }; // State: Synchronizing server list. class ClientStateSynchronizingServerList : public ClientState { public: // Access the state singleton. static ClientStateSynchronizingServerList &Instance(); virtual ~ClientStateSynchronizingServerList(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} void SetDownloadHelper(boost::shared_ptr helper); protected: // Protected constructor - this is a singleton. ClientStateSynchronizingServerList(); // Poll for the completion of the download. void TimerLoop(const boost::system::error_code& ec, boost::shared_ptr client); private: boost::shared_ptr m_downloadHelper; }; // State: Downloading the server list. class ClientStateDownloadingServerList : public ClientState { public: // Access the state singleton. static ClientStateDownloadingServerList &Instance(); virtual ~ClientStateDownloadingServerList(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} void SetDownloadHelper(boost::shared_ptr helper); protected: // Protected constructor - this is a singleton. ClientStateDownloadingServerList(); // Poll for the completion of the download. void TimerLoop(const boost::system::error_code& ec, boost::shared_ptr client); private: boost::shared_ptr m_downloadHelper; }; // State: Reading the server list. class ClientStateReadingServerList : public ClientState { public: static ClientStateReadingServerList &Instance(); virtual ~ClientStateReadingServerList(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} protected: // Protected constructor - this is a singleton. ClientStateReadingServerList(); }; // State: Waiting for the user to choose a server. class ClientStateWaitChooseServer : public ClientState { public: static ClientStateWaitChooseServer &Instance(); virtual ~ClientStateWaitChooseServer(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} protected: // Protected constructor - this is a singleton. ClientStateWaitChooseServer(); void TimerLoop(const boost::system::error_code& ec, boost::shared_ptr client); }; // State: Initiate server connection. class ClientStateStartConnect : public ClientState { public: // Access the state singleton. static ClientStateStartConnect &Instance(); virtual ~ClientStateStartConnect(); // Call connect. virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} void SetRemoteEndpoint(boost::asio::ip::tcp::resolver::iterator endpointIterator); protected: // Protected constructor - this is a singleton. ClientStateStartConnect(); void HandleConnect(const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator, boost::shared_ptr client); void TimerTimeout(const boost::system::error_code& ec, boost::shared_ptr client); private: boost::asio::ip::tcp::resolver::iterator m_remoteEndpointIterator; }; // Abstract State: Receiving class AbstractClientStateReceiving : public ClientState { public: virtual ~AbstractClientStateReceiving(); virtual void HandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket); protected: AbstractClientStateReceiving(); virtual void InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket) = 0; }; // State: Session init. class ClientStateStartSession : public AbstractClientStateReceiving { public: // Access the state singleton. static ClientStateStartSession &Instance(); virtual ~ClientStateStartSession(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); protected: // Protected constructor - this is a singleton. ClientStateStartSession(); virtual void InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket); }; // State: Waiting for the user to enter login data. class ClientStateWaitEnterLogin : public ClientState { public: static ClientStateWaitEnterLogin &Instance(); virtual ~ClientStateWaitEnterLogin(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} protected: // Protected constructor - this is a singleton. ClientStateWaitEnterLogin(); void TimerLoop(const boost::system::error_code& ec, boost::shared_ptr client); }; // State: Wait for Authentication Challenge. class ClientStateWaitAuthChallenge : public AbstractClientStateReceiving { public: // Access the state singleton. static ClientStateWaitAuthChallenge &Instance(); virtual ~ClientStateWaitAuthChallenge(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); protected: // Protected constructor - this is a singleton. ClientStateWaitAuthChallenge(); virtual void InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket); }; // State: Wait for Authentication Verification. class ClientStateWaitAuthVerify : public AbstractClientStateReceiving { public: // Access the state singleton. static ClientStateWaitAuthVerify &Instance(); virtual ~ClientStateWaitAuthVerify(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); protected: // Protected constructor - this is a singleton. ClientStateWaitAuthVerify(); virtual void InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket); }; // State: Wait for Session ACK. class ClientStateWaitSession : public AbstractClientStateReceiving { public: // Access the state singleton. static ClientStateWaitSession &Instance(); virtual ~ClientStateWaitSession(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); protected: // Protected constructor - this is a singleton. ClientStateWaitSession(); virtual void InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket); }; // State: Wait for Join. class ClientStateWaitJoin : public AbstractClientStateReceiving { public: // Access the state singleton. static ClientStateWaitJoin &Instance(); virtual ~ClientStateWaitJoin(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); protected: // Protected constructor - this is a singleton. ClientStateWaitJoin(); virtual void InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket); }; // State: Wait for start of the game or start info. class ClientStateWaitGame : public AbstractClientStateReceiving { public: // Access the state singleton. static ClientStateWaitGame &Instance(); virtual ~ClientStateWaitGame(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); protected: // Protected constructor - this is a singleton. ClientStateWaitGame(); virtual void InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket); }; // State: Synchronize on game start. class ClientStateSynchronizeStart : public AbstractClientStateReceiving { public: // Access the state singleton. static ClientStateSynchronizeStart &Instance(); virtual ~ClientStateSynchronizeStart(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); protected: // Protected constructor - this is a singleton. ClientStateSynchronizeStart(); void TimerLoop(const boost::system::error_code& ec, boost::shared_ptr client); virtual void InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket); }; // State: Wait for game start. class ClientStateWaitStart : public AbstractClientStateReceiving { public: // Access the state singleton. static ClientStateWaitStart &Instance(); virtual ~ClientStateWaitStart(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); protected: // Protected constructor - this is a singleton. ClientStateWaitStart(); virtual void InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket); }; // State: Wait for start of the next hand. class ClientStateWaitHand : public AbstractClientStateReceiving { public: // Access the state singleton. static ClientStateWaitHand &Instance(); virtual ~ClientStateWaitHand(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); protected: // Protected constructor - this is a singleton. ClientStateWaitHand(); virtual void InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket); }; // State: Hand Loop. class ClientStateRunHand : public AbstractClientStateReceiving { public: // Access the state singleton. static ClientStateRunHand &Instance(); virtual ~ClientStateRunHand(); virtual void Enter(boost::shared_ptr client); virtual void Exit(boost::shared_ptr client); protected: // Protected constructor - this is a singleton. ClientStateRunHand(); virtual void InternalHandlePacket(boost::shared_ptr client, boost::shared_ptr tmpPacket); static void ResetPlayerActions(Game &curGame); static void ResetPlayerSets(Game &curGame); }; class ClientStateFinal : public ClientState { public: static ClientStateFinal &Instance(); virtual ~ClientStateFinal() {} virtual void Enter(boost::shared_ptr /*client*/) {} virtual void Exit(boost::shared_ptr /*client*/) {} virtual void HandlePacket(boost::shared_ptr /*client*/, boost::shared_ptr /*tmpPacket*/) {} protected: // Protected constructor - this is a singleton. ClientStateFinal() {} }; #endif