include/Client.h

00001 #include <iostream>
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 #include <sstream>
00005 #include <zoidcom.h>
00006 #include <limits.h>
00007 #include <Game.h>
00008 
00009 #define PORT 1337
00010 #define IP "141.82.59.28"
00011 //SERVER AUF DER PRÄSE!
00012 //#define IP "141.82.59.30"
00013 
00014 //char IP[ 16 ];
00015 //char PORT[ 6 ];
00016 
00017 class Client : public ZCom_Control 
00018 {
00019 
00020 public:
00021         ZoidCom* zcom;
00022         ZCom_BitStream* groupTypeMsg;
00023         bool exit_now;
00024         ZCom_Address server_addr;
00025         ZCom_ConnID server_id;
00026         ZCom_ConnID client_id;
00027         //char clientType[ 100 ];
00028         char* clientType;
00029         Game* game;
00030 
00031         Client()
00032         {
00033                 ;
00034         }
00035 
00036         Client( char* clientType, Game* game )
00037         {
00038 
00039                 this->game = game;
00040                 this->exit_now = false;
00041                 this->server_id = 0;
00042                 this->client_id = 0;
00043                 this->clientType = clientType;
00044 
00045                 // initialize Zoidcom
00046                 zcom = new ZoidCom("sonus_network_controller.log");     
00047                 if ( !zcom || !zcom->Init() )
00048                         exit(255);
00049 
00050                 // make instance of our Client class
00051                 /*Client *client = new Client();*/
00052                 this->ZCom_setDebugName( "Client" );
00053 
00054                 zcom->setConnectionTimeout( UINT_MAX );
00055                 //printf( "Timeout set to %u seconds\n", UINT_MAX / 1000 );
00056 
00057                 // this creates and initializes the network sockets
00058                 // true = use UDP socket, 0 = let OS choose UDP port, 0 = no internal socket
00059                 bool result = this->ZCom_initSockets( true, 0, 0 );
00060 
00061                 /*printf( "Select client type: (GAME or TUTORIAL): " );
00062                 fgets( clientType, 10, stdin );
00063                 printf( "Selected client type: %s\n", clientType );*/
00064 
00065                 /*printf("Connecting...\n");*/
00066                 // prepare bitstream
00067                 groupTypeMsg = new ZCom_BitStream();
00068                 groupTypeMsg->addString( clientType );
00069 
00070                 // prepare address
00071                 std::stringstream sstr;
00072                 sstr << IP << ":" << PORT;
00073                 server_addr.setAddress( eZCom_AddressUDP, 0, sstr.str().c_str() );
00074 
00075                 // connect
00076                 server_id = this->ZCom_Connect( server_addr, groupTypeMsg );
00077                 //printf( "SERVER ID: %d ADDRESS: %u\n", server_id, server_addr.getIP() );
00078                 this->ZCom_processInput();
00079                 this->ZCom_processOutput();
00080                 this->ZCom_processInput();
00081         }
00082 
00083         ~Client()
00084         {
00085                 delete zcom;
00086         }
00087 
00088         void update()
00089         {
00090                 // put this into a codeblock so that server_addr gets out of scope before 
00091                 // 'delete zcom;' get called (everything needs to be gone before the 
00092                 // ZoidCom object gets deleted)
00093                 {
00094 
00095                                 // make sure every input process is processed!
00096                                 for( int i = 0; i < 3; i++ )
00097                                 {
00098                                         this->ZCom_processInput();
00099                                 }
00100 
00101                                 ZCom_BitStream* message = new ZCom_BitStream();
00102                                 //code Nr 6 for 'update'
00103                                 message->addInt( 6, 8 );
00104 
00105                                 //player position
00106                                 message->addFloat( 132.465, 23 );
00107                                 message->addFloat( -100.69, 23 );
00108 
00109                                 //player orientation
00110                                 message->addFloat( -1.46682, 23 );
00111 
00112                                 //playerReachedPortal
00113                                 message->addBool( false );
00114 
00115                                 //playerObstaclesBeforePortal
00116                                 message->addBool( true );
00117 
00118                                 //end game
00119                                 message->addBool( false );
00120 
00121                                 //level name
00122                                 message->addString( "01tut" );
00123 
00124                                 // send the data to the server
00125                                 //ZCom_sendData( _id, message );
00126 
00127 
00128                                 for( int i = 0; i < 3; i++ )
00129                                 {
00130                                         this->ZCom_processOutput();
00131                                 }
00132 
00133                                 zcom->Sleep( 100 );
00134                 }
00135 
00136                 this->ZCom_processOutput();
00137 
00138                 // give up remaining cpu time
00139                 //zcom->Sleep(20);
00140         }
00141 
00142         protected:
00143 
00144         virtual void ZCom_cbConnectResult( ZCom_ConnID _id, eZCom_ConnectResult _result, ZCom_BitStream &_reply )
00145         {
00146                 if ( _result == eZCom_ConnAccepted )
00147                 {
00148                         //printf("Connection established. The reply was: %s\n", _reply.getStringStatic());
00149                         client_id = _reply.getInt( 8 );
00150                         //printf("This clients ID is: %d\n", client_id );
00151 
00152                 } else {
00153                         //printf("Connection failed. The reply was: %s\n", _reply.getStringStatic());
00154                         return;
00155                 }
00156 
00157                 // if we get here, the connection was accepted
00158 
00159                 server_id = _id;
00160                 //printf( "server_id has been set to: %i\n", server_id );
00161         }
00162 
00163         void ZCom_cbConnectionClosed( ZCom_ConnID _id, eZCom_CloseReason _reason, ZCom_BitStream &_reasondata )  
00164         {
00165                 //printf("Connection to server closed. Exiting...\n");
00166                 exit_now = true;
00167         }
00168 
00169         // data was received
00170         void ZCom_cbDataReceived( ZCom_ConnID  _id, ZCom_BitStream &_data ) 
00171         {
00172                 ZCom_BitStream* message = new ZCom_BitStream();
00173 
00174                 switch( _data.getInt( 8 ) )
00175                 {
00176                 case 1:
00177                         //printf( "starting game\n" );
00178                         //call game->begingame
00179                         game->beginGame();
00180                         OutputDebugStr( "CLIENT: starting game\n" );
00181                         break;
00182                 case 2:
00183                         //printf( "stopping game\n" );
00184                         //call game->endgame
00185                         game->endGame();
00186                         OutputDebugStr( "CLIENT: stopping game\n" );
00187                         break;
00188                 case 3:
00189                         //printf( "restarting game\n" );
00190                         game->endGame();
00191                         game->beginGame();
00192                         break;
00193                 case 4:
00194                         //printf( "winning game\n" );
00195                         //end game for other player
00196                         break;
00197                 case 5:
00198                         {
00199                                 //Send dummy Data, should be actual data from the Game
00200 
00201                                 //printf( "Sending update to server...\n" );
00202                                 //ZCom_BitStream* message = new ZCom_BitStream();
00203 
00204                                 //code Nr 6 for 'update'
00205                                 message->addInt( 6, 8 );
00206 
00207                                 //player position
00208                                 message->addFloat( 132.465, 23);
00209                                 message->addFloat( -100.69, 23);
00210                                 //player orientation
00211                                 message->addFloat( -1.46682, 23);
00212                                 //playerReachedPortal
00213                                 message->addBool( false );
00214                                 //playerObstaclesBeforePortal
00215                                 message->addBool( true );
00216                                 //end game
00217                                 message->addBool( false );
00218                                 //level name
00219                                 message->addString( "01tut" );
00220 
00221                                 // send the data to the server
00222                                 //ZCom_sendData( _id, message );
00223                                 // message has been given to Zoidcom and may not be deleted
00224                                 //message = NULL;
00225                                 break;
00226                         }
00227                 default:
00228                         break;
00229                 }
00230 
00231 
00232                 // send the data to the server
00233                 ZCom_sendData( _id, message );
00234                 // message has been given to Zoidcom and may not be deleted
00235                 message = NULL;                 
00236         }
00237 
00238         //these functions can be ignored for a start; not (yet) necessary for basic networking control for sonus
00239         bool ZCom_cbConnectionRequest( ZCom_ConnID  _id, ZCom_BitStream &_request, ZCom_BitStream &_reply ){return false;}
00240         void ZCom_cbConnectionSpawned( ZCom_ConnID _id ) {}
00241         bool ZCom_cbZoidRequest( ZCom_ConnID _id, zU8 _requested_level, ZCom_BitStream &_reason ) {return false;}
00242         void ZCom_cbZoidResult( ZCom_ConnID _id, eZCom_ZoidResult _result, zU8 _new_level, ZCom_BitStream &_reason ) {}
00243         void ZCom_cbNodeRequest_Dynamic( ZCom_ConnID _id, ZCom_ClassID _requested_class, ZCom_BitStream *_announcedata,
00244                 eZCom_NodeRole _role, ZCom_NodeID _net_id ) {}
00245         void ZCom_cbNodeRequest_Tag( ZCom_ConnID _id, ZCom_ClassID _requested_class, ZCom_BitStream *_announcedata,
00246                 eZCom_NodeRole _role, zU32 _tag ) {}
00247         bool ZCom_cbDiscoverRequest( const ZCom_Address &_addr, 
00248                 ZCom_BitStream &_request, ZCom_BitStream &_reply ) {return false;}
00249         void ZCom_cbDiscovered( const ZCom_Address & _addr, ZCom_BitStream &_reply )  {}
00250 };

Generated on Tue Aug 26 12:26:54 2008 for sonus by  doxygen 1.5.4