Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed bug

File size: 3.8 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->incomingZippedBandWidth = 0;
36  this->outgoingZippedBandWidth = 0;
37  this->nIncomingPackets = 0;
38  this->nOutgoingPackets = 0;
39 
40  this->lastPacketTick = 0;
41}
42
43ConnectionMonitor::~ConnectionMonitor( )
44{
45}
46
47void ConnectionMonitor::processUnzippedOutgoingPacket( byte * data, int length, int stateId )
48{
49  int tick = SDL_GetTicks();
50 
51  nOutgoingPackets++;
52 
53  // for ping calculation
54  sentStateTicks[stateId] = tick;
55 
56  // calculate bandwidth
57  outgoingUnzippedPacketHistory[tick] = length;
58  outgoingUnzippedBandWidth = calculateBandWidth( outgoingUnzippedPacketHistory, tick );
59 
60  NETPRINTF(n)("UPSTREAM: user: %d bandwidth %f\n", userId, outgoingUnzippedBandWidth );
61 
62  // count zero bytes
63  int nZeroBytes = 0;
64 
65  for ( int i = 0; i < length; i++ )
66  {
67    if ( data[i] == '\0' )
68      nZeroBytes++;
69  }
70 
71  NETPRINTF(n)( "ZEROBYTES: %d (%f%%)\n", nZeroBytes, ((float)100)*nZeroBytes/length );
72}
73
74void ConnectionMonitor::processUnzippedIncomingPacket( byte * data, int length, int stateId, int ackedState )
75{
76  int tick = SDL_GetTicks();
77 
78  nIncomingPackets++;
79 
80  lastPacketTick = tick;
81 
82  // calculate ping
83  if ( sentStateTicks.find( ackedState ) != sentStateTicks.end() )
84  {
85    ackDelay.push_back( tick - sentStateTicks[ackedState] );
86  }
87     
88  //TODO cleanup list
89//  while ( sentStateTicks.begin()->first <= ackedState )
90//  {
91//    NETPRINTF(n)("removing old state\n");
92//    sentStateTicks.erase( sentStateTicks.begin() );
93//  }
94 
95  NETPRINTF(n)("adsf\n");
96     
97  //TODO cleanup list
98#if 0
99  while ( ackDelay.size() > N_PACKETS_FOR_PING )
100  {
101    NETPRINTF(n)("removing old ackdelay\n");
102    ackDelay.erase( ackDelay.begin() );
103  }
104#endif
105     
106  ping = 0;
107     
108  for ( std::list<int>::iterator it = ackDelay.begin(); it != ackDelay.end(); it++ )
109    ping += *it;
110     
111  if ( ackDelay.size() == 0 )
112    ping = -1;
113  else
114    ping /= ackDelay.size();
115     
116  NETPRINTF(n)("PING: user: %d ping: %d\n", userId, ping );
117 
118  // calculate bandwidth
119  incomingUnzippedPacketHistory[tick] = length;
120  incomingUnzippedBandWidth = calculateBandWidth( incomingUnzippedPacketHistory, tick );
121 
122  NETPRINTF(n)("DOWNSTREAM: user: %d bandwidth %f\n", userId, incomingUnzippedBandWidth );
123 
124}
125
126float ConnectionMonitor::calculateBandWidth( std::map< int, int > packetHistory, int tick )
127{
128  // delete old packets
129  while ( packetHistory.begin()->first < tick - MSECS_TO_CALC_BWIDTH )
130  {
131    packetHistory.erase( packetHistory.begin() );
132  }
133 
134  float res = 0.0f;
135 
136  for ( std::map<int,int>::iterator it = packetHistory.begin(); it != packetHistory.end(); it++ )
137  {
138    res += it->second;
139  }
140 
141  if ( packetHistory.size() <= 1 || tick - packetHistory.begin()->first == 0 )
142    res = 0;
143  else
144    res /= (float)((tick - packetHistory.begin()->first)*( 1 + 1/((float)(packetHistory.size()-1)) ));
145 
146  res *= 1000;
147 
148  return res;
149}
150
151/**
152 * calculates max packet size you can send now to fit into bandwidth
153 * @return
154 */
155int ConnectionMonitor::getMaxPacketSize( )
156{
157}
158
159
Note: See TracBrowser for help on using the repository browser.