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
00012
00013
00014
00015
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
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
00046 zcom = new ZoidCom("sonus_network_controller.log");
00047 if ( !zcom || !zcom->Init() )
00048 exit(255);
00049
00050
00051
00052 this->ZCom_setDebugName( "Client" );
00053
00054 zcom->setConnectionTimeout( UINT_MAX );
00055
00056
00057
00058
00059 bool result = this->ZCom_initSockets( true, 0, 0 );
00060
00061
00062
00063
00064
00065
00066
00067 groupTypeMsg = new ZCom_BitStream();
00068 groupTypeMsg->addString( clientType );
00069
00070
00071 std::stringstream sstr;
00072 sstr << IP << ":" << PORT;
00073 server_addr.setAddress( eZCom_AddressUDP, 0, sstr.str().c_str() );
00074
00075
00076 server_id = this->ZCom_Connect( server_addr, groupTypeMsg );
00077
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
00091
00092
00093 {
00094
00095
00096 for( int i = 0; i < 3; i++ )
00097 {
00098 this->ZCom_processInput();
00099 }
00100
00101 ZCom_BitStream* message = new ZCom_BitStream();
00102
00103 message->addInt( 6, 8 );
00104
00105
00106 message->addFloat( 132.465, 23 );
00107 message->addFloat( -100.69, 23 );
00108
00109
00110 message->addFloat( -1.46682, 23 );
00111
00112
00113 message->addBool( false );
00114
00115
00116 message->addBool( true );
00117
00118
00119 message->addBool( false );
00120
00121
00122 message->addString( "01tut" );
00123
00124
00125
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
00139
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
00149 client_id = _reply.getInt( 8 );
00150
00151
00152 } else {
00153
00154 return;
00155 }
00156
00157
00158
00159 server_id = _id;
00160
00161 }
00162
00163 void ZCom_cbConnectionClosed( ZCom_ConnID _id, eZCom_CloseReason _reason, ZCom_BitStream &_reasondata )
00164 {
00165
00166 exit_now = true;
00167 }
00168
00169
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
00178
00179 game->beginGame();
00180 OutputDebugStr( "CLIENT: starting game\n" );
00181 break;
00182 case 2:
00183
00184
00185 game->endGame();
00186 OutputDebugStr( "CLIENT: stopping game\n" );
00187 break;
00188 case 3:
00189
00190 game->endGame();
00191 game->beginGame();
00192 break;
00193 case 4:
00194
00195
00196 break;
00197 case 5:
00198 {
00199
00200
00201
00202
00203
00204
00205 message->addInt( 6, 8 );
00206
00207
00208 message->addFloat( 132.465, 23);
00209 message->addFloat( -100.69, 23);
00210
00211 message->addFloat( -1.46682, 23);
00212
00213 message->addBool( false );
00214
00215 message->addBool( true );
00216
00217 message->addBool( false );
00218
00219 message->addString( "01tut" );
00220
00221
00222
00223
00224
00225 break;
00226 }
00227 default:
00228 break;
00229 }
00230
00231
00232
00233 ZCom_sendData( _id, message );
00234
00235 message = NULL;
00236 }
00237
00238
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 };