Changeset 6634 in orxonox.OLD for trunk/src/lib/network
- Timestamp:
- Jan 21, 2006, 1:18:19 AM (19 years ago)
- Location:
- trunk/src/lib/network
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/converter.cc
r6341 r6634 22 22 /* include your own header */ 23 23 #include "converter.h" 24 #include "shell_command.h" 25 26 #include <limits> 27 28 SHELL_COMMAND_STATIC(debug, Converter, Converter::debug); 24 29 25 30 … … 182 187 const int expmult = 8388608; //2^23 183 188 189 float Converter::getDenormConst() 190 { 191 const int exp = 126; 192 float result = 1.0f; 193 for (int i = 0; i < exp; i++) 194 result /= 2.0f; 195 return result; 196 } 197 184 198 /*! 185 199 * Converts a float value into a byte-array … … 189 203 byte* Converter::floatToByteArray(float x) 190 204 { 205 byte* result = new byte[4]; 206 floatToByteArray(x, result, 4); 207 return result; 208 /* 191 209 int mantisse = 0; 192 210 int exponent = 128; … … 201 219 sgn = 1; 202 220 203 float sub = 1; 204 while (sub < x) 205 { 206 sub *= 2; 221 if (x == 0) 222 { 223 exponent = 255; 224 mantisse = 0; 225 } 226 else 227 { 228 //if (x < getDenormConst()) 229 // printf("Denormalisiert!\n"); 230 //printf("DenormConst = %e", getDenormConst()); 231 232 float sub = 1; 233 while (sub < x) 234 { 235 sub *= 2; 236 exponent++; 237 } 238 239 while (x > 0) 240 { 241 if (x >= sub) 242 { 243 mantisse += 1; 244 x -= sub; 245 } 246 247 mantisse *= 2; 248 exponent--; 249 sub /= 2; 250 } 207 251 exponent++; 208 } 209 210 while (x > 0) 211 { 212 if (x >= sub) 213 { 214 mantisse += 1; 215 x -= sub; 216 } 217 218 mantisse *= 2; 219 exponent--; 220 sub /= 2; 221 } 222 exponent++; 223 mantisse /= 2; 224 while (mantisse < expmult) 225 { 226 mantisse *= 2; 227 exponent--; 228 } 229 230 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 231 232 mantisse -= expmult; 252 mantisse /= 2; 253 254 printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent); 255 256 257 if (mantisse != 0) 258 { 259 while (mantisse < expmult) 260 { 261 mantisse *= 2; 262 exponent--; 263 } 264 265 mantisse -= expmult; 266 } 267 } 268 269 printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent); 270 233 271 234 272 int hx = mantisse + expmult * exponent; … … 238 276 239 277 return result; 278 */ 240 279 } 241 280 … … 248 287 float Converter::byteArrayToFloat(byte* a) 249 288 { 289 byte* h = new byte[4]; 290 float result = 0.0f; 291 byteArrayToFloat(a, &result); 292 return result; 293 /* 294 int hexp = a[2] + a[3] * 256; 295 int exponent = (hexp / 128) % 256; 296 250 297 int hmant = a[0] + a[1] * 256 + a[2] * 65536; 251 298 int mantisse = hmant % expmult; 299 if (mantisse == 0 && exponent == 255) 300 return 0; 301 252 302 mantisse += expmult; 253 254 int hexp = a[2] + a[3] * 256; 255 int exponent = (hexp / 128) % 256 - 128; 303 exponent -= 128; 304 256 305 257 306 int sgn; … … 261 310 sgn = 1; 262 311 263 //printf("mantisse = %i exponent = %i \n", mantisse, exponent);312 printf("ReConv: mantisse = %i exponent = %i \n", mantisse, exponent); 264 313 265 314 float emult = 1; … … 276 325 277 326 return result; 278 } 279 280 /*! 281 * Converts a float value into a byte-array 282 * @param x: The float which is to convert 283 * @return: A byte-array which accords the given float 284 */ 285 byte* Converter::_floatToByteArray(float x) 286 { 287 byte* p = (byte*)&x; 288 byte* result = new byte[4]; 289 for (int i = 0; i < 4; i++) 290 result[i] = p[i]; 291 return result; 292 } 293 294 295 /*! 296 * Converts a byte-array into a float value 297 * @param a: The byte-array which is to convert 298 * @return: A float value which accords the given byte-array 299 */ 300 float Converter::_byteArrayToFloat(byte* a) 301 { 302 float* p = (float*)a; 303 float result = *p; 304 return result; 327 */ 305 328 } 306 329 … … 310 333 * @return: A byte-array which accords the given float 311 334 */ 312 int Converter:: floatToByteArray(float x, byte* a, int length)335 int Converter::_floatToByteArray(float x, byte* a, int length) 313 336 { 314 337 if (length < FLOATSIZE) … … 319 342 320 343 //handle 0 else function will loop for ever 321 if ( x == 0 )344 /*if ( x == 0 ) 322 345 { 323 346 for ( int i = 0; i<FLOATSIZE; i++) 324 347 a[i] = 0; 325 348 return FLOATSIZE; 326 } 349 }*/ 327 350 328 351 int mantisse = 0; … … 338 361 sgn = 1; 339 362 340 float sub = 1; 341 while (sub < x) 342 { 343 sub *= 2; 363 if (x == 0) 364 { 365 exponent = 255; 366 mantisse = 0; 367 } 368 else 369 { 370 //if (x < getDenormConst()) 371 // printf("Denormalisiert!\n"); 372 //printf("DenormConst = %e", getDenormConst()); 373 374 float sub = 1; 375 while (sub < x) 376 { 377 sub *= 2; 378 exponent++; 379 } 380 381 while (x > 0) 382 { 383 if (x >= sub) 384 { 385 mantisse += 1; 386 x -= sub; 387 } 388 389 mantisse *= 2; 390 exponent--; 391 sub /= 2; 392 } 344 393 exponent++; 345 } 346 347 while (x > 0) 348 { 349 if (x >= sub) 350 { 351 mantisse += 1; 352 x -= sub; 353 } 354 355 mantisse *= 2; 356 exponent--; 357 sub /= 2; 358 } 359 exponent++; 360 mantisse /= 2; 361 while (mantisse < expmult) 362 { 363 mantisse *= 2; 364 exponent--; 365 } 366 367 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 368 369 mantisse -= expmult; 394 mantisse /= 2; 395 396 397 /// printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent); 398 399 400 if (mantisse != 0) 401 { 402 while (mantisse < expmult) 403 { 404 mantisse *= 2; 405 exponent--; 406 } 407 408 mantisse -= expmult; 409 } 410 } 411 412 /// printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent); 413 370 414 371 415 int hx = mantisse + expmult * exponent; … … 374 418 a[3] += sgnadd; 375 419 420 421 // int hx = mantisse + expmult * exponent; 422 // byte* result = intToByteArray(hx); 423 // if (sgn == -1) 424 // result[3] += sgnadd; 425 376 426 return result; 377 427 } … … 383 433 * @return: A float value which accords the given byte-array 384 434 */ 385 int Converter:: byteArrayToFloat(const byte* a, float* x)386 { 387 //handle 0388 for (int i = 0; i<FLOATSIZE; i++)435 int Converter::_byteArrayToFloat(const byte* a, float* x) 436 { 437 //handle 0 438 /*for (int i = 0; i<FLOATSIZE; i++) 389 439 { 390 440 if (a[i]!=0) … … 395 445 return FLOATSIZE; 396 446 } 397 } 398 447 }*/ 448 449 int hexp = a[2] + a[3] * 256; 450 int exponent = (hexp / 128) % 256; 399 451 400 452 int hmant = a[0] + a[1] * 256 + a[2] * 65536; 401 453 int mantisse = hmant % expmult; 454 455 //handle 0 456 if (mantisse == 0 && exponent == 255) 457 { 458 *x = 0; 459 return FLOATSIZE; 460 } 461 402 462 mantisse += expmult; 403 404 int hexp = a[2] + a[3] * 256; 405 int exponent = (hexp / 128) % 256 - 128; 463 exponent -= 128; 464 406 465 407 466 int sgn; … … 411 470 sgn = 1; 412 471 413 //printf("mantisse = %i exponent = %i \n", mantisse, exponent);472 /// printf("ReConv: mantisse = %i exponent = %i \n", mantisse, exponent); 414 473 415 474 float emult = 1; … … 421 480 emult /= 2; 422 481 482 /* 483 float result = mantisse * emult; 484 if (sgn == -1) 485 result = -result; 486 487 return result; 488 */ 489 423 490 *x = mantisse * emult; 424 491 if (sgn == -1) … … 426 493 427 494 return FLOATSIZE; 495 } 496 497 /*! 498 * Converts a float value into a byte-array 499 * @param x: The float which is to convert 500 * @return: A byte-array which accords the given float 501 */ 502 int Converter::floatToByteArray(float x, byte* a, int length) 503 { 504 if ( length<4 ) 505 { 506 PRINTF(1)("Byte Array to small\n"); 507 return 0; 508 } 509 byte* p = (byte*)&x; 510 511 for (int i = 0; i < 4; i++) 512 a[i] = p[i]; 513 return 4; 514 } 515 516 517 /*! 518 * Converts a byte-array into a float value 519 * @param a: The byte-array which is to convert 520 * @return: A float value which accords the given byte-array 521 */ 522 int Converter::byteArrayToFloat(const byte* a, float* x) 523 { 524 *x = *((float*)a); 525 526 return 4; 428 527 } 429 528 … … 504 603 } 505 604 605 606 607 608 void Converter::floatTest(float x) 609 { 610 //float x = 8.0f; 611 //float x = numeric_limits<float>::infinity(); 612 613 printf("To Convert: %e\n", x); 614 615 byte* res = floatToByteArray(x); 616 for (int i = 0; i < 4; i++) 617 printf("%i ", res[i]); 618 printf("\n"); 619 620 float y = byteArrayToFloat(res); 621 printf("ReConvert: %e\n", y); 622 623 if (x == y) 624 printf("equal\n"); 625 else 626 printf("different\n"); 627 } 628 629 void Converter::debug() 630 { 631 printf("We rulez\n"); 632 633 //Denormalized? 634 //floatTest(-9.87624e-38f); 635 //floatTest(-9.87624e-37f); 636 //floatTest(-9.87624e-36f); 637 //floatTest(-9.87624e-35f); 638 639 /* 640 floatTest(14.7f); 641 floatTest(12.07e15f); 642 floatTest(0.0f); 643 floatTest(5.67e-15f); 644 */ 645 646 //floatTest(-18.0098f); 647 //floatTest(-24.07e23f); 648 //floatTest(-0.0f); 649 //floatTest(-5.67e-29f); 650 floatTest(-45.7e-32f); 651 floatTest(45.7e-33f); 652 floatTest(-45.7e-34f); 653 floatTest(45.7e-35f); 654 } -
trunk/src/lib/network/converter.h
r6341 r6634 9 9 /* include this file, it contains some default definitions */ 10 10 #include "netdefs.h" 11 11 12 12 13 /* include base_object.h since all classes are derived from this one */ … … 21 22 * a class that can convert int to byte-array and vice versa 22 23 */ 23 class Converter : public BaseObject24 class Converter : public BaseObject 24 25 { 25 26 public: … … 43 44 static float byteArrayToFloat(byte* a); 44 45 45 static byte* _floatToByteArray(float x); 46 static float _byteArrayToFloat(byte* a); 46 static int _floatToByteArray(float x, byte* a, int length); 47 static int _byteArrayToFloat(const byte* a, float* x); 48 49 50 static void debug(); 51 static void floatTest(float x); 52 static float getDenormConst(); 53 54 47 55 private: 48 56 Converter(); … … 50 58 }; 51 59 60 #undef byte 61 52 62 #endif /*_CONVERTER*/ -
trunk/src/lib/network/network_game_manager.cc
r6498 r6634 52 52 this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager"); 53 53 54 allOutBuffer.length = 0;55 56 allOutBuffer.maxLength = 10*1024;57 58 allOutBuffer.buffer = new byte[10*1024];59 60 54 newUniqueID = MAX_CONNECTIONS + 2; 61 55 … … 74 68 } 75 69 76 if ( allOutBuffer.buffer )77 delete allOutBuffer.buffer;78 70 } 79 71 … … 107 99 if ( b == CREATE_ENTITY ) 108 100 { 101 PRINTF(0)("CREATE_ENTITY\n"); 109 102 if ( !handleCreateEntity( i, data, length, sender ) ) 110 103 return i; … … 137 130 } 138 131 132 if ( b == REQUEST_ENTITY_LIST ) 133 { 134 sendEntityList( sender ); 135 continue; 136 } 137 139 138 if ( b == REQUEST_SYNC ) 140 139 { 141 140 if ( !handleRequestSync( i, data, length, sender ) ) 142 141 return i; 143 continue;144 }145 146 if ( b == REQUEST_ENTITY_LIST )147 {148 sendEntityList( sender );149 142 continue; 150 143 } … … 188 181 } 189 182 190 *reciever = 0; 191 int nbytes = allOutBuffer.length; 192 allOutBuffer.length = 0; 193 194 if ( nbytes <=0 ) 195 return 0; 196 197 if ( nbytes > maxLength ) 198 { 199 PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.length\n", nbytes, maxLength); 200 return 0; 201 } 202 203 memcpy( data, allOutBuffer.buffer, nbytes ); 204 return nbytes; 183 return 0; 205 184 } 206 185 … … 221 200 int NetworkGameManager::createEntity( ClassID classID, int owner ) 222 201 { 223 224 202 if ( this->isServer() ) 225 203 { … … 254 232 return NULL; 255 233 } 256 newUniqueID++;257 234 258 235 BaseObject * b = Factory::fabricate( element ); … … 268 245 { 269 246 Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); 270 s->setUniqueID( newUniqueID );247 s->setUniqueID( newUniqueID++ ); 271 248 s->setOwner( 0 ); 272 249 this->networkStream->connectSynchronizeable( *s ); … … 313 290 void NetworkGameManager::requestCreateEntity(ClassID classID) 314 291 { 315 if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_CREATE ) ) 316 return; 317 if ( !writeToClientBuffer( allOutBuffer, (int)classID ) ) 318 return; 292 for ( int i = 0; i<outBuffer.size(); i++) 293 { 294 if ( !this->networkStream->isUserIdActive( i ) ) 295 continue; 296 297 if ( !writeToClientBuffer( outBuffer[i], (byte)REQUEST_CREATE ) ) 298 return; 299 if ( !writeToClientBuffer( outBuffer[i], (int)classID ) ) 300 return; 301 } 319 302 } 320 303 … … 325 308 void NetworkGameManager::requestRemoveEntity(int uniqueID) 326 309 { 327 if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_REMOVE ) ) 328 return; 329 if ( !writeToClientBuffer( allOutBuffer, uniqueID ) ) 330 return; 310 for ( int i = 0; i<outBuffer.size(); i++) 311 { 312 if ( !this->networkStream->isUserIdActive( i ) ) 313 continue; 314 315 if ( !writeToClientBuffer( outBuffer[i], (byte)REQUEST_REMOVE ) ) 316 return; 317 if ( !writeToClientBuffer( outBuffer[i], uniqueID ) ) 318 return; 319 } 331 320 } 332 321 … … 338 327 int NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner) 339 328 { 340 if ( !writeToClientBuffer( allOutBuffer, (byte)CREATE_ENTITY ) ) 341 return -1; 342 if ( !writeToClientBuffer( allOutBuffer, (int)classID ) ) 343 return -1; 344 if ( !writeToClientBuffer( allOutBuffer, uniqueID ) ) 345 return -1; 346 if ( !writeToClientBuffer( allOutBuffer, owner ) ) 347 return -1; 329 for ( int i = 0; i<outBuffer.size(); i++) 330 { 331 if ( !this->networkStream->isUserIdActive( i ) ) 332 continue; 333 334 if ( !writeToClientBuffer( outBuffer[i], (byte)CREATE_ENTITY ) ) 335 return -1; 336 if ( !writeToClientBuffer( outBuffer[i], (int)classID ) ) 337 return -1; 338 if ( !writeToClientBuffer( outBuffer[i], uniqueID ) ) 339 return -1; 340 if ( !writeToClientBuffer( outBuffer[i], owner ) ) 341 return -1; 342 } 348 343 349 344 doCreateEntity( classID, uniqueID, owner ); … … 359 354 void NetworkGameManager::executeRemoveEntity(int uniqueID) 360 355 { 361 if ( !writeToClientBuffer( allOutBuffer, (byte)REMOVE_ENTITY ) ) 362 return; 363 if ( !writeToClientBuffer( allOutBuffer, uniqueID ) ) 364 return; 356 for ( int i = 0; i<outBuffer.size(); i++) 357 { 358 if ( !this->networkStream->isUserIdActive( i ) ) 359 continue; 360 361 if ( !writeToClientBuffer( outBuffer[i], (byte)REMOVE_ENTITY ) ) 362 return; 363 if ( !writeToClientBuffer( outBuffer[i], uniqueID ) ) 364 return; 365 } 365 366 366 367 doRemoveEntity(uniqueID); … … 406 407 while ( it != e ) 407 408 { 408 409 PRINTF(5)("SENDING ENTITY %s id %d\n", (*it)->getClassName(), (*it)->getUniqueID() ); 409 410 if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) ) 410 411 return; … … 426 427 bool NetworkGameManager::signalNewPlayer(int userId) 427 428 { 429 if ( userId >= outBuffer.size() ) 430 resizeBufferVector( userId ); 428 431 429 432 /* create new playable for Player*/ … … 483 486 484 487 //HACK: hack to prevent collision 485 if ( b->isA(CL_WORLD_ENTITY) )488 if ( b->isA(CL_WORLD_ENTITY) && !b->isA(CL_PLAYABLE) ) 486 489 { 487 490 if ( NetworkManager::getInstance()->getHostID()!=0 ) … … 489 492 static Vector pos = Vector(1000.0, 1000.0, 1000.0); 490 493 PNode *p = dynamic_cast<PNode*>(b); 491 p->set RelCoor(pos);492 p->updateNode(0);494 p->setAbsCoor(pos); 495 //p->updateNode(0); 493 496 pos += Vector(1000.0, 1000.0, 1000.0); 494 497 } … … 596 599 597 600 Playable *p = NULL; 601 Synchronizeable *s = NULL; 598 602 599 603 for ( ; it !=networkStream->getSyncEnd(); it++ ) … … 601 605 if ( (*it)->getUniqueID()==uniqueID ) 602 606 { 607 if ( (*it)->isA( CL_SYNCHRONIZEABLE ) ) 608 { 609 s = dynamic_cast<Synchronizeable*>(*it); 610 } 603 611 if ( (*it)->isA( CL_PLAYABLE ) ) 604 612 { … … 614 622 Player* player = State::getPlayer(); 615 623 assert(p != NULL); 624 assert(s != NULL); 616 625 assert(player != NULL); 626 627 s->setIsOutOfSync( true ); 628 629 PRINTF(0)("uniqueID = %d\n", s->getUniqueID()); 617 630 618 631 player->setControllable(p); … … 871 884 void NetworkGameManager::sync( int uniqueID, int owner ) 872 885 { 873 if ( owner==this->getHostID() )874 return;886 /*if ( owner==this->getHostID() ) 887 return;*/ 875 888 876 889 if ( !isServer() ) -
trunk/src/lib/network/network_game_manager.h
r6498 r6634 122 122 private: 123 123 std::vector<clientBuffer> outBuffer; 124 clientBuffer allOutBuffer;124 //clientBuffer allOutBuffer; 125 125 static NetworkGameManager* singletonRef; 126 126 -
trunk/src/lib/network/network_socket.cc
r6341 r6634 398 398 bool NetworkSocket::writePacket( byte * data, int length ) 399 399 { 400 PRINTF(5)("NetworkSocket::writePacket()\n"); 400 PRINTF(5)("NetworkSocket::writePacket() size=%d\n", length); 401 402 if ( length > 1024 ) 403 PRINTF(2)("WARNING SENDING BIG PACKET SIZE = %d\n", length); 401 404 402 405 byte blen[INTSIZE]; -
trunk/src/lib/network/network_stream.cc
r6498 r6634 78 78 this->connectionMonitor = new ConnectionMonitor(); 79 79 this->networkSockets.push_back( NULL ); 80 this->networkSockets[0] = NULL; //TODO: remove this 80 81 this->handshakes.push_back( NULL ); 81 82 this->bActive = true; … … 266 267 if ( networkSockets[i] != NULL ) 267 268 { 268 PRINTF(5)("write %d bytes to socket %d\n", dataLength, reciever);269 PRINTF(5)("write %d bytes to socket %d\n", dataLength, i); 269 270 networkSockets[i]->writePacket(downBuffer, dataLength); 270 271 } … … 310 311 if ( (*it)->writeBytes(upBuffer+sizeof(header), dataLength, i) != header.length ) 311 312 { 312 PRINTF(1)("%s did not read all the data !\n", (*it)->getClassName());313 PRINTF(1)("%s did not read all the data id = %d!\n", (*it)->getClassName(), (*it)->getUniqueID()); 313 314 break; 314 315 } -
trunk/src/lib/network/network_stream.h
r6341 r6634 55 55 inline int getSyncCount(){ return synchronizeables.size(); } 56 56 57 inline bool isUserIdActive( int userID ) { if (userID>=networkSockets.size()) return false; else return networkSockets[userID]!=NULL; } 58 57 59 private: 58 60 NetworkProtocol* networkProtocol;
Note: See TracChangeset
for help on using the changeset viewer.