-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetStatistics.h
168 lines (125 loc) · 5.77 KB
/
NetStatistics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/******************************************************************************/
/* */
/* Copyright (c) 2010, 2014 Sylwester Wysocki <sw143@wp.pl> */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a */
/* copy of this software and associated documentation files (the "Software"), */
/* to deal in the Software without restriction, including without limitation */
/* the rights to use, copy, modify, merge, publish, distribute, sublicense, */
/* and/or sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in */
/* all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL */
/* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER */
/* DEALINGS IN THE SOFTWARE. */
/* */
/******************************************************************************/
#ifndef Tegenaria_Core_NetStatistics_H
#define Tegenaria_Core_NetStatistics_H
#include <Tegenaria/Math.h>
#include <string>
namespace Tegenaria
{
using std::string;
//
// Field set in NetStatistics.fieldsSet_ meaning which struct
// fields are set.
//
#define NET_STAT_FIELD_UPLOAD_SPEED (1 << 0)
#define NET_STAT_FIELD_DOWNLOAD_SPEED (1 << 1)
#define NET_STAT_FIELD_REQUEST_SPEED (1 << 2)
#define NET_STAT_FIELD_BYTES_SENT (1 << 3)
#define NET_STAT_FIELD_BYTES_RECV (1 << 4)
#define NET_STAT_FIELD_BYTES_UPLOADED (1 << 5)
#define NET_STAT_FIELD_BYTES_DOWNLOADED (1 << 6)
#define NET_STAT_FIELD_PACKET_SENT_COUNT (1 << 7)
#define NET_STAT_FIELD_PACKET_RECV_COUNT (1 << 8)
#define NET_STAT_FIELD_REQUEST_COUNT (1 << 9)
#define NET_STAT_FIELD_REQUEST_TIME_TOTAL (1 << 10)
#define NET_STAT_FIELD_REQUEST_TIME_MAX (1 << 11)
#define NET_STAT_FIELD_REQUEST_TIME_AVG (1 << 12)
#define NET_STAT_FIELD_PARTIAL_READ_TRIGGERED (1 << 13)
#define NET_STAT_FIELD_PARTIAL_WRITE_TRIGGERED (1 << 14)
#define NET_STAT_FIELD_PING_MAX (1 << 15)
#define NET_STAT_FIELD_PING_AVG (1 << 16)
//
// Structure to store network statistics.
//
class NetStatistics
{
private:
//
// Fields.
//
MathWeightAvg uploadSpeedAvg_; // Average upload speed in KB/s
MathWeightAvg downloadSpeedAvg_; // Average download speed in KB/s
MathWeightAvg requestTimeAvg_; // Average request speed in KB/s
MathWeightAvg requestSpeedAvg_; // Average request time in ms
MathWeightAvg pingAvg_; // Average ping in ms
double resetTime_; // Last reset time to compute connection time
int packetSentCount_; // Number of all packets sent
int packetRecvCount_; // Number of all packets received
int requestCount_; // Number of all requests processed.
double bytesUploaded_; // Number of bytes uploaded
double bytesDownloaded_; // Number of bytes downloaded
double bytesSent_; // Total number of bytes sent
double bytesRecv_; // Total number of bytes received
double requestTimeTotal_; // Total ms spent in processing one request in ms
double requestTimeMax_; // Max. time spent in processing one request in ms
int partialReadTriggered_; // Read operation was cancelled due to timeout
int partialWriteTriggered_; // Write operation was cancelled due to timeout
double pingMax_; // Max. noted ping
unsigned int fieldsSet_; // Combination of NET_STAT_FIELD_XXX values
// telling which struct fields are set.
//
// Functions.
//
public:
NetStatistics();
string toString();
void reset();
double limit(double x);
//
// Put data into statistics.
//
void insertOutcomingPacket(int size);
void insertIncomingPacket(int size);
void insertUploadEvent(int size, double elapsed);
void insertDownloadEvent(int size, double elapsed);
void insertRequest(int size, double elapsed);
void insertPing(double ping);
void triggerPartialRead();
void triggerPartialWrite();
//
// Getters.
//
double getTimeMs();
double getUploadSpeed();
double getDownloadSpeed();
double getRequestTime();
double getRequestSpeed();
double getPing();
double getPingMax();
double getConnectionTime();
int getPacketSentCount();
int getPacketRecvCount();
int getRequestCount();
double getBytesUploaded();
double getBytesDownloaded();
double getBytesSent();
double getBytesReceived();
double getRequestTimeTotal();
double getRequestTimeMax();
int isPartialReadTriggered();
int isPartialWriteTriggered();
int getNetworkQuality();
};
} /* namespace Tegenaria */
#endif /* Tegenaria_Core_NetStatistics_H */