Changeset 7565 in orxonox.OLD for branches/network/src/lib
- Timestamp:
- May 10, 2006, 1:13:49 PM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/handshake.cc
r7444 r7565 41 41 error_handler = registerVarId( new SynchronizeableInt( &localState.error, &remoteState.error, "error" ) ); 42 42 errorString_handler = registerVarId( new SynchronizeableString( &localState.errorString, &remoteState.errorString, "errorString" ) ); 43 43 44 localState.completed = false; 45 localState.error = 0; 46 localState.errorString = ""; 47 localState.hostId = clientId; 48 localState.networkManagerId = networkGameManagerId; 49 localState.orxId = _ORXONOX_ID; 50 localState.version = _ORXONOX_VERSION; 51 52 remoteState.completed = false; 53 remoteState.error = 0; 54 remoteState.errorString = ""; 55 remoteState.hostId = -1; 56 remoteState.networkManagerId = -1; 57 remoteState.orxId = 0; 58 remoteState.version = 0; 44 59 45 60 this->setSynchronized(true); … … 53 68 void Handshake::varChangeHandler( std::list< int > & id ) 54 69 { 70 for ( std::list<int>::iterator it = id.begin(); it != id.end(); it++ ) 71 { 72 if ( *it == orxId_handler ) 73 { 74 if ( remoteState.orxId != _ORXONOX_ID ) 75 { 76 localState.error = 1; 77 localState.completed = true; 78 localState.errorString = "Seems not to be orxonox!"; 79 return; 80 } 81 } 82 83 if ( *it == version_handler ) 84 { 85 if ( remoteState.version != _ORXONOX_VERSION ) 86 { 87 localState.error = 2; 88 localState.completed = true; 89 localState.errorString = "Versions of server and client do not match!"; 90 return; 91 } 92 } 93 94 } 55 95 } 56 96 -
branches/network/src/lib/network/handshake.h
r7444 r7565 10 10 #include "synchronizeable.h" 11 11 12 #define _ INITIAL_DATA0xF91337A012 #define _ORXONOX_ID 0xF91337A0 13 13 14 14 #define _ORXONOX_VERSION 1 -
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 -
branches/network/src/lib/network/network_stream.h
r7540 r7565 9 9 #include <vector> 10 10 #include <list> 11 #include <map> 11 12 12 13 #include "data_stream.h" … … 22 23 class NetworkGameManager; 23 24 25 class PeerInfo 26 { 27 public: 28 PeerInfo() { clear(); } 29 void clear() { userId = 0; isServer = false; socket = NULL; handshake = NULL; lastAckedState = 0; lastRecvedState = 0; } 30 int userId; 31 bool isServer; 32 NetworkSocket * socket; 33 Handshake * handshake; 34 int lastAckedState; 35 int lastRecvedState; 36 }; 37 24 38 typedef std::list<Synchronizeable*> SynchronizeableList; 25 typedef std::vector<NetworkSocket*> NetworkSocketVector; 26 typedef std::vector<Handshake*> HandshakeVector; 39 typedef std::map<int,PeerInfo> PeerList; 27 40 28 41 … … 48 61 49 62 inline int getMaxConnections(){ return maxConnections; } 50 void setMaxConnections( int n );51 63 52 64 virtual void processData(); … … 56 68 int getSyncCount(); 57 69 58 inline bool isUserIdActive( int userID ) { if (userID>=networkSockets.size()) return false; else return networkSockets[userID]!=NULL; }70 inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); } 59 71 60 72 void debug(); 73 74 inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; } 61 75 62 76 63 77 private: 64 78 void updateConnectionList(); 79 void handleHandshakes(); 80 void handleUpstream(); 81 void handleDownstream(); 65 82 66 83 … … 69 86 ConnectionMonitor* connectionMonitor; 70 87 SynchronizeableList synchronizeables; 71 NetworkSocketVector networkSockets; 72 HandshakeVector handshakes; 88 PeerList peers; 73 89 ServerSocket* serverSocket; 74 90 int type; … … 79 95 int myHostId; 80 96 int maxConnections; 97 98 int currentState; 81 99 82 100 NetworkGameManager* networkGameManager; -
branches/network/src/lib/network/server_socket.h
r7540 r7565 27 27 virtual NetworkSocket* getNewSocket( void ) = 0; 28 28 virtual void close() = 0; 29 virtual void update() = 0; 29 30 virtual bool isOk() { return this->bOk; }; 30 31 -
branches/network/src/lib/network/synchronizeable.cc
r7559 r7565 41 41 this->networkStream = NULL; 42 42 this->bSynchronize = false; 43 this->mLeafClassId = getLeafClassID(); 43 44 44 45 if( State::isOnline()) … … 50 51 } 51 52 53 /* make sure loadClassId is first synced var because this is read by networkStream */ 54 assert( syncVarList.size() == 0 ); 55 this->registerVar( new SynchronizeableInt( &this->mLeafClassId, &this->mLeafClassId, "leafClassId" ) ); 56 57 this->registerVar( new SynchronizeableInt( &this->owner, &this->owner, "owner" ) ); 52 58 this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) ); 53 59 } … … 175 181 i += n; 176 182 } 183 else if ( ! ( 184 this->isServer() && (*it)->checkPremission( PERMISSION_SERVER ) || 185 this->owner == this->hostID && (*it)->checkPremission( PERMISSION_OWNER ) || 186 (*it)->checkPremission( PERMISSION_ALL ) 187 ) ) 188 { 189 for ( int j = 0; j < (*it)->getSize(); it++ ) 190 { 191 stateTo->data[i] = 0; 192 i++; 193 } 194 } 177 195 else 178 196 { … … 212 230 * @param fromStateId id of the base state id 213 231 * @return true on success 232 * @todo check for permissions 214 233 */ 215 234 bool Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ) … … 264 283 stateFrom = (*it); 265 284 285 std::list<int> changes; 286 266 287 //apply diff 267 288 for ( int i = 0; i<length; i++ ) … … 271 292 else 272 293 stateTo->data[i] = data[i]; 294 295 if ( data[i] != 0 ) 296 changes.push_back(i); 273 297 } 274 298 … … 282 306 i += (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i ); 283 307 } 308 309 this->varChangeHandler( changes ); 284 310 285 311 assert( i == length -1 ); -
branches/network/src/lib/network/synchronizeable.h
r7559 r7565 41 41 42 42 enum { 43 PERMISSION_OWNER = 1, 44 PERMISSION_ALL = 2 43 PERMISSION_SERVER = 1, 44 PERMISSION_OWNER = 2, 45 PERMISSION_ALL = 4 45 46 }; 46 47 … … 89 90 private: 90 91 int uniqueID; //!< unique id assigned to synchronizeable 92 int mLeafClassId; //!< store leafClassId to send via states 91 93 int owner; //!< hostId of owner ( 0 if none / server ) 92 94 int hostID; //!< my own host id -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_quaternion.cc
r7559 r7565 87 87 n += res; 88 88 89 res += Converter::byteArrayToFloat( buf + n, & x);89 res += Converter::byteArrayToFloat( buf + n, &y ); 90 90 assert( res > 0 ); 91 91 n += res; 92 92 93 res += Converter::byteArrayToFloat( buf + n, & x);93 res += Converter::byteArrayToFloat( buf + n, &z ); 94 94 assert( res > 0 ); 95 95 n += res; -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_vector.cc
r7559 r7565 84 84 n += res; 85 85 86 res += Converter::byteArrayToFloat( buf + n, & x);86 res += Converter::byteArrayToFloat( buf + n, &y ); 87 87 assert( res > 0 ); 88 88 n += res; 89 89 90 res += Converter::byteArrayToFloat( buf + n, & x);90 res += Converter::byteArrayToFloat( buf + n, &z ); 91 91 assert( res > 0 ); 92 92 n += res; -
branches/network/src/lib/network/tcp_server_socket.h
r7541 r7565 27 27 virtual NetworkSocket* getNewSocket( void ); 28 28 virtual void close(); 29 virtual void update() {}; 29 30 30 31 private: -
branches/network/src/lib/network/udp_server_socket.cc
r7556 r7565 72 72 73 73 /** 74 * get newly connected socket. note: this function will also recieve 75 * packets and create new sockets 74 * get newly connected socket. note 76 75 * @return new socket or NULL if no new socket exists 77 76 */ 78 77 NetworkSocket * UdpServerSocket::getNewSocket( void ) 78 { 79 NetworkSocket * result = NULL; 80 81 if ( newSocketList.size() > 0 ) 82 { 83 result = newSocketList.front(); 84 85 newSocketList.pop_front(); 86 } 87 88 return result; 89 } 90 91 /** 92 * stop listening on server 93 */ 94 void UdpServerSocket::close( ) 95 { 96 97 for ( int i = 0; i < packetBuffer.size(); i++ ) 98 removeUserPackets( i ); 99 100 packetBuffer.clear(); 101 userList.clear(); 102 103 SDLNet_UDP_Close( socket ); 104 socket = NULL; 105 } 106 107 /** 108 * clean up users buffer 109 * @param userId users userid 110 */ 111 void UdpServerSocket::removeUserPackets( int userId ) 112 { 113 if ( userId >= packetBuffer.size() ) 114 return; 115 116 for ( NetworkPacketList::iterator it = packetBuffer[userId].begin(); it!=packetBuffer[userId].end(); it++ ) 117 { 118 if ( it->data ) 119 { 120 free( it->data ); 121 it->data = NULL; 122 } 123 } 124 125 packetBuffer[userId].clear(); 126 } 127 128 /** 129 * get next packet for user 130 * @param userId user id 131 * @return recieved packet or packet with length 0 if no packet available 132 */ 133 NetworkPacket UdpServerSocket::getPacket( int userId ) 134 { 135 NetworkPacket res; 136 res.data = NULL; 137 res.length = 0; 138 139 if ( packetBuffer.size() > userId && packetBuffer[userId].size() > 0 ) 140 { 141 res.data = packetBuffer[userId].front().data; 142 res.length = packetBuffer[userId].front().length; 143 packetBuffer[userId].pop_front(); 144 } 145 146 return res; 147 } 148 149 /** 150 * get number of packets recieved for user 151 * @param userId user id 152 * @return number of packets in buffer 153 */ 154 int UdpServerSocket::getPacketCount( int userId ) 155 { 156 if ( userId >= packetBuffer.size() ) 157 return -1; 158 159 return packetBuffer[userId].size(); 160 } 161 162 /** 163 * will set user state 164 * @param userId users id 165 * @param ip users host / port 166 */ 167 void UdpServerSocket::initUser( int userId, IPaddress ip ) 168 { 169 int channel = SDLNet_UDP_Bind( socket, userId, &ip ); 170 if( channel != userId ) 171 { 172 PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError()); 173 assert(false); 174 return; 175 } 176 177 if ( userId < packetBuffer.size() ) 178 removeUserPackets( userId ); 179 180 if ( packetBuffer.size() <= userId ) 181 packetBuffer.resize( userId + 1 ); 182 183 if ( userList.size() <= userId ) 184 userList.resize( userId + 1 ); 185 186 userList[ userId ] = ip; 187 } 188 189 /** 190 * remove user from list 191 * @param userId user id 192 */ 193 void UdpServerSocket::removeUser( int userId ) 194 { 195 removeUserPackets( userId ); 196 197 if ( userId >= userList.size() ) 198 return; 199 200 userList[userId].host = 0; 201 userList[userId].port = 0; 202 203 SDLNet_UDP_Unbind( socket, userId ); 204 } 205 206 /** 207 * send one packet to client associated to userId 208 * @param networkPacket packet to send 209 * @param userId users id 210 * @return true on success 211 */ 212 bool UdpServerSocket::sendPacket( NetworkPacket networkPacket , int userId ) 213 { 214 assert( networkPacket.length <= UDP_PACKET_SIZE ); 215 216 memcpy( packet->data, networkPacket.data, networkPacket.length ); 217 packet->len = networkPacket.length; 218 packet->channel = -1; 219 220 if ( SDLNet_UDP_Send( socket, userId, packet ) == 0 ) 221 { 222 PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError()); 223 return false; 224 } 225 226 return true; 227 } 228 229 /** 230 * do periodically things 231 */ 232 void UdpServerSocket::update( ) 79 233 { 80 234 int res; … … 84 238 { 85 239 int userId; 240 bool isNewConnection = false; 86 241 87 242 for ( userId =0; userId < userList.size(); userId++ ) … … 92 247 { 93 248 newConn++; 249 isNewConnection = true; 94 250 95 251 if ( newConn > MAX_NEW_CONNECTIONS ) … … 118 274 } 119 275 else 276 { 277 if ( isNewConnection ) 278 continue; 279 120 280 networkPacket.data = NULL; 281 } 121 282 memcpy( networkPacket.data, packet->data, packet->len ); 122 283 packetBuffer[userId].push_back( networkPacket ); … … 124 285 125 286 assert( res == 0 ); 126 127 NetworkSocket * result = NULL; 128 129 if ( newSocketList.size() > 0 ) 130 { 131 result = newSocketList.front(); 132 133 newSocketList.pop_front(); 134 } 135 136 return result; 137 } 138 139 /** 140 * stop listening on server 141 */ 142 void UdpServerSocket::close( ) 143 { 144 145 for ( int i = 0; i < packetBuffer.size(); i++ ) 146 removeUserPackets( i ); 147 148 packetBuffer.clear(); 149 userList.clear(); 150 151 SDLNet_UDP_Close( socket ); 152 socket = NULL; 153 } 154 155 /** 156 * clean up users buffer 157 * @param userId users userid 158 */ 159 void UdpServerSocket::removeUserPackets( int userId ) 160 { 161 if ( userId >= packetBuffer.size() ) 162 return; 163 164 for ( NetworkPacketList::iterator it = packetBuffer[userId].begin(); it!=packetBuffer[userId].end(); it++ ) 165 { 166 if ( it->data ) 167 { 168 free( it->data ); 169 it->data = NULL; 170 } 171 } 172 173 packetBuffer[userId].clear(); 174 } 175 176 /** 177 * get next packet for user 178 * @param userId user id 179 * @return recieved packet or packet with length 0 if no packet available 180 */ 181 NetworkPacket UdpServerSocket::getPacket( int userId ) 182 { 183 NetworkPacket res; 184 res.data = NULL; 185 res.length = 0; 186 187 if ( packetBuffer.size() > userId && packetBuffer[userId].size() > 0 ) 188 { 189 res.data = packetBuffer[userId].front().data; 190 res.length = packetBuffer[userId].front().length; 191 packetBuffer[userId].pop_front(); 192 } 193 194 return res; 195 } 196 197 /** 198 * get number of packets recieved for user 199 * @param userId user id 200 * @return number of packets in buffer 201 */ 202 int UdpServerSocket::getPacketCount( int userId ) 203 { 204 if ( userId >= packetBuffer.size() ) 205 return -1; 206 207 return packetBuffer[userId].size(); 208 } 209 210 /** 211 * will set user state 212 * @param userId users id 213 * @param ip users host / port 214 */ 215 void UdpServerSocket::initUser( int userId, IPaddress ip ) 216 { 217 int channel = SDLNet_UDP_Bind( socket, userId, &ip ); 218 if( channel != userId ) 219 { 220 PRINTF(1)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError()); 221 assert(false); 222 return; 223 } 224 225 if ( userId < packetBuffer.size() ) 226 removeUserPackets( userId ); 227 228 if ( packetBuffer.size() <= userId ) 229 packetBuffer.resize( userId + 1 ); 230 231 if ( userList.size() <= userId ) 232 userList.resize( userId + 1 ); 233 234 userList[ userId ] = ip; 235 } 236 237 /** 238 * remove user from list 239 * @param userId user id 240 */ 241 void UdpServerSocket::removeUser( int userId ) 242 { 243 removeUserPackets( userId ); 244 245 if ( userId >= userList.size() ) 246 return; 247 248 userList[userId].host = 0; 249 userList[userId].port = 0; 250 251 SDLNet_UDP_Unbind( socket, userId ); 252 } 253 254 /** 255 * send one packet to client associated to userId 256 * @param networkPacket packet to send 257 * @param userId users id 258 * @return true on success 259 */ 260 bool UdpServerSocket::sendPacket( NetworkPacket networkPacket , int userId ) 261 { 262 assert( networkPacket.length <= UDP_PACKET_SIZE ); 263 264 memcpy( packet->data, networkPacket.data, networkPacket.length ); 265 packet->len = networkPacket.length; 266 packet->channel = -1; 267 268 if ( SDLNet_UDP_Send( socket, userId, packet ) == 0 ) 269 { 270 PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError()); 271 return false; 272 } 273 274 return true; 275 } 287 } -
branches/network/src/lib/network/udp_server_socket.h
r7540 r7565 50 50 virtual void close(); 51 51 52 virtual void update(); 53 52 54 void removeUserPackets( int userId ); 53 55 void removeUser( int userId ); -
branches/network/src/lib/network/udp_socket.cc
r7556 r7565 121 121 return; 122 122 } 123 124 writePacket( NULL, 0 ); 123 125 } 124 126 … … 130 132 SDLNet_UDP_Unbind( socket, -1 ); 131 133 SDLNet_UDP_Close( socket ); 134 bOk = false; 132 135 socket = NULL; 133 136 } … … 180 183 int UdpSocket::readPacket( byte * data, int maxLength ) 181 184 { 185 assert( maxLength <= UDP_PACKET_SIZE ); 186 182 187 if ( serverSocket ) 183 188 {
Note: See TracChangeset
for help on using the changeset viewer.