00001 #ifndef _MERCURY_NET_SERVER_H 00002 #define _MERCURY_NET_SERVER_H 00003 00004 #include "MercuryTimer.h" 00005 #include "global.h" 00006 #include "MercuryVector.h" 00007 #include "MercuryMessages.h" 00008 #include "MercuryTypes.h" 00009 #include "ezSockets.h" 00010 00011 class MercuryODEWorld; 00012 class MercuryNetServer; 00013 00014 struct ODEUnitInfo 00015 { 00016 MercuryPoint pos; 00017 MQuaternion rot; 00018 MercuryPoint linvel; 00019 MercuryPoint angvel; 00020 }; 00021 00022 class MercuryNetServerConnection 00023 { 00024 public: 00025 MercuryNetServerConnection( ezSockets * pAccepted ); 00026 ~MercuryNetServerConnection(); 00027 void BroadcastToClient( const MString & sMessage, const PStack & pParameters ); 00028 static void * CoreThreadCaller( void * NetClient); 00029 void CoreThread(); 00030 bool IsActive() { return m_bIsActive; } 00031 00032 void SendCommand( const MString & sCommandName, const PStack & kParameters ); 00034 void SetNetClass( MercuryObject * pNetClass ) { m_pNetClass = pNetClass; } 00035 00036 void AttachParent( MercuryNetServer * pParent ) { m_pParent = pParent; } 00037 protected: 00038 MercuryNetServer * m_pParent; 00039 MercuryObject * m_pNetClass; 00040 MercuryThread m_thdMain; 00041 bool m_bIsActive; 00042 ezSockets * m_pConnection; 00043 float m_fHalfPing; 00044 friend class MercuryNetServer; 00045 }; 00046 00047 //Default server connection. Your game probably will not use these. 00048 MercuryNetServerConnection * DefaultSocketConnection(ezSockets * pAccepted); 00049 00050 typedef MercuryNetServerConnection * (*GenClientFunction)(ezSockets * pAccepted); 00051 00052 class MercuryNetServer 00053 { 00054 public: 00055 MercuryNetServer(); 00056 ~MercuryNetServer(); 00057 00058 void BroadcastToAllClients( const MString & sMessage, const PStack & pParameters ); 00059 00060 bool BindAndListen( int iPort, GenClientFunction cbFunction = &DefaultSocketConnection); 00061 00062 void LinkODE( MercuryODEWorld * pWorld ) { m_pWorld = pWorld; } 00063 void Update( const float dTime ); 00064 00065 static void * CoreThreadCaller( void * NetClient ); 00066 static void * CoreUDPThreadCaller( void * NetClient ); 00067 void CoreThread(); 00068 void CoreUDPThread(); 00069 00070 void UpdateODE( const MString & sNameOfObject = "" ); 00071 void UpdateSingleODE( const MString & sObjectName ); 00072 00073 protected: 00074 ezSockets * m_pListener; 00075 ezSockets * m_pUDPListener; 00076 00077 00078 GenClientFunction m_cbGenFunction; 00079 MercuryThread m_thdMain; 00080 MercuryThread m_thdUDPMain; 00081 00082 MDeque< MercuryNetServerConnection * > m_vClients; 00083 00084 MercuryODEWorld * m_pWorld; 00085 MercuryTimer m_tmrSync; 00086 friend class MercuryNetServerConnection; 00087 00088 float m_fTimeSinceLastODE; 00089 }; 00090 00091 00092 //Utility functions 00093 00094 void NetEncodePStack( ezSocketsPacket & pPacket, const PStack & pParameters ); 00095 00096 void NetDecodePStack( ezSocketsPacket & pPacket, PStack & pParameters ); 00097 #endif