Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/connection_monitor.cc @ 8336

Last change on this file since 8336 was 8068, checked in by patrick, 18 years ago

trunk: merged the network branche back to trunk

File size: 6.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
26/**
27 * constructor
28 * @param userId user's id
29 */
30ConnectionMonitor::ConnectionMonitor( int userId )
31{
32  /* set the class id for the base object and add ist to class list*/
33  this->setClassID(CL_CONNECTION_MONITOR, "ConnectionMonitor");
34 
35  this->userId = userId;
36  this->ping = 0;
37  this->incomingUnzippedBandWidth = 0;
38  this->outgoingUnzippedBandWidth = 0;
39  this->incomingZippedBandWidth = 0;
40  this->outgoingZippedBandWidth = 0;
41  this->nIncomingPackets = 0;
42  this->nOutgoingPackets = 0;
43  this->nZIncomingPackets = 0;
44  this->nZOutgoingPackets = 0;
45 
46  this->lastPacketTick = 0;
47  this->lastPrintTick = 0;
48}
49
50/**
51 * deconstructor
52 */
53ConnectionMonitor::~ConnectionMonitor( )
54{
55}
56
57/**
58 * process unzipped outgoing packet
59 * @param data pointer to packet data
60 * @param length length of packet
61 * @param stateId packet's state id
62 */
63void ConnectionMonitor::processUnzippedOutgoingPacket( int tick, byte * data, int length, int stateId )
64{
65  nOutgoingPackets++;
66 
67  // for ping calculation
68  sentStateTicks[stateId] = tick;
69 
70  // calculate bandwidth
71  outgoingUnzippedPacketHistory[tick] = length;
72  outgoingUnzippedBandWidth = calculateBandWidth( outgoingUnzippedPacketHistory, tick );
73 
74  //NETPRINTF(n)("UNZIPPED UPSTREAM: user: %d bandwidth %f\n", userId, outgoingUnzippedBandWidth );
75 
76  // count zero bytes
77  //int nZeroBytes = 0;
78 
79  //for ( int i = 0; i < length; i++ )
80  //  if ( data[i] == '\0' )
81  //    nZeroBytes++;
82 
83  //NETPRINTF(n)( "ZEROBYTES: %d (%f%%)\n", nZeroBytes, ((float)100)*nZeroBytes/length );
84}
85
86/**
87 * process unzipped incoming packet
88 * @param data pointer to packet data
89 * @param length length of packet
90 * @param stateId packet's state id
91 * @param ackedState state which was acked by this packet
92 */
93void ConnectionMonitor::processUnzippedIncomingPacket( int tick, byte * data, int length, int stateId, int ackedState )
94{
95  nIncomingPackets++;
96 
97  lastPacketTick = tick;
98 
99  // calculate ping
100  if ( sentStateTicks.find( ackedState ) != sentStateTicks.end() )
101  {
102    ackDelay.push_back( tick - sentStateTicks[ackedState] );
103  }
104 
105  while ( sentStateTicks.begin() != sentStateTicks.end() && sentStateTicks.begin()->first <= ackedState )
106    sentStateTicks.erase( sentStateTicks.begin() );
107     
108  while ( ackDelay.size() > N_PACKETS_FOR_PING )
109    ackDelay.erase( ackDelay.begin() );
110     
111  ping = 0;
112     
113  for ( std::list<int>::iterator it = ackDelay.begin(); it != ackDelay.end(); it++ )
114    ping += *it;
115     
116  if ( ackDelay.size() == 0 )
117    ping = -1;
118  else
119    ping /= ackDelay.size();
120     
121  //NETPRINTF(n)("PING: user: %d ping: %d\n", userId, ping );
122 
123  // calculate bandwidth
124  incomingUnzippedPacketHistory[tick] = length;
125  incomingUnzippedBandWidth = calculateBandWidth( incomingUnzippedPacketHistory, tick );
126 
127  //NETPRINTF(n)("UNZIPPED DOWNSTREAM: user: %d bandwidth %f\n", userId, incomingUnzippedBandWidth );
128 
129}
130
131/**
132 * calculate bandwidth out of packethistory
133 * @param packetHistory packet history
134 * @param tick current tick from SDL_GetTicks
135 * @return bandwidth in bytes/sec
136 */
137float ConnectionMonitor::calculateBandWidth( std::map< int, int > packetHistory, int tick )
138{
139  // delete old packets
140  while ( packetHistory.begin()->first < tick - MSECS_TO_CALC_BWIDTH )
141    packetHistory.erase( packetHistory.begin() );
142 
143  float res = 0.0f;
144 
145  for ( std::map<int,int>::iterator it = packetHistory.begin(); it != packetHistory.end(); it++ )
146  {
147    if ( it != packetHistory.begin() )
148      res += it->second;
149  }
150 
151  if ( packetHistory.size() <= 1 || tick - packetHistory.begin()->first == 0 )
152    res = 0.0f;
153  else
154    res /= (float)(tick - packetHistory.begin()->first);
155 
156  res *= 1000.0f;
157 
158  return res;
159}
160
161
162/**
163 * process zipped outgoing packet
164 * @param data pointer to packet data
165 * @param length length of packet
166 * @param stateId packet's state id
167 */
168void ConnectionMonitor::processZippedOutgoingPacket( int tick, byte * data, int length, int stateId )
169{
170  nZOutgoingPackets++;
171 
172  // calculate bandwidth
173  outgoingZippedPacketHistory[tick] = length;
174  outgoingZippedBandWidth = calculateBandWidth( outgoingZippedPacketHistory, tick );
175 
176  //NETPRINTF(n)("UPSTREAM: user: %d bandwidth %f nOutgoingPackets %d\n", userId, outgoingZippedBandWidth, nOutgoingPackets );
177
178  if ( lastPrintTick < tick-1000 )
179  {
180    printStatis();
181    lastPrintTick = tick;
182  }
183}
184
185
186/**
187 * process zipped incoming packet
188 * @param data pointer to packet data
189 * @param length length of packet
190 * @param stateId packet's state id
191 * @param ackedState state which was acked by this packet
192 */
193void ConnectionMonitor::processZippedIncomingPacket( int tick, byte * data, int length )
194{
195  nZIncomingPackets++;
196 
197  // calculate bandwidth
198  incomingZippedPacketHistory[tick] = length;
199  incomingZippedBandWidth = calculateBandWidth( incomingZippedPacketHistory, tick );
200 
201  //NETPRINTF(n)("DOWNSTREAM: user: %d bandwidth %f nIncomingPackets %d\n", userId, incomingZippedBandWidth, nIncomingPackets );
202 
203}
204
205
206/**
207 * check if client sent no packets for SECS_TO_TIMEOUT
208 * @return true if last packet recieved \< NOW() - SECS_TO_TIMEOUT
209 */
210bool ConnectionMonitor::hasTimedOut( )
211{
212  if ( lastPacketTick + SECS_TO_TIMEOUT*1000 < SDL_GetTicks() && nIncomingPackets > 0 )
213    return true;
214 
215  if ( nIncomingPackets == 0 && nOutgoingPackets >= NETWORK_FREQUENCY*SECS_TO_TIMEOUT )
216    return true;
217 
218  return false;
219}
220
221
222
223/**
224 * prints bandwith usage, ping and other important things to telnet-console
225 */
226void ConnectionMonitor::printStatis( )
227{
228  NETPRINT(n)("============NETWORKSTATS FOR USER %d============\n", userId);
229  NETPRINT(n)("PING = %d\n", ping );
230  NETPRINT(n)("BANDWIDTH: UP: %f (%f) DOWN %f (%f)\n", outgoingZippedBandWidth, outgoingUnzippedBandWidth, incomingZippedBandWidth, incomingUnzippedBandWidth);
231  NETPRINT(n)("PACKETS: RECIEVED %d; SENT %d\n", nIncomingPackets, nOutgoingPackets );
232  NETPRINT(n)("================================================\n");
233}
234
235
Note: See TracBrowser for help on using the repository browser.