[5523] | 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: |
---|
[7767] | 12 | main-programmer: Christoph Renner |
---|
[5523] | 13 | co-programmer: ... |
---|
[5550] | 14 | */ |
---|
[5523] | 15 | |
---|
[5547] | 16 | #include "connection_monitor.h" |
---|
[7767] | 17 | #include "network_log.h" |
---|
| 18 | |
---|
[5618] | 19 | #include <debug.h> |
---|
| 20 | #include <SDL/SDL.h> |
---|
| 21 | #include <string.h> |
---|
[5526] | 22 | |
---|
[5564] | 23 | /* using namespace std is default, this needs to be here */ |
---|
| 24 | using namespace std; |
---|
| 25 | |
---|
[7767] | 26 | ConnectionMonitor::ConnectionMonitor( int userId ) |
---|
[5618] | 27 | { |
---|
| 28 | /* set the class id for the base object and add ist to class list*/ |
---|
| 29 | this->setClassID(CL_CONNECTION_MONITOR, "ConnectionMonitor"); |
---|
[7767] | 30 | |
---|
| 31 | this->userId = userId; |
---|
| 32 | this->ping = 0; |
---|
| 33 | this->incomingUnzippedBandWidth = 0; |
---|
| 34 | this->outgoingUnzippedBandWidth = 0; |
---|
[7772] | 35 | this->incomingZippedBandWidth = 0; |
---|
| 36 | this->outgoingZippedBandWidth = 0; |
---|
[7767] | 37 | this->nIncomingPackets = 0; |
---|
| 38 | this->nOutgoingPackets = 0; |
---|
| 39 | |
---|
| 40 | this->lastPacketTick = 0; |
---|
[5618] | 41 | } |
---|
| 42 | |
---|
[7767] | 43 | ConnectionMonitor::~ConnectionMonitor( ) |
---|
[5618] | 44 | { |
---|
| 45 | } |
---|
[5523] | 46 | |
---|
[7767] | 47 | void ConnectionMonitor::processUnzippedOutgoingPacket( byte * data, int length, int stateId ) |
---|
[5618] | 48 | { |
---|
[7767] | 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++ ) |
---|
[7772] | 66 | { |
---|
[7767] | 67 | if ( data[i] == '\0' ) |
---|
| 68 | nZeroBytes++; |
---|
[7772] | 69 | } |
---|
[7767] | 70 | |
---|
| 71 | NETPRINTF(n)( "ZEROBYTES: %d (%f%%)\n", nZeroBytes, ((float)100)*nZeroBytes/length ); |
---|
[5618] | 72 | } |
---|
[5581] | 73 | |
---|
[7767] | 74 | void ConnectionMonitor::processUnzippedIncomingPacket( byte * data, int length, int stateId, int ackedState ) |
---|
[5618] | 75 | { |
---|
[7767] | 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 | |
---|
[7772] | 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"); |
---|
[7767] | 96 | |
---|
[7772] | 97 | //TODO cleanup list |
---|
| 98 | #if 0 |
---|
[7767] | 99 | while ( ackDelay.size() > N_PACKETS_FOR_PING ) |
---|
[7772] | 100 | { |
---|
| 101 | NETPRINTF(n)("removing old ackdelay\n"); |
---|
[7767] | 102 | ackDelay.erase( ackDelay.begin() ); |
---|
[7772] | 103 | } |
---|
| 104 | #endif |
---|
[7767] | 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 | |
---|
[5618] | 124 | } |
---|
[5581] | 125 | |
---|
[7767] | 126 | float ConnectionMonitor::calculateBandWidth( std::map< int, int > packetHistory, int tick ) |
---|
[5720] | 127 | { |
---|
[7767] | 128 | // delete old packets |
---|
| 129 | while ( packetHistory.begin()->first < tick - MSECS_TO_CALC_BWIDTH ) |
---|
[7772] | 130 | { |
---|
[7767] | 131 | packetHistory.erase( packetHistory.begin() ); |
---|
[7772] | 132 | } |
---|
[7767] | 133 | |
---|
| 134 | float res = 0.0f; |
---|
| 135 | |
---|
| 136 | for ( std::map<int,int>::iterator it = packetHistory.begin(); it != packetHistory.end(); it++ ) |
---|
[5726] | 137 | { |
---|
[7767] | 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; |
---|
[5720] | 149 | } |
---|
| 150 | |
---|
[7772] | 151 | /** |
---|
| 152 | * calculates max packet size you can send now to fit into bandwidth |
---|
| 153 | * @return |
---|
| 154 | */ |
---|
| 155 | int ConnectionMonitor::getMaxPacketSize( ) |
---|
| 156 | { |
---|
| 157 | } |
---|
[5720] | 158 | |
---|
[7772] | 159 | |
---|