Changeset 7954 in orxonox.OLD for trunk/src/subprojects
- Timestamp:
- May 29, 2006, 3:28:41 PM (18 years ago)
- Location:
- trunk/src/subprojects/network
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/subprojects/network/Makefile.am
r7160 r7954 1 1 MAINSRCDIR=../.. 2 2 include $(MAINSRCDIR)/defs/include_paths.am 3 4 LIB_PREFIX=$(MAINSRCDIR)/lib 5 include $(MAINSRCDIR)/lib/BuildLibs.am 6 3 7 4 8 bin_PROGRAMS = network 5 9 6 10 7 network_CPPFLAGS = -DNO_SHELL -DNO_SHELL_COMMAND 11 network_CPPFLAGS = 12 #-DNO_SHELL -DNO_SHELL_COMMAND 8 13 9 DEPENDENCIES = \14 network_DEPENDENCIES = \ 10 15 $(MAINSRCDIR)/world_entities/libORXwe.a \ 11 16 $(libORXlibs_a_LIBRARIES_) \ 12 17 $(MAINSRCDIR)/util/libORXutils.a 13 18 14 LDADD = \19 network_LDADD = \ 15 20 $(MAINSRCDIR)/util/libORXutils.a \ 16 21 $(libORXlibs_a_LIBRARIES_) \ -
trunk/src/subprojects/network/network_unit_test.cc
r6424 r7954 9 9 #include "network_socket.h" 10 10 #include "server_socket.h" 11 #include "udp_server_socket.h" 12 #include "udp_socket.h" 11 13 #include "network_stream.h" 12 14 #include "synchronizeable.h" 13 15 #include "converter.h" 16 #include "state.h" 17 #include "shared_network_data.h" 18 #include "message_manager.h" 19 #include "network_log.h" 14 20 15 21 #include "simple_sync.h" … … 36 42 int testSocket(int argc, char** argv) 37 43 { 38 IPaddress ip; 39 SDLNet_ResolveHost(&ip, "127.0.0.1", 9999); 40 ServerSocket server; 41 server.listen(9999); 42 43 NetworkSocket* client1 = new NetworkSocket(ip); 44 45 NetworkSocket* server1 = server.getNewSocket(); 46 47 NetworkSocket* client2 = new NetworkSocket(ip); 48 49 NetworkSocket* server2 = server.getNewSocket(); 44 UdpServerSocket server(9999); 45 46 NetworkSocket* client1 = new UdpSocket("localhost", 9999); 47 48 NetworkSocket* server1 = NULL; 49 while ( server1 == NULL ) 50 { 51 server.update(); 52 server1 = server.getNewSocket(); 53 } 54 55 assert( server1->isOk() ); 56 57 NetworkSocket* client2 = new UdpSocket("localhost", 9999); 58 59 NetworkSocket* server2 = NULL; 60 while ( server2 == NULL ) 61 { 62 server.update(); 63 server2 = server.getNewSocket(); 64 } 50 65 51 66 char buf[1024]; … … 70 85 n = server2->writePacket((byte*)str4, strlen(str4)+1); 71 86 printf("%d bytes send from server2\n", n); 72 SDL_Delay(1000); 87 SDL_Delay(10); 88 89 server.update(); 73 90 74 91 printf("read from server1\n"); … … 103 120 104 121 printf("data: '%s'\n", buf); 122 123 124 //22222222222222222222222222222222222222222 125 n = client1->writePacket((byte*)str1, strlen(str1)+1); 126 printf("%d bytes send from client1\n", n); 127 n = server1->writePacket((byte*)str2, strlen(str2)+1); 128 printf("%d bytes send from server1\n", n); 129 n = client2->writePacket((byte*)str3, strlen(str3)+1); 130 printf("%d bytes send from client2\n", n); 131 n = server2->writePacket((byte*)str4, strlen(str4)+1); 132 printf("%d bytes send from server2\n", n); 133 SDL_Delay(10); 134 135 server.update(); 136 137 printf("read from server1\n"); 138 n = server1->readPacket((byte*)buf, 1024); 139 printf("read %d bytes\n", n); 140 if (n<0) 141 return -1; 142 143 printf("data: '%s'\n", buf); 144 145 printf("read from server2\n"); 146 n = server2->readPacket((byte*)buf, 1024); 147 printf("read %d bytes\n", n); 148 if (n<0) 149 return -1; 150 151 printf("data: '%s'\n", buf); 152 153 printf("read from client1\n"); 154 n = client1->readPacket((byte*)buf, 1024); 155 printf("read %d bytes\n", n); 156 if (n<0) 157 return -1; 158 159 printf("data: '%s'\n", buf); 160 161 printf("read from client2\n"); 162 n = client2->readPacket((byte*)buf, 1024); 163 printf("read %d bytes\n", n); 164 if (n<0) 165 return -1; 166 167 printf("data: '%s'\n", buf); 105 168 106 169 //sending bigger packets than 255 is not supported 107 170 #if 0 108 171 printf("try to send more than 255 bytes\n"); 109 172 printf("result: %d\n", client1->writePacket((byte*)buf, 1000)); … … 113 176 printf("try to read with a too small buffer\n"); 114 177 printf("result: %d\n", client1->readPacket((byte*)buf, strlen(str1))); 115 178 #endif 179 180 delete client1; 181 delete client2; 182 delete server1; 183 delete server2; 184 116 185 return 0; 117 186 } … … 124 193 printf("=================\n"); 125 194 126 Synchronizeable* clientSync = new SimpleSync( "Client\0");127 Synchronizeable* serverSync = new SimpleSync( "Server\0");195 Synchronizeable* clientSync = new SimpleSync( std::string("Client") ); 196 Synchronizeable* serverSync = new SimpleSync( std::string("Server") ); 128 197 129 198 unsigned int port = 9999; … … 144 213 if(error == -1) 145 214 printf("\n\nerror on address resolution, program inconsistancy\n\n"); 146 nm->establishConnection(ip, *clientSync); 215 nm->establishConnection("localhost", port); 216 nm->connectSynchronizeable( *clientSync ); 147 217 /* adding some break for connection setup */ 148 218 SDL_Delay(20); … … 151 221 for( int i = 0; i < 3; i++) 152 222 { 153 nm->synchronize( );223 nm->synchronize( 1000 ); 154 224 /* simulate the network delay */ 155 225 SDL_Delay(50); … … 168 238 } 169 239 170 240 bool testCB( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) 241 { 242 printf("GOT MESSAGE: %s\n", data); 243 return true; 244 } 171 245 172 246 /** … … 178 252 int startServer(int argc, char** argv) 179 253 { 180 if( argc <= 2) 181 { 182 printf(" Wrong arguments try following notations:\n"); 183 printf(" --server [port number]\n"); 184 return 0; 185 } 186 187 int port = atoi(argv[2]); 188 printf("Starting Server on port %i\n", port); 254 255 256 State::setOnline(true); 257 SharedNetworkData::getInstance()->setGameServer( true ); 258 printf("Starting Server on port %i\n", 9999); 189 259 190 260 NetworkManager* netMan = NetworkManager::getInstance(); 191 Synchronizeable* ss = new SimpleSync("Server\0"); 192 193 netMan->createServer(/**ss, */port); 261 262 netMan->initialize(); 263 264 netMan->createServer(/**ss, */ 9999); 265 266 SimpleSync* ss = new SimpleSync("Server"); 267 ss->setSynchronized( true ); 268 269 NetworkLog::getInstance()->listen( 8888 ); 270 271 //MessageManager::getInstance()->initUser( 1 ); 272 MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL ); 273 194 274 SDL_Delay(20); 195 275 196 276 for(;;) 197 277 { 198 netMan->synchronize(); 199 SDL_Delay(1000); 278 MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"server to client", 18, RT_ALL, 0, MP_HIGHBANDWIDTH ); 279 netMan->synchronize( 100 ); 280 SDL_Delay(100); 281 ss->debug(); 200 282 } 201 283 … … 210 292 int startClient(int argc, char** argv) 211 293 { 294 295 296 State::setOnline(true); 297 NetworkManager* netMan = NetworkManager::getInstance(); 298 299 300 netMan->initialize(); 301 302 std::string host; 303 304 if ( argc > 2 ) 305 host = argv[2]; 306 else 307 host = "localhost"; 308 309 netMan->establishConnection(host, 9999); 310 311 // SimpleSync* ss = new SimpleSync("SimpleSync"); 312 // ss->setSynchronized( true ); 313 // netMan->connectSynchronizeable( *ss ); 314 315 NetworkLog::getInstance()->listen( 7777 ); 316 317 SimpleSync * ss = NULL; 318 319 //MessageManager::getInstance()->initUser( 0 ); 320 MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL ); 321 322 for(;;) 323 { 324 netMan->synchronize( 100 ); 325 MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"client to server", 18, RT_ALL, 0, MP_HIGHBANDWIDTH ); 326 ss = dynamic_cast<SimpleSync*>(ClassList::getObject( "Server", CL_SIMPLE_SYNC ) ); 327 SDL_Delay(100); 328 if (ss) 329 ss->debug(); 330 } 331 332 333 delete netMan; 334 delete ss; 335 336 return 0; 337 } 338 339 340 341 int startListen(int argc, char** argv) 342 { 212 343 if( argc < 3) 213 344 { 214 345 printf(" Wrong arguments try following notations:\n"); 215 printf(" -- client[server ip] [port number]\n");216 printf(" -- client[server name] [port number]\n");346 printf(" --listen [server ip] [port number]\n"); 347 printf(" --listen [server name] [port number]\n"); 217 348 return 0; 218 349 } … … 228 359 229 360 NetworkManager* netMan = NetworkManager::getInstance(); 230 Synchronizeable* ss = new SimpleSync("Client\0"); 231 232 netMan->establishConnection((const char*)"localhost", port/*,ip, *ss*/); 361 Synchronizeable* ss = new SimpleSync("SimpleSync\0"); 362 363 netMan->establishConnection( name, port ); 364 netMan->connectSynchronizeable( *ss ); 233 365 234 366 for(;;) 235 367 { 236 netMan->synchronize( );237 SDL_Delay( 500);368 netMan->synchronize( 1000 ); 369 SDL_Delay(10); 238 370 } 239 371 … … 245 377 } 246 378 247 248 249 int startListen(int argc, char** argv)250 {251 if( argc < 3)252 {253 printf(" Wrong arguments try following notations:\n");254 printf(" --listen [server ip] [port number]\n");255 printf(" --listen [server name] [port number]\n");256 return 0;257 }258 259 char* name = argv[2];260 int port = atoi(argv[3]);261 printf("Connecting to %s, on port %i\n", name, port);262 263 IPaddress ip;264 int error = SDLNet_ResolveHost(&ip, name, port);265 if(error == -1)266 printf("\n\nerror on address resolution, program inconsistancy\n\n");267 268 NetworkManager* netMan = NetworkManager::getInstance();269 Synchronizeable* ss = new ReadSync("WriteSync\0");270 271 netMan->establishConnection(ip, *ss);272 273 for(;;)274 {275 netMan->synchronize();276 SDL_Delay(10);277 }278 279 280 delete netMan;281 delete ss;282 283 return 0;284 }285 286 379 void testFloatConverter(float f) 287 380 { 381 #if 0 288 382 char* s = Converter::floatToBinString(f); 289 383 printf("%f = ", f); … … 293 387 printf("Byte Array: "); 294 388 for (int i = 0; i < 4; i++) 295 printf("%i ", res[i]);389 // printf("%i ", res[i]); 296 390 printf("\n"); 297 391 298 392 float b = Converter::byteArrayToFloat(res); 299 393 printf("ReConvert: %f \n", b); 394 #endif 300 395 } 301 396 302 397 void testFloatConverter2(float f) 303 398 { 399 #if 0 304 400 char* s = Converter::floatToBinString(f); 305 401 printf("### %f = ", f); … … 314 410 float b = Converter::_byteArrayToFloat(res); 315 411 printf("ReConvert: %f \n", b); 412 #endif 316 413 } 317 414 int converter(int argc, char** argv) … … 435 532 436 533 437 bool ShellBuffer::addBufferLineStatic(const char* line, ...)438 {534 //bool ShellBuffer::addBufferLineStatic(const char* line, ...) 535 //{ 439 536 //va_list arguments; 440 537 //vprintf(line, arguments); 441 printf("%s", line);442 }538 // printf("%s", line); 539 //} -
trunk/src/subprojects/network/simple_sync.cc
r6634 r7954 22 22 #include "simple_sync.h" 23 23 24 #include "class_id.h" 25 #include "fast_factory.h" 26 #include "lib/util/loading/factory.h" 27 24 28 #include "debug.h" 29 30 CREATE_FACTORY(SimpleSync, CL_SIMPLE_SYNC); 25 31 26 32 … … 28 34 * default constructor 29 35 */ 30 SimpleSync::SimpleSync(const char* name) 31 : Synchronizeable() 36 SimpleSync::SimpleSync( std::string name ) 32 37 { 33 /* define the local buffer size */ 34 this->outLength = 10; 35 this->recLength = 0; 36 this->inLength = 40; 37 this->outData = new byte[this->outLength]; 38 this->inData = new byte[this->inLength]; 39 40 /* init the buffer data */ 41 for( int i = 0; i < this->outLength; i++) 42 { 43 this->outData[i] = i; 44 } 45 for( int i = 0; i < this->inLength; i++) 46 { 47 this->inData[i] = 0; 48 } 49 38 setName( name ); 39 this->setClassID( CL_SIMPLE_SYNC, "SimpleSync" ); 40 in = 0; 41 out = 1; 42 syncStr = "hallo test test"; 43 id = this->registerVarId( new SynchronizeableInt( &in, &out, "var", PERMISSION_ALL ) ); 44 registerVar( new SynchronizeableString( &syncStr, &syncStr, "syncStr" ) ); 50 45 } 51 46 … … 56 51 SimpleSync::~SimpleSync() 57 52 { 58 if( this->outData) 59 delete[] this->outData; 60 if( this->inData) 61 delete[] this->inData; 53 } 54 55 void SimpleSync::debug( ) 56 { 57 printf("IN: %d OUT: %d\n", in, out); 58 printf("str: %s\n", syncStr.c_str()); 59 } 60 61 SimpleSync::SimpleSync( const TiXmlElement * root ) 62 { 63 setName( "" ); 64 this->setClassID( CL_SIMPLE_SYNC, "SimpleSync" ); 65 in = 0; 66 out = 1; 67 syncStr = "hallo test test"; 68 id = this->registerVarId( new SynchronizeableInt( &in, &out, "var", PERMISSION_ALL ) ); 69 registerVar( new SynchronizeableString( &syncStr, &syncStr, "syncStr" ) ); 62 70 } 63 71 64 72 65 /**66 * write data to Synchronizeable67 */68 int SimpleSync::writeBytes(const byte* data, int length, int sender)69 {70 PRINTF(0)("SimpleSync: got %i bytes of data\n", length);71 this->recLength = length;72 if(this->inLength < length)73 PRINTF(0)("ERROR: local buffer is smaller than the data to receive.\n");74 75 /* copy the data localy */76 for( int i = 0; i < length; i++)77 {78 this->inData[i] = data[i];79 }80 /* and debug output */81 this->writeDebug();82 }83 73 84 74 85 /**86 * read data from Synchronizeable87 */88 int SimpleSync::readBytes(byte* data, int maxLength, int * reciever)89 {90 PRINTF(0)("SimpleSync: sent %i bytes of data\n", this->outLength);91 92 /* debug msg */93 this->readDebug();94 95 /* write the test message */96 for( int i = 0; i < this->outLength; i++)97 data[i] = this->outData[i];98 99 /* return the length of the test */100 return this->outLength;101 }102 103 104 void SimpleSync::writeDebug() const105 {106 PRINTF(0)("Write in bytes: \t(0 <-) |");107 for(int i = 0; i < this->recLength; i++)108 {109 PRINT(0)(" [%u] ",this->inData[i]);110 }111 PRINT(0)("|\n");112 }113 114 115 void SimpleSync::readDebug() const116 {117 PRINTF(0)("Read out bytes: \t(0 ->) |");118 for(int i = 0; i < this->outLength; i++)119 {120 PRINT(0)(" [%u] ",this->outData[i]);121 }122 PRINT(0)("|\n");123 } -
trunk/src/subprojects/network/simple_sync.h
r6981 r7954 13 13 { 14 14 public: 15 SimpleSync(const char* name); 15 SimpleSync( std::string name); 16 SimpleSync(const TiXmlElement* root = NULL); 16 17 virtual ~SimpleSync(); 17 18 virtual int writeBytes(const byte* data, int length, int sender); 19 virtual int readBytes(byte* data, int maxLength, int * reciever); 18 19 void debug(); 20 20 21 21 22 22 private: 23 virtual void writeDebug() const; 24 virtual void readDebug() const; 25 26 27 private: 28 byte* inData; 29 int inLength; 30 int recLength; 31 byte* outData; 32 int outLength; 23 int in; 24 int out; 25 std::string syncStr; 26 int id; 33 27 }; 34 28
Note: See TracChangeset
for help on using the changeset viewer.