Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7767 in orxonox.OLD for branches/network


Ignore:
Timestamp:
May 23, 2006, 11:40:29 AM (19 years ago)
Author:
rennerc
Message:

hover registers sync vars. started with connection monitor

Location:
branches/network/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/network/connection_monitor.cc

    r5822 r7767  
    1010
    1111### File Specific:
    12    main-programmer: Silvan Nellen
     12   main-programmer: Christoph Renner
    1313   co-programmer: ...
    1414*/
    1515
    1616#include "connection_monitor.h"
     17#include "network_log.h"
     18
    1719#include <debug.h>
    1820#include <SDL/SDL.h>
     
    2224using namespace std;
    2325
    24 ConnectionMonitor::ConnectionMonitor()
     26ConnectionMonitor::ConnectionMonitor( int userId )
    2527{
    26 
    2728  /* set the class id for the base object and add ist to class list*/
    2829  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}
    2940
    30   /*initialize variables*/
     41ConnectionMonitor::~ConnectionMonitor( )
     42{
     43}
    3144
    32   /*Data of the lifetime of the ConnectionMonitor Object*/
    33   packetToAverage = 100;
    34   protocollType = "default(TCP)";
     45void 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}
    3569
    36   startTime= SDL_GetTicks();
    37   totalReceivedPackets=0;
    38   averageDatarate=0;
    39   totalLostPackets=0;
    40   totalPacketloss=0;
     70void 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}
    41109
    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 
     110float 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;
    57131}
    58132
    59133
    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 stuff
    114 
    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  
    1010#include "netdefs.h"
    1111
     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
    1218class ConnectionMonitor : virtual public BaseObject
    13   {
     19{
    1420  public:
    15     ConnectionMonitor();
     21    ConnectionMonitor( int userId );
    1622    virtual ~ConnectionMonitor();
    1723
    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
    1936
     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;
    2044
    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;
    5252  };
    5353
  • branches/network/src/lib/network/network_game_manager.cc

    r7697 r7767  
    478478{
    479479  const std::list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);
     480 
     481  if ( !playableList )
     482    return false;
     483 
    480484  std::list<BaseObject*>::const_iterator it = playableList->begin();
    481485
  • branches/network/src/lib/network/network_stream.cc

    r7752 r7767  
    5656  /* initialize the references */
    5757  this->type = NET_CLIENT;
    58   this->networkProtocol = new NetworkProtocol();
    59   this->connectionMonitor = new ConnectionMonitor();
    6058}
    6159
     
    6866  this->peers[0].userId = 0;
    6967  this->peers[0].isServer = true;
    70   this->networkProtocol = new NetworkProtocol();
    71   this->connectionMonitor = new ConnectionMonitor();
     68  this->peers[0].connectionMonitor = new ConnectionMonitor( 0 );
    7269}
    7370
     
    7875  this->init();
    7976  this->serverSocket = new UdpServerSocket(port);
    80   this->networkProtocol = new NetworkProtocol();
    81   this->connectionMonitor = new ConnectionMonitor();
    8277  this->bActive = true;
    8378}
     
    126121  }
    127122
    128   delete connectionMonitor;
    129   delete networkProtocol;
    130123}
    131124
     
    170163    this->synchronizeables.erase(disconnectSynchro);
    171164 
    172   //TODO set timestamp
    173   oldSynchronizeables[sync.getUniqueID()] = 0;
     165  oldSynchronizeables[sync.getUniqueID()] = SDL_GetTicks();
    174166}
    175167
     
    202194  }
    203195
     196  cleanUpOldSyncList();
    204197  handleHandshakes();
    205198  handleUpstream();
     
    223216      peers[clientId].socket = tempNetworkSocket;
    224217      peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() );
     218      peers[clientId].connectionMonitor = new ConnectionMonitor( clientId );
    225219      peers[clientId].handshake->setUniqueID(clientId);
    226220      peers[clientId].userId = clientId;
     
    237231      peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID());
    238232      peers[clientId].handshake->setUniqueID(clientId);
     233      peers[clientId].connectionMonitor = new ConnectionMonitor( clientId );
    239234      peers[clientId].userId = clientId;
    240235      peers[clientId].isServer = false;
     
    259254  for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ )
    260255  {
    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       )
    262261    {
    263262      PRINTF(0)("Client is gone: %d\n", it->second.userId);
     
    424423   
    425424    assert( peer->second.socket->writePacket( buf, offset ) );
    426     peer->second.sentStateTicks[currentState] = SDL_GetTicks();
     425   
     426    peer->second.connectionMonitor->processUnzippedOutgoingPacket( buf, offset, currentState );
     427   
    427428    NETPRINTF(n)("send packet: %d userId = %d\n", offset, peer->second.userId);
    428429  }
     
    509510          }
    510511
    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         
    512518          int leafClassId;
    513519          if ( INTSIZE > length - offset )
     
    564570      }
    565571     
    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 );
    588573   
    589574      peer->second.lastAckedState = ackedState;
     
    600585 * @todo create playable for new user
    601586 */
    602 #include "world_entities/space_ships/space_ship.h"
    603587void NetworkStream::handleNewClient( int userId )
    604588{
     
    608592}
    609593
    610 
    611 
    612 
    613 
    614 
     594/**
     595 * removes old items from oldSynchronizeables
     596 */
     597void 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  
    1212
    1313#include "data_stream.h"
    14 #include "network_protocol.h"
    1514#include "server_socket.h"
    1615#include "handshake.h"
     16#include "connection_monitor.h"
    1717
    1818class Synchronizeable;
    1919class NetworkSocket;
    2020class ServerSocket;
    21 class ConnectionMonitor;
    22 class NetworkProtocol;
    2321class NetworkGameManager;
    2422
     
    2725  public:
    2826    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;
    3935};
    4036
     
    8581    void handleDownstream();
    8682    void handleNewClient( int userId );
     83    void cleanUpOldSyncList();
    8784
    8885
    8986  private:
    90     NetworkProtocol*           networkProtocol;
    91     ConnectionMonitor*         connectionMonitor;
    9287    SynchronizeableList        synchronizeables;
    9388    PeerList                   peers;
    9489    ServerSocket*              serverSocket;
    9590    int                        type;
    96     Header                     packetHeader;
    9791    bool                       bActive;
    9892    std::list<int>             freeSocketSlots;
  • branches/network/src/lib/network/synchronizeable.cc

    r7749 r7767  
    351351  for ( UserStateHistory::iterator it = sentStates.begin(); it != sentStates.end(); it++ )
    352352  {
    353     for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it++ )
     353    for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ )
    354354    {
    355355      if ( (*it2)->data )
     
    357357      (*it2)->data = NULL;
    358358     
    359       delete [] *it2;
     359      delete *it2;
    360360    }
    361361  }
     
    365365  for ( UserStateHistory::iterator it = recvStates.begin(); it != recvStates.end(); it++ )
    366366  {
    367     for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it++ )
     367    for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ )
    368368    {
    369369      if ( (*it2)->data )
     
    371371      (*it2)->data = NULL;
    372372     
    373       delete [] *it2;
     373      delete *it2;
    374374    }
    375375  }
  • branches/network/src/world_entities/space_ships/hover.cc

    r7337 r7767  
    176176  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
    177177  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
     178 
     179  // NETWORK THINGS
     180 
     181  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
     182  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
     183  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
     184  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
     185  registerVar( new SynchronizeableBool( &bAscend, &bAscend, "bAscend", PERMISSION_OWNER ) );
     186  registerVar( new SynchronizeableBool( &bDescend, &bDescend, "bDescend", PERMISSION_OWNER ) );
     187 
     188  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
    178189}
    179190
Note: See TracChangeset for help on using the changeset viewer.