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 */ |
---|
24 | using namespace std; |
---|
25 | |
---|
26 | ConnectionMonitor::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 | this->nZIncomingPackets = 0; |
---|
40 | this->nZOutgoingPackets = 0; |
---|
41 | |
---|
42 | this->lastPacketTick = 0; |
---|
43 | } |
---|
44 | |
---|
45 | ConnectionMonitor::~ConnectionMonitor( ) |
---|
46 | { |
---|
47 | } |
---|
48 | |
---|
49 | void ConnectionMonitor::processUnzippedOutgoingPacket( byte * data, int length, int stateId ) |
---|
50 | { |
---|
51 | int tick = SDL_GetTicks(); |
---|
52 | |
---|
53 | nOutgoingPackets++; |
---|
54 | |
---|
55 | // for ping calculation |
---|
56 | sentStateTicks[stateId] = tick; |
---|
57 | |
---|
58 | // calculate bandwidth |
---|
59 | outgoingUnzippedPacketHistory[tick] = length; |
---|
60 | outgoingUnzippedBandWidth = calculateBandWidth( outgoingUnzippedPacketHistory, tick ); |
---|
61 | |
---|
62 | NETPRINTF(n)("UNZIPPED UPSTREAM: user: %d bandwidth %f\n", userId, outgoingUnzippedBandWidth ); |
---|
63 | |
---|
64 | // count zero bytes |
---|
65 | int nZeroBytes = 0; |
---|
66 | |
---|
67 | for ( int i = 0; i < length; i++ ) |
---|
68 | if ( data[i] == '\0' ) |
---|
69 | nZeroBytes++; |
---|
70 | |
---|
71 | NETPRINTF(n)( "ZEROBYTES: %d (%f%%)\n", nZeroBytes, ((float)100)*nZeroBytes/length ); |
---|
72 | } |
---|
73 | |
---|
74 | void 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 | while ( sentStateTicks.begin() != sentStateTicks.end() && sentStateTicks.begin()->first <= ackedState ) |
---|
89 | sentStateTicks.erase( sentStateTicks.begin() ); |
---|
90 | |
---|
91 | while ( ackDelay.size() > N_PACKETS_FOR_PING ) |
---|
92 | ackDelay.erase( ackDelay.begin() ); |
---|
93 | |
---|
94 | ping = 0; |
---|
95 | |
---|
96 | for ( std::list<int>::iterator it = ackDelay.begin(); it != ackDelay.end(); it++ ) |
---|
97 | ping += *it; |
---|
98 | |
---|
99 | if ( ackDelay.size() == 0 ) |
---|
100 | ping = -1; |
---|
101 | else |
---|
102 | ping /= ackDelay.size(); |
---|
103 | |
---|
104 | NETPRINTF(n)("PING: user: %d ping: %d\n", userId, ping ); |
---|
105 | |
---|
106 | // calculate bandwidth |
---|
107 | incomingUnzippedPacketHistory[tick] = length; |
---|
108 | incomingUnzippedBandWidth = calculateBandWidth( incomingUnzippedPacketHistory, tick ); |
---|
109 | |
---|
110 | NETPRINTF(n)("UNZIPPED DOWNSTREAM: user: %d bandwidth %f\n", userId, incomingUnzippedBandWidth ); |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | float ConnectionMonitor::calculateBandWidth( std::map< int, int > packetHistory, int tick ) |
---|
115 | { |
---|
116 | // delete old packets |
---|
117 | while ( packetHistory.begin()->first < tick - MSECS_TO_CALC_BWIDTH ) |
---|
118 | packetHistory.erase( packetHistory.begin() ); |
---|
119 | |
---|
120 | float res = 0.0f; |
---|
121 | |
---|
122 | for ( std::map<int,int>::iterator it = packetHistory.begin(); it != packetHistory.end(); it++ ) |
---|
123 | { |
---|
124 | res += it->second; |
---|
125 | } |
---|
126 | |
---|
127 | if ( packetHistory.size() <= 1 || tick - packetHistory.begin()->first == 0 ) |
---|
128 | res = 0.0f; |
---|
129 | else |
---|
130 | res /= (float)((tick - packetHistory.begin()->first)*( 1 + 1/((float)(packetHistory.size()-1)) )); |
---|
131 | |
---|
132 | res *= 1000.0f; |
---|
133 | |
---|
134 | return res; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | void ConnectionMonitor::processZippedOutgoingPacket( byte * data, int length, int stateId ) |
---|
141 | { |
---|
142 | int tick = SDL_GetTicks(); |
---|
143 | |
---|
144 | nZOutgoingPackets++; |
---|
145 | |
---|
146 | // calculate bandwidth |
---|
147 | outgoingZippedPacketHistory[tick] = length; |
---|
148 | outgoingZippedBandWidth = calculateBandWidth( outgoingZippedPacketHistory, tick ); |
---|
149 | |
---|
150 | NETPRINTF(n)("UPSTREAM: user: %d bandwidth %f nOutgoingPackets %d\n", userId, outgoingZippedBandWidth, nOutgoingPackets ); |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | void ConnectionMonitor::processZippedIncomingPacket( byte * data, int length, int stateId, int ackedState ) |
---|
156 | { |
---|
157 | int tick = SDL_GetTicks(); |
---|
158 | |
---|
159 | nZIncomingPackets++; |
---|
160 | |
---|
161 | // calculate bandwidth |
---|
162 | incomingZippedPacketHistory[tick] = length; |
---|
163 | incomingZippedBandWidth = calculateBandWidth( incomingZippedPacketHistory, tick ); |
---|
164 | |
---|
165 | NETPRINTF(n)("DOWNSTREAM: user: %d bandwidth %f nIncomingPackets %d\n", userId, incomingZippedBandWidth, nIncomingPackets ); |
---|
166 | |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | bool ConnectionMonitor::hasTimedOut( ) |
---|
171 | { |
---|
172 | if ( lastPacketTick + SECS_TO_TIMEOUT*1000 < SDL_GetTicks() && nIncomingPackets > 0 ) |
---|
173 | return true; |
---|
174 | |
---|
175 | if ( nIncomingPackets == 0 && nOutgoingPackets >= NETWORK_FREQUENCY*SECS_TO_TIMEOUT ) |
---|
176 | return true; |
---|
177 | |
---|
178 | return false; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|