Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/connection_monitor.cc @ 7767

Last change on this file since 7767 was 7767, checked in by rennerc, 18 years ago

hover registers sync vars. started with connection monitor

File size: 3.4 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Christoph Renner
13   co-programmer: ...
14*/
15
16#include "connection_monitor.h"
17#include "network_log.h"
18
19#include <debug.h>
20#include <SDL/SDL.h>
21#include <string.h>
22
23/* using namespace std is default, this needs to be here */
24using namespace std;
25
26ConnectionMonitor::ConnectionMonitor( int userId )
27{
28  /* set the class id for the base object and add ist to class list*/
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}
40
41ConnectionMonitor::~ConnectionMonitor( )
42{
43}
44
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}
69
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}
109
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;
131}
132
133
Note: See TracBrowser for help on using the repository browser.