Changeset 7767 in orxonox.OLD for branches/network/src/lib
- Timestamp:
- May 23, 2006, 11:40:29 AM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/connection_monitor.cc
r5822 r7767 10 10 11 11 ### File Specific: 12 main-programmer: Silvan Nellen12 main-programmer: Christoph Renner 13 13 co-programmer: ... 14 14 */ 15 15 16 16 #include "connection_monitor.h" 17 #include "network_log.h" 18 17 19 #include <debug.h> 18 20 #include <SDL/SDL.h> … … 22 24 using namespace std; 23 25 24 ConnectionMonitor::ConnectionMonitor( )26 ConnectionMonitor::ConnectionMonitor( int userId ) 25 27 { 26 27 28 /* set the class id for the base object and add ist to class list*/ 28 29 this->setClassID(CL_CONNECTION_MONITOR, "ConnectionMonitor"); 30 31 this->userId = userId; 32 this->ping = 0; 33 this->incomingUnzippedBandWidth = 0; 34 this->outgoingUnzippedBandWidth = 0; 35 this->nIncomingPackets = 0; 36 this->nOutgoingPackets = 0; 37 38 this->lastPacketTick = 0; 39 } 29 40 30 /*initialize variables*/ 41 ConnectionMonitor::~ConnectionMonitor( ) 42 { 43 } 31 44 32 /*Data of the lifetime of the ConnectionMonitor Object*/ 33 packetToAverage = 100; 34 protocollType = "default(TCP)"; 45 void ConnectionMonitor::processUnzippedOutgoingPacket( byte * data, int length, int stateId ) 46 { 47 int tick = SDL_GetTicks(); 48 49 nOutgoingPackets++; 50 51 // for ping calculation 52 sentStateTicks[stateId] = tick; 53 54 // calculate bandwidth 55 outgoingUnzippedPacketHistory[tick] = length; 56 outgoingUnzippedBandWidth = calculateBandWidth( outgoingUnzippedPacketHistory, tick ); 57 58 NETPRINTF(n)("UPSTREAM: user: %d bandwidth %f\n", userId, outgoingUnzippedBandWidth ); 59 60 // count zero bytes 61 int nZeroBytes = 0; 62 63 for ( int i = 0; i < length; i++ ) 64 if ( data[i] == '\0' ) 65 nZeroBytes++; 66 67 NETPRINTF(n)( "ZEROBYTES: %d (%f%%)\n", nZeroBytes, ((float)100)*nZeroBytes/length ); 68 } 35 69 36 startTime= SDL_GetTicks(); 37 totalReceivedPackets=0; 38 averageDatarate=0; 39 totalLostPackets=0; 40 totalPacketloss=0; 70 void ConnectionMonitor::processUnzippedIncomingPacket( byte * data, int length, int stateId, int ackedState ) 71 { 72 int tick = SDL_GetTicks(); 73 74 nIncomingPackets++; 75 76 lastPacketTick = tick; 77 78 // calculate ping 79 if ( sentStateTicks.find( ackedState ) != sentStateTicks.end() ) 80 { 81 ackDelay.push_back( tick - sentStateTicks[ackedState] ); 82 } 83 84 while ( sentStateTicks.begin()->first <= ackedState ) 85 sentStateTicks.erase( sentStateTicks.begin() ); 86 87 while ( ackDelay.size() > N_PACKETS_FOR_PING ) 88 ackDelay.erase( ackDelay.begin() ); 89 90 ping = 0; 91 92 for ( std::list<int>::iterator it = ackDelay.begin(); it != ackDelay.end(); it++ ) 93 ping += *it; 94 95 if ( ackDelay.size() == 0 ) 96 ping = -1; 97 else 98 ping /= ackDelay.size(); 99 100 NETPRINTF(n)("PING: user: %d ping: %d\n", userId, ping ); 101 102 // calculate bandwidth 103 incomingUnzippedPacketHistory[tick] = length; 104 incomingUnzippedBandWidth = calculateBandWidth( incomingUnzippedPacketHistory, tick ); 105 106 NETPRINTF(n)("DOWNSTREAM: user: %d bandwidth %f\n", userId, incomingUnzippedBandWidth ); 107 108 } 41 109 42 /*Data of the current packet*/ 43 currentPacketID=0; 44 currentPacketTick=0; 45 lastPacketID=0; 46 lastPacketTick=0; 47 currentDelay=0; 48 49 /*Data of the last n packets (n is specified by paxketsToAverage)*/ 50 sizeOfLastFewPackets=0; 51 currentDatarate=0; 52 lastFewDelays = new unsigned int [packetToAverage]; 53 lastFewPackets = new byte* [packetToAverage]; 54 packetCounter=0; 55 56 110 float ConnectionMonitor::calculateBandWidth( std::map< int, int > packetHistory, int tick ) 111 { 112 // delete old packets 113 while ( packetHistory.begin()->first < tick - MSECS_TO_CALC_BWIDTH ) 114 packetHistory.erase( packetHistory.begin() ); 115 116 float res = 0.0f; 117 118 for ( std::map<int,int>::iterator it = packetHistory.begin(); it != packetHistory.end(); it++ ) 119 { 120 res += it->second; 121 } 122 123 if ( packetHistory.size() <= 1 || tick - packetHistory.begin()->first == 0 ) 124 res = 0; 125 else 126 res /= (float)((tick - packetHistory.begin()->first)*( 1 + 1/((float)(packetHistory.size()-1)) )); 127 128 res *= 1000; 129 130 return res; 57 131 } 58 132 59 133 60 ConnectionMonitor::~ConnectionMonitor()61 {62 63 64 }65 66 67 68 void ConnectionMonitor::processPacket(byte* currentPacket, unsigned int packetLength)69 {70 /*Process the current Packet*/71 currentPacketTick = SDL_GetTicks();72 currentDelay = currentPacketTick - lastPacketTick;73 74 /*Do whats needed for Averaging*/75 76 if(packetCounter == packetToAverage)77 {78 computeCurrentDatarate();79 displayStatistic();80 packetCounter = 0;81 sizeOfLastFewPackets = 0;82 }83 84 lastFewDelays[packetCounter] = currentDelay;85 lastFewPackets[packetCounter] = currentPacket;86 sizeOfLastFewPackets += packetLength;87 88 /*Update the lifetime Variables*/89 totalReceivedPackets ++;90 float timeDiff = this->currentPacketTick - this->startTime;91 if( timeDiff != 0.0f )92 averageDatarate = totalReceivedPackets/timeDiff;93 94 /*Preparefor the next Packet*/95 lastPacketTick = currentPacketTick;96 packetCounter++;97 }98 99 100 /* Compute the value of current Datarate*/101 void ConnectionMonitor::computeCurrentDatarate()102 {103 int timeForLastFewPackets=0;104 for(int i=0;i < packetToAverage;i++)105 timeForLastFewPackets += lastFewDelays[i];106 107 if( timeForLastFewPackets != 0)108 currentDatarate = sizeOfLastFewPackets/timeForLastFewPackets;109 }110 111 void doUDPRelatedStuff()112 {113 /* Do protocol related stuff114 115 Only for Udp:116 "currentPacketID = getID from package";117 118 if(currentPacketID - lastPacketID > 1)119 {120 totalLostPackets += currentPacketID - lastPacketID;121 }122 123 totalPacketloss = (totalLostPackets/totalReceivedPackets)*100 ;124 */125 }126 127 128 129 /* Display connectoin statistic*/130 void ConnectionMonitor::displayStatistic()131 {132 // PRINT(0)("============================================\n");133 // PRINT(0)("Connection Monitor Network Statistics:\n");134 // PRINT(0)("Total received packets:",totalReceivedPackets);135 // PRINT(0)("Average datarate :\n",averageDatarate);136 // PRINT(0)("Total lost packets:",totalLostPackets);137 // PRINT(0)("Packetloss [%] :\n",totalPacketloss);138 //139 // PRINT(0)("Current datarate :\n",currentDatarate);140 // PRINT(0)("Delays of the last few packets :\n");141 // for(int i=1 ;i <= packetToAverage-1;i++)142 // PRINT(0)("%i ",lastFewDelays[i]);143 //144 // PRINT(0)("============================================\n");145 146 }147 -
branches/network/src/lib/network/connection_monitor.h
r6981 r7767 10 10 #include "netdefs.h" 11 11 12 #include <map> 13 14 #define N_PACKETS_FOR_PING 20 15 #define MSECS_TO_CALC_BWIDTH 1000 16 #define SECS_TO_TIMEOUT 10 17 12 18 class ConnectionMonitor : virtual public BaseObject 13 19 { 14 20 public: 15 ConnectionMonitor( );21 ConnectionMonitor( int userId ); 16 22 virtual ~ConnectionMonitor(); 17 23 18 void processPacket(byte* packet, unsigned int packetLength); 24 void processUnzippedOutgoingPacket( byte * data, int length, int stateId ); 25 void processUnzippedIncomingPacket( byte * data, int length, int stateId, int ackedState ); 26 void processZippedPacket( ); 27 28 void calculatePing(); 29 30 bool hasTimedOut(){ return ( lastPacketTick + SECS_TO_TIMEOUT*1000 < SDL_GetTicks() ) && nIncomingPackets > 0; } 31 32 private: 33 float calculateBandWidth( std::map<int,int> packetHistory, int tick ); 34 35 int userId; //!< user's id 19 36 37 std::map<int,int> sentStateTicks; 38 39 std::map<int,int> incomingUnzippedPacketHistory; 40 std::map<int,int> outgoingUnzippedPacketHistory; 41 42 std::list<int> ackDelay; 43 int ping; 20 44 21 private: 22 23 24 void displayStatistic(); 25 void computeCurrentDatarate(); 26 void doUDPRelatedStuff(); 27 28 /*Data of the lifetime of the ConnectionMonitor Object*/ 29 unsigned int packetToAverage; 30 char* protocollType; 31 32 unsigned int totalReceivedPackets; 33 float averageDatarate; 34 unsigned int totalLostPackets; 35 float totalPacketloss; 36 unsigned int startTime; 37 38 /*Data of the current packet*/ 39 unsigned int currentPacketID; 40 unsigned int currentPacketTick; 41 unsigned int lastPacketID; 42 unsigned int lastPacketTick; 43 unsigned int currentDelay; 44 45 /*Data of the last n packets (n is specified by paxketsToAverage)*/ 46 unsigned int sizeOfLastFewPackets; 47 unsigned int currentDatarate; 48 byte** lastFewPackets; 49 unsigned int* lastFewDelays; 50 unsigned int packetCounter; 51 45 float incomingUnzippedBandWidth; 46 float outgoingUnzippedBandWidth; 47 48 int nIncomingPackets; 49 int nOutgoingPackets; 50 51 int lastPacketTick; 52 52 }; 53 53 -
branches/network/src/lib/network/network_game_manager.cc
r7697 r7767 478 478 { 479 479 const std::list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE); 480 481 if ( !playableList ) 482 return false; 483 480 484 std::list<BaseObject*>::const_iterator it = playableList->begin(); 481 485 -
branches/network/src/lib/network/network_stream.cc
r7752 r7767 56 56 /* initialize the references */ 57 57 this->type = NET_CLIENT; 58 this->networkProtocol = new NetworkProtocol();59 this->connectionMonitor = new ConnectionMonitor();60 58 } 61 59 … … 68 66 this->peers[0].userId = 0; 69 67 this->peers[0].isServer = true; 70 this->networkProtocol = new NetworkProtocol(); 71 this->connectionMonitor = new ConnectionMonitor(); 68 this->peers[0].connectionMonitor = new ConnectionMonitor( 0 ); 72 69 } 73 70 … … 78 75 this->init(); 79 76 this->serverSocket = new UdpServerSocket(port); 80 this->networkProtocol = new NetworkProtocol();81 this->connectionMonitor = new ConnectionMonitor();82 77 this->bActive = true; 83 78 } … … 126 121 } 127 122 128 delete connectionMonitor;129 delete networkProtocol;130 123 } 131 124 … … 170 163 this->synchronizeables.erase(disconnectSynchro); 171 164 172 //TODO set timestamp 173 oldSynchronizeables[sync.getUniqueID()] = 0; 165 oldSynchronizeables[sync.getUniqueID()] = SDL_GetTicks(); 174 166 } 175 167 … … 202 194 } 203 195 196 cleanUpOldSyncList(); 204 197 handleHandshakes(); 205 198 handleUpstream(); … … 223 216 peers[clientId].socket = tempNetworkSocket; 224 217 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() ); 218 peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); 225 219 peers[clientId].handshake->setUniqueID(clientId); 226 220 peers[clientId].userId = clientId; … … 237 231 peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); 238 232 peers[clientId].handshake->setUniqueID(clientId); 233 peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); 239 234 peers[clientId].userId = clientId; 240 235 peers[clientId].isServer = false; … … 259 254 for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) 260 255 { 261 if ( it->second.socket && !it->second.socket->isOk() ) 256 //TODO check for timeout 257 if ( 258 (it->second.socket && !it->second.socket->isOk() ) || 259 it->second.connectionMonitor->hasTimedOut() 260 ) 262 261 { 263 262 PRINTF(0)("Client is gone: %d\n", it->second.userId); … … 424 423 425 424 assert( peer->second.socket->writePacket( buf, offset ) ); 426 peer->second.sentStateTicks[currentState] = SDL_GetTicks(); 425 426 peer->second.connectionMonitor->processUnzippedOutgoingPacket( buf, offset, currentState ); 427 427 428 NETPRINTF(n)("send packet: %d userId = %d\n", offset, peer->second.userId); 428 429 } … … 509 510 } 510 511 511 //TODO dont accept new object from all peers (probably only servers) 512 if ( !peers[peer->second.userId].isServer ) 513 { 514 offset += syncDataLength; 515 continue; 516 } 517 512 518 int leafClassId; 513 519 if ( INTSIZE > length - offset ) … … 564 570 } 565 571 566 if ( peer->second.sentStateTicks.find( ackedState ) != peer->second.sentStateTicks.end() ) 567 { 568 peer->second.ackDelay.push_back( SDL_GetTicks() - peer->second.sentStateTicks[ackedState] ); 569 } 570 571 while ( peer->second.sentStateTicks.begin()->first <= ackedState ) 572 peer->second.sentStateTicks.erase( peer->second.sentStateTicks.begin() ); 573 574 while ( peer->second.ackDelay.size() > 20 ) 575 peer->second.ackDelay.erase( peer->second.ackDelay.begin() ); 576 577 peer->second.ping = 0; 578 579 for ( std::list<int>::iterator it = peer->second.ackDelay.begin(); it != peer->second.ackDelay.end(); it++ ) 580 peer->second.ping += *it; 581 582 if ( peer->second.ackDelay.size() == 0 ) 583 peer->second.ping = -1; 584 else 585 peer->second.ping /= peer->second.ackDelay.size(); 586 587 NETPRINTF(n)("PING: user: %d ping: %d\n", peer->second.userId, peer->second.ping ); 572 peer->second.connectionMonitor->processUnzippedIncomingPacket( buf, offset, currentState, ackedState ); 588 573 589 574 peer->second.lastAckedState = ackedState; … … 600 585 * @todo create playable for new user 601 586 */ 602 #include "world_entities/space_ships/space_ship.h"603 587 void NetworkStream::handleNewClient( int userId ) 604 588 { … … 608 592 } 609 593 610 611 612 613 614 594 /** 595 * removes old items from oldSynchronizeables 596 */ 597 void NetworkStream::cleanUpOldSyncList( ) 598 { 599 int now = SDL_GetTicks(); 600 601 for ( std::map<int,int>::iterator it = oldSynchronizeables.begin(); it != oldSynchronizeables.end(); ) 602 { 603 if ( it->second < now - 10*1000 ) 604 { 605 std::map<int,int>::iterator delIt = it; 606 it++; 607 oldSynchronizeables.erase( delIt ); 608 continue; 609 } 610 it++; 611 } 612 } 613 614 615 616 617 618 -
branches/network/src/lib/network/network_stream.h
r7731 r7767 12 12 13 13 #include "data_stream.h" 14 #include "network_protocol.h"15 14 #include "server_socket.h" 16 15 #include "handshake.h" 16 #include "connection_monitor.h" 17 17 18 18 class Synchronizeable; 19 19 class NetworkSocket; 20 20 class ServerSocket; 21 class ConnectionMonitor;22 class NetworkProtocol;23 21 class NetworkGameManager; 24 22 … … 27 25 public: 28 26 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 std::map<int,int> sentStateTicks; 37 std::list<int> ackDelay; 38 int ping; 27 void clear() { userId = 0; isServer = false; socket = NULL; handshake = NULL; lastAckedState = 0; lastRecvedState = 0; connectionMonitor = NULL; } 28 int userId; 29 bool isServer; 30 NetworkSocket * socket; 31 Handshake * handshake; 32 ConnectionMonitor * connectionMonitor; 33 int lastAckedState; 34 int lastRecvedState; 39 35 }; 40 36 … … 85 81 void handleDownstream(); 86 82 void handleNewClient( int userId ); 83 void cleanUpOldSyncList(); 87 84 88 85 89 86 private: 90 NetworkProtocol* networkProtocol;91 ConnectionMonitor* connectionMonitor;92 87 SynchronizeableList synchronizeables; 93 88 PeerList peers; 94 89 ServerSocket* serverSocket; 95 90 int type; 96 Header packetHeader;97 91 bool bActive; 98 92 std::list<int> freeSocketSlots; -
branches/network/src/lib/network/synchronizeable.cc
r7749 r7767 351 351 for ( UserStateHistory::iterator it = sentStates.begin(); it != sentStates.end(); it++ ) 352 352 { 353 for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it ++ )353 for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ ) 354 354 { 355 355 if ( (*it2)->data ) … … 357 357 (*it2)->data = NULL; 358 358 359 delete []*it2;359 delete *it2; 360 360 } 361 361 } … … 365 365 for ( UserStateHistory::iterator it = recvStates.begin(); it != recvStates.end(); it++ ) 366 366 { 367 for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it ++ )367 for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ ) 368 368 { 369 369 if ( (*it2)->data ) … … 371 371 (*it2)->data = NULL; 372 372 373 delete []*it2;373 delete *it2; 374 374 } 375 375 }
Note: See TracChangeset
for help on using the changeset viewer.