OpenMortal Apocalypse mirror
Sourceforge mirror
SourceForge.net Logo
      News | Info | Characters | Arenas | Screenshots | Forums | Download  
Developer: Making of | Character-HOWTO | AI Design | Submit a Character
Documentation: Main Page | Modules | Class Hierarchy | Class List | File List

MortalNetworkImpl.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                           MortalNetworkImpl.h  -  description
00003                              -------------------
00004     begin                : Sun Jan 25 2004
00005     copyright            : (C) 2004 by upi
00006     email                : upi@feel
00007  ***************************************************************************/
00008 
00009 #ifndef MORTALNETWORKIMPL_H
00010 #define MORTALNETWORKIMPL_H
00011 
00012 
00013 #include "MortalNetwork.h"
00014 #include "SDL_net.h"
00015 #include <string>
00016 #include <list>
00017 
00018 
00019 typedef std::list<int> TIntList;
00020 typedef std::list<std::string> TStringList;
00021 
00047 class CMortalNetworkImpl: public CMortalNetwork
00048 {
00049 public:
00050     CMortalNetworkImpl();
00051     virtual ~CMortalNetworkImpl();
00052     
00053     // Connection's lifecycle
00054 
00055     bool        Start( const char* a_pcServerName );
00056     void        Stop();                 // Disconnect
00057     bool        IsConnectionAlive();    // Is the connection still good?
00058     void        Update();               // Read network traffic. Might get disconnected...
00059     const char* GetLastError();
00060     bool        IsMaster();             // Am I Master or Slave?
00061 
00062     // Msg related methods
00063 
00064     const char* GetRemoteUsername();    // This is the name that is passed upon connection.
00065     void        SendMsg( const char* a_pcMsg ); // Prompt the user for a line of chat text
00066     bool        IsMsgAvailable();       // Returns true is a chatline has arrived
00067     const char* GetMsg();               // The next chatline, or NULL if there are no more.
00068 
00069     // Charater Selection methods
00070 
00071     bool        IsRemoteFighterAvailable( FighterEnum a_enFighter );    // Does the other computer have fighter X installed?
00072     FighterEnum GetRemoteFighter();     // Returns the latest fighter chosen by the remote side.
00073     bool        IsRemoteSideReady();    // The other player is finished choosing.
00074 
00075     void        SendFighter( FighterEnum a_enFighter ); // Let the other side know that I switched to fighter X.
00076     void        SendReady();            // Let the other side know that I am ready.
00077     void        SendGameParams( int a_iGameSpeed, int a_iGameTime, int a_iHitPoints, int a_iBackgroundNumber );
00078     SGameParams GetGameParams();
00079 
00080     // Game methods
00081 
00082     bool        SynchStartRound();
00083     void        SendGameData( const char* a_pcGameData );
00084     const char* GetLatestGameData();
00085     
00086     void        SendKeystroke( int a_iTime, int a_iKey, bool a_bPressed );
00087     bool        GetKeystroke( int& a_riOutTime, int& a_riOutKey, bool& a_rbPressed );
00088 
00089     void        SendGameTick( int a_iGameTick );
00090     int         GetGameTick();
00091 
00092     void        SendHurryup( int a_iHurryUpCode );
00093     int         GetHurryup() ;
00094 
00095     void        SendRoundOver( int a_iWhoWon, bool a_bGameOver );
00096     int         GetWhoWon();
00097     bool        IsRoundOver();
00098     bool        IsGameOver();
00099 
00100 protected:
00101     void        SendRawData( char a_cID, const void* a_pData, int a_iLength );
00102     
00103     void        ReceiveMsg( void* a_pData, int a_iLength );
00104     void        ReceiveRemoteUserName( void* a_pData, int a_iLength );
00105     void        ReceiveGameData( void* a_pData, int a_iLength );
00106     void        ReceiveKeystroke( void* a_pData, int a_iLength );
00107     void        ReceiveFighter( void* a_pData, int a_iLength );
00108     void        ReceiveReady( void* a_pData, int a_iLength );
00109     void        ReceiveRoundOver( void* a_pData, int a_iLength );
00110     void        ReceiveGameTick( void* a_pData, int a_iLength );
00111     void        ReceiveHurryup( void* a_pData, int a_iLength );
00112     void        ReceiveRemoteFighterAvailable( void* a_pData, int a_iLength );
00113     void        ReceiveRemoteFighterQuery( void* a_pData, int a_iLength );
00114     void        ReceiveGameParams( void* a_pData, int a_iLength );
00115     
00116 protected:
00117     
00118     enum TNetworkState
00119     {
00120         NS_DISCONNECTED,
00121         NS_CHARACTER_SELECTION,
00122         NS_IN_GAME,
00123     };
00124 
00125     // Network METADATA
00126     
00127     bool                    m_bNetworkAvailable;        
00128     TNetworkState           m_enState;                  
00129     bool                    m_bServer;                  
00130     bool                    m_bMaster;                  
00131     TCPsocket               m_poSocket;                 
00132     SDLNet_SocketSet        m_poSocketSet;              
00133 
00134     char                    m_acIncomingBuffer[2048];   
00135     int                     m_iIncomingBufferSize;      
00136     
00137     std::string             m_sLastError;               
00138     TStringList             m_asMsgs;                   
00139     
00140     // GAME DATA
00141 
00142     std::string             m_sRemoteUserName;
00143     TIntList                m_aiAvailableRemoteFighters;
00144     FighterEnum             m_enRemoteFighter;
00145     bool                    m_bRemoteReady;
00146     SGameParams             m_oGameParams;
00147     
00148     std::string             m_sLatestGameData;
00149     TIntList                m_aiKeyTimes;
00150     TIntList                m_aiKeystrokes;
00151     TIntList                m_abKeystrokes;
00152 
00153     bool                    m_bRoundOver;
00154     int                     m_iWhoWon;
00155     bool                    m_bGameOver;
00156     int                     m_iGameTick;
00157     int                     m_iHurryupCode;
00158     
00159     // REMOTE QUERY RESPONSES
00160     
00161     bool                    m_bSynchQueryResponse;
00162 };
00163 
00164 
00165 #endif // MORTALNETWORKIMPL_H
00166