/* $Id: udpdata.hpp,v 1.13 2005/06/28 13:55:24 chfreund Exp $ */ #ifndef _UDPDATA_HPP_ #define _UDPDATA_HPP_ #include #include #include "serializable.hpp" #include "serialize.hpp" #include "event.hpp" #include "constants.hpp" /*! \brief UDP data for transmission of an event from a client to a server */ struct EventData : public Serializable { //! The event Event event; //! The last consecutive echo frame the client has received Uint8 lastReceivedFrame; static Uint32 getStaticSerializeBufferSize() { return Event::getStaticSerializeBufferSize() + Serialize::sizeOf(); } virtual void serialize( Uint8*& bufferPointer ) const { event.serialize( bufferPointer ); Serialize::serialize( lastReceivedFrame, bufferPointer ); } virtual void deserialize( Uint8*& bufferPointer ) { event.deserialize( bufferPointer ); Serialize::deserialize( bufferPointer, lastReceivedFrame ); } virtual Uint32 getSerializeBufferSize() const { return getStaticSerializeBufferSize(); } void dumpToFile( const char* filename, const int playerID ) const { DBG( 5 ) { FILE* file = fopen( filename, "a" ); if( file ) { fprintf( file, "%2d: %3d: %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", playerID, lastReceivedFrame, (event.testAnyActions( Event::LEFT ) ? "L" : "."), (event.testAnyActions( Event::RIGHT ) ? "R" : "."), (event.testAnyActions( Event::UP ) ? "U" : "."), (event.testAnyActions( Event::DOWN ) ? "D" : "."), (event.testAnyActions( Event::SHOOT ) ? "S" : "."), (event.testAnyActions( Event::JUMP ) ? "J" : "."), (event.testAnyActions( Event::DIG ) ? "D" : "."), (event.testAnyActions( Event::JET ) ? "J" : "."), (event.testAnyActions( Event::ROPE_ON ) ? "N" : "."), (event.testAnyActions( Event::ROPE_OFF ) ? "F" : "."), (event.testAnyActions( Event::ROPE_IN ) ? "I" : "."), (event.testAnyActions( Event::ROPE_OUT ) ? "O" : "."), (event.testAnyActions( Event::ROPE_REL ) ? "R" : "."), (event.testAnyActions( Event::N_A ) ? "*" : ".") ); fclose( file ); } } } }; /*! \brief UDP data for the server replies */ struct EchoData : public Serializable { //! The frame the echo message is referring to Uint8 frame; //! The status of the server Status status; //! The number of connected clients Uint8 numberClients; //! The counter of the random number generator Uint32 randomCounter; //! The events from all clients Event event[MAX_NUMBER_OF_PLAYERS]; void dump() const { INFO( "Echo Data: " ); for ( int i = 0; i < numberClients; i++ ) { INFO( "%d ", event[i].get() ); } INFO( "\n" ); } static Uint32 getStaticSerializeBufferSize() { return 2 * Serialize::sizeOf() + Serialize::sizeOf() + Serialize::sizeOf() + MAX_NUMBER_OF_PLAYERS * Event::getStaticSerializeBufferSize(); } virtual void serialize( Uint8*& bufferPointer ) const { Serialize::serialize( frame, bufferPointer ); Serialize::serialize( status, bufferPointer ); Serialize::serialize( numberClients, bufferPointer ); Serialize::serialize( randomCounter, bufferPointer ); for ( int i = 0; i < MAX_NUMBER_OF_PLAYERS; i++ ) { event[i].serialize( bufferPointer ); } } virtual void deserialize( Uint8*& bufferPointer ) { Serialize::deserialize( bufferPointer, frame ); Serialize::deserialize( bufferPointer, status ); Serialize::deserialize( bufferPointer, numberClients ); Serialize::deserialize( bufferPointer, randomCounter ); for ( int i = 0; i < MAX_NUMBER_OF_PLAYERS; i++ ) { event[i].deserialize( bufferPointer ); } } virtual Uint32 getSerializeBufferSize() const { return getStaticSerializeBufferSize(); } }; #endif // _UDPDATA_HPP_