Changeset 7565 in orxonox.OLD for branches/network/src/lib/network/network_stream.cc
- Timestamp:
- May 10, 2006, 1:13:49 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/network_stream.cc
r7540 r7565 23 23 #include "base_object.h" 24 24 #include "network_protocol.h" 25 #include " tcp_socket.h"26 #include " tcp_server_socket.h"25 #include "udp_socket.h" 26 #include "udp_server_socket.h" 27 27 #include "connection_monitor.h" 28 28 #include "synchronizeable.h" 29 29 #include "network_game_manager.h" 30 30 #include "shared_network_data.h" 31 32 #include "lib/util/loading/factory.h" 31 33 32 34 #include "debug.h" … … 59 61 this->type = NET_CLIENT; 60 62 this->init(); 61 this-> networkSockets.push_back(new TcpSocket( host, port ));63 this->peers[0].socket = new UdpSocket( host, port ); 62 64 this->networkProtocol = new NetworkProtocol(); 63 65 this->connectionMonitor = new ConnectionMonitor(); 64 this->maxConnections = 1;65 } 66 67 68 NetworkStream::NetworkStream( int port )66 this->maxConnections = MAX_CONNECTIONS; 67 } 68 69 70 NetworkStream::NetworkStream( int port ) 69 71 { 70 72 this->type = NET_SERVER; 71 73 this->init(); 72 this->serverSocket = new TcpServerSocket(port);74 this->serverSocket = new UdpServerSocket(port); 73 75 this->networkProtocol = new NetworkProtocol(); 74 76 this->connectionMonitor = new ConnectionMonitor(); 75 this->networkSockets.push_back( NULL );76 this->networkSockets[0] = NULL; //TODO: remove this77 this->handshakes.push_back( NULL );78 77 this->bActive = true; 79 78 } … … 88 87 this->networkGameManager = NULL; 89 88 myHostId = 0; 89 currentState = 0; 90 90 } 91 91 … … 99 99 } 100 100 101 for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++) 102 { 103 if ( *i ) 104 { 105 (*i)->disconnectServer(); 106 } 107 } 108 109 for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++) 110 { 111 if ( *i ) 112 { 113 delete (*i); 114 } 101 for ( PeerList::iterator i = peers.begin(); i!=peers.end(); i++) 102 { 103 if ( i->second.socket ) 104 { 105 i->second.socket->disconnectServer(); 106 delete i->second.socket; 107 i->second.socket = NULL; 108 } 109 110 if ( i->second.handshake ) 111 { 112 delete i->second.handshake; 113 i->second.handshake = NULL; 114 } 115 } 116 117 if ( serverSocket ) 118 { 119 delete serverSocket; 120 serverSocket = NULL; 115 121 } 116 122 … … 126 132 // and one for handshake to reject client maxCon+1 127 133 this->networkGameManager->setUniqueID( this->maxConnections + 2 ); 128 //this->connectSynchronizeable( *(this->networkGameManager) ); 129 this->setMaxConnections( 10 ); 134 130 135 } 131 136 … … 135 140 Handshake* hs = new Handshake(false); 136 141 hs->setUniqueID( 0 ); 137 this->handshakes.push_back(hs); 142 assert( peers[0].handshake == NULL ); 143 peers[0].handshake = hs; 138 144 //this->connectSynchronizeable(*hs); 139 145 PRINTF(0)("NetworkStream: %s\n", hs->getName()); … … 146 152 sync.setNetworkStream( this ); 147 153 148 if( this->networkSockets.size()>0 ) 149 this->bActive = true; 154 this->bActive = true; 150 155 } 151 156 … … 158 163 this->synchronizeables.erase(disconnectSynchro); 159 164 160 if( this->networkSockets.size()<=0 ) 161 this->bActive = false; 165 this->bActive = false; 162 166 } 163 167 … … 165 169 void NetworkStream::processData() 166 170 { 171 currentState++; 172 167 173 if ( this->type == NET_SERVER ) 168 174 this->updateConnectionList(); 169 175 else 170 176 { 171 if ( networkSockets[0] && !networkSockets[0]->isOk() )177 if ( peers[0].socket && !peers[0].socket->isOk() ) 172 178 { 173 179 PRINTF(1)("lost connection to server\n"); 174 180 175 //delete networkSockets[i]; 176 networkSockets[0]->disconnectServer(); 177 networkSockets[0] = NULL; 178 179 if ( handshakes[0] ) 180 delete handshakes[0]; 181 handshakes[0] = NULL; 182 } 183 } 184 185 for (int i = 0; i<handshakes.size(); i++) 186 { 187 if ( handshakes[i] ) 188 { 189 if ( handshakes[i]->completed() ) 190 { 191 if ( handshakes[i]->ok() ) 192 { 193 if ( type != NET_SERVER ) 194 { 195 SharedNetworkData::getInstance()->setHostID( handshakes[i]->getHostId() ); 196 myHostId = SharedNetworkData::getInstance()->getHostID(); 197 198 this->networkGameManager = NetworkGameManager::getInstance(); 199 this->networkGameManager->setUniqueID( handshakes[i]->getNetworkGameManagerId() ); 200 //this->connectSynchronizeable( *(this->networkGameManager) ); 201 } 202 else 203 { 204 205 } 206 PRINT(0)("handshake finished id=%d\n", handshakes[i]->getNetworkGameManagerId()); 207 208 209 delete handshakes[i]; 210 handshakes[i] = NULL; 211 } 212 else 213 { 214 PRINT(1)("handshake failed!\n"); 215 networkSockets[i]->disconnectServer(); 216 delete handshakes[i]; 217 handshakes[i] = NULL; 218 //TODO: handle error 219 } 220 } 221 } 222 } 181 peers[0].socket->disconnectServer(); 182 delete peers[0].socket; 183 peers[0].socket = NULL; 184 185 if ( peers[0].handshake ) 186 delete peers[0].handshake; 187 peers[0].handshake = NULL; 188 } 189 } 190 191 223 192 224 193 225 194 /* DOWNSTREAM */ 226 195 #if 0 227 196 228 197 … … 341 310 { 342 311 #warning fix this 343 #if 0 312 344 313 if ( *it && (*it)->getUniqueID()==header.synchronizeableID ) 345 314 { … … 351 320 continue; 352 321 } 322 323 } 324 325 } while ( dataLength>0 ); 326 } 327 328 } 353 329 #endif 354 }355 356 } while ( dataLength>0 );357 }358 }359 330 } 360 331 … … 372 343 clientId = freeSocketSlots.back(); 373 344 freeSocketSlots.pop_back(); 374 networkSockets[clientId] = tempNetworkSocket; 375 handshakes[clientId] = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); 376 handshakes[clientId]->setUniqueID(clientId); 345 peers[clientId].socket = tempNetworkSocket; 346 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); 347 peers[clientId].handshake->setUniqueID(clientId); 348 peers[clientId].userId = clientId; 377 349 } else 378 350 { 379 clientId = networkSockets.size(); 380 networkSockets.push_back(tempNetworkSocket); 381 Handshake* tHs = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); 382 tHs->setUniqueID(clientId); 383 handshakes.push_back(tHs); 351 clientId = 0; 352 353 for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) 354 if ( it->first >= clientId ) 355 clientId = it->first + 1; 356 357 peers[clientId].socket = tempNetworkSocket; 358 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); 359 peers[clientId].handshake->setUniqueID(clientId); 360 peers[clientId].userId = clientId; 384 361 } 385 362 386 363 if ( clientId > this->maxConnections ) 387 364 { 388 handshakes[clientId]->doReject( "too many connections" );365 peers[clientId].handshake->doReject( "too many connections" ); 389 366 PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); 390 367 } … … 398 375 399 376 //check if connections are ok else remove them 400 for ( int i = 1; i<networkSockets.size(); i++) 401 { 402 if ( networkSockets[i] && !networkSockets[i]->isOk() ) 403 { 404 //TODO: tell EntityManager that this player left the game 405 PRINTF(0)("Client is gone: %d\n", i); 406 407 //delete networkSockets[i]; 408 networkSockets[i]->disconnectServer(); 409 networkSockets[i] = NULL; 410 411 if ( handshakes[i] ) 412 delete handshakes[i]; 413 handshakes[i] = NULL; 414 415 416 NetworkGameManager::getInstance()->signalLeftPlayer(i); 417 418 if ( i == networkSockets.size()-1 ) 419 { 420 networkSockets.pop_back(); 421 handshakes.pop_back(); 422 } 423 else 424 { 425 freeSocketSlots.push_back(i); 426 } 427 } 428 } 429 430 431 } 432 433 void NetworkStream::setMaxConnections( int n ) 434 { 435 if ( !this->isServer() ) 436 { 437 PRINTF(1)("Cannot set maxConnections because I am no server.\n"); 438 } 439 if ( this->networkSockets.size() > 1 ) 440 { 441 PRINTF(1)("Cannot set maxConnections because there are already %d connections.\n", this->networkSockets.size()); 442 return; 443 } 444 445 if ( n > MAX_CONNECTIONS ) 446 { 447 PRINTF(1)("Cannot set maxConnectiosn to %d because of hardcoded limit %d\n", n, MAX_CONNECTIONS); 448 return; 449 } 450 451 this->maxConnections = n; 452 this->networkGameManager->setUniqueID( n+2 ); 453 } 454 455 377 for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) 378 { 379 if ( it->second.socket && !it->second.socket->isOk() ) 380 { 381 PRINTF(0)("Client is gone: %d\n", it->second.userId); 382 383 it->second.socket->disconnectServer(); 384 delete it->second.socket; 385 it->second.socket = NULL; 386 387 if ( it->second.handshake ) 388 delete it->second.handshake; 389 it->second.handshake = NULL; 390 391 392 NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId); 393 394 freeSocketSlots.push_back( it->second.userId ); 395 396 } 397 } 398 399 400 } 456 401 457 402 void NetworkStream::debug() … … 485 430 } 486 431 487 488 489 490 491 432 /** 433 * check if handshakes completed 434 */ 435 void NetworkStream::handleHandshakes( ) 436 { 437 for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) 438 { 439 if ( it->second.handshake ) 440 { 441 if ( it->second.handshake->completed() ) 442 { 443 if ( it->second.handshake->ok() ) 444 { 445 if ( type != NET_SERVER ) 446 { 447 SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); 448 myHostId = SharedNetworkData::getInstance()->getHostID(); 449 450 this->networkGameManager = NetworkGameManager::getInstance(); 451 this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); 452 } 453 454 PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId()); 455 456 457 delete it->second.handshake; 458 it->second.handshake = NULL; 459 } 460 else 461 { 462 PRINT(1)("handshake failed!\n"); 463 it->second.socket->disconnectServer(); 464 } 465 } 466 } 467 } 468 } 469 470 /** 471 * handle upstream network traffic 472 */ 473 void NetworkStream::handleUpstream( ) 474 { 475 byte buf[UDP_PACKET_SIZE]; 476 int offset; 477 int n; 478 479 for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) 480 { 481 offset = INTSIZE; //make already space for length 482 483 if ( peer->second.socket ) 484 continue; 485 486 n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset ); 487 assert( n == INTSIZE ); 488 offset += n; 489 490 n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset ); 491 assert( n == INTSIZE ); 492 offset += n; 493 494 n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset ); 495 assert( n == INTSIZE ); 496 offset += n; 497 498 for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) 499 { 500 Synchronizeable & sync = **it; 501 502 assert( offset + INTSIZE <= UDP_PACKET_SIZE ); 503 504 n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset ); 505 assert( n == INTSIZE ); 506 offset += n; 507 508 offset += sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, 0 ); 509 } 510 511 assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE ); 512 513 assert( peer->second.socket->writePacket( buf, offset ) ); 514 } 515 } 516 517 /** 518 * handle downstream network traffic 519 */ 520 void NetworkStream::handleDownstream( ) 521 { 522 byte buf[UDP_PACKET_SIZE]; 523 int offset = 0; 524 525 int length = 0; 526 int packetLength = 0; 527 int uniqueId = 0; 528 int state = 0; 529 int ackedState = 0; 530 int fromState = 0; 531 532 for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) 533 { 534 packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE ); 535 536 assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE ); 537 assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE ); 538 assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); 539 assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); 540 541 //if this is an old state drop it 542 if ( state <= peer->second.lastRecvedState ) 543 continue; 544 545 if ( packetLength != length ) 546 { 547 PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length); 548 peer->second.socket->disconnectServer(); 549 continue; 550 } 551 552 offset = 4*INTSIZE; 553 554 while ( offset < length ) 555 { 556 assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE ); 557 offset += INTSIZE; 558 559 Synchronizeable * sync = NULL; 560 561 for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) 562 { 563 if ( (*it)->getUniqueID() == uniqueId ) 564 { 565 sync = *it; 566 break; 567 } 568 } 569 570 if ( sync == NULL ) 571 { 572 //TODO dont accept new object from all peers (probably only servers) 573 int leafClassId; 574 if ( INTSIZE > length - offset ) 575 break; 576 577 Converter::byteArrayToInt( buf + offset, &leafClassId ); 578 579 BaseObject * b; 580 /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ 581 /* Exception 1: NullParent */ 582 if( leafClassId == CL_NULL_PARENT) 583 { 584 PRINTF(1)("Can not create Class with ID %x!", (int)leafClassId); 585 break; 586 } 587 else 588 b = Factory::fabricate( (ClassID)leafClassId ); 589 590 if ( !b ) 591 { 592 PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId); 593 break; 594 } 595 596 if ( b->isA(CL_SYNCHRONIZEABLE) ) 597 { 598 sync = dynamic_cast<Synchronizeable*>(b); 599 sync->setUniqueID( uniqueId ); 600 sync->setSynchronized(true); 601 602 PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID()); 603 } 604 else 605 { 606 PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)leafClassId); 607 delete b; 608 break; 609 } 610 } 611 612 offset += sync->setStateDiff( peer->second.userId, buf+offset, length-offset, state, fromState ); 613 } 614 615 if ( offset != length ) 616 { 617 peer->second.socket->disconnectServer(); 618 } 619 620 peer->second.lastAckedState = ackedState; 621 } 622 } 623 624 625 626 627 628
Note: See TracChangeset
for help on using the changeset viewer.