Line data Source code
1 : /* Copyright (C) 2019 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "NetStats.h"
21 :
22 : #include "lib/external_libraries/enet.h"
23 :
24 : enum
25 : {
26 : Row_InData,
27 : Row_OutData,
28 : Row_LastSendTime,
29 : Row_LastRecvTime,
30 : Row_NextTimeout,
31 : Row_PacketsSent,
32 : Row_PacketsLost,
33 : Row_LastRTT,
34 : Row_RTT,
35 : Row_MTU,
36 : Row_ReliableInTransit,
37 : NumberRows
38 : };
39 :
40 0 : CNetStatsTable::CNetStatsTable(const ENetPeer* peer)
41 0 : : m_Peer(peer)
42 : {
43 0 : }
44 :
45 0 : CNetStatsTable::CNetStatsTable()
46 0 : : m_Peer(NULL)
47 : {
48 0 : }
49 :
50 0 : CStr CNetStatsTable::GetName()
51 : {
52 0 : return "net";
53 : }
54 :
55 0 : CStr CNetStatsTable::GetTitle()
56 : {
57 0 : if (m_Peer)
58 0 : return "Network client statistics";
59 : else
60 0 : return "Network host statistics";
61 : }
62 :
63 0 : size_t CNetStatsTable::GetNumberRows()
64 : {
65 0 : return NumberRows;
66 : }
67 :
68 0 : const std::vector<ProfileColumn>& CNetStatsTable::GetColumns()
69 : {
70 0 : m_ColumnDescriptions.clear();
71 0 : m_ColumnDescriptions.push_back(ProfileColumn("Name", 200));
72 :
73 0 : if (m_Peer)
74 0 : m_ColumnDescriptions.push_back(ProfileColumn("Value", 80));
75 : else
76 : {
77 0 : std::lock_guard<std::mutex> lock(m_Mutex);
78 :
79 0 : for (size_t i = 0; i < m_LatchedData.size(); ++i)
80 0 : m_ColumnDescriptions.push_back(ProfileColumn("Peer "+CStr::FromUInt(i), 80));
81 : }
82 :
83 0 : return m_ColumnDescriptions;
84 : }
85 :
86 0 : CStr CNetStatsTable::GetCellText(size_t row, size_t col)
87 : {
88 : // Return latched data, if we have any
89 : {
90 0 : std::lock_guard<std::mutex> lock(m_Mutex);
91 0 : if (col > 0 && m_LatchedData.size() > col-1 && m_LatchedData[col-1].size() > row)
92 0 : return m_LatchedData[col-1][row];
93 : }
94 :
95 : #define ROW(id, title, member) \
96 : case id: \
97 : if (col == 0) return title; \
98 : if (m_Peer) return CStr::FromUInt(m_Peer->member); \
99 : return "???"
100 :
101 0 : switch(row)
102 : {
103 0 : ROW(Row_InData, "incoming bytes", incomingDataTotal);
104 0 : ROW(Row_OutData, "outgoing bytes", outgoingDataTotal);
105 0 : ROW(Row_LastSendTime, "last send time", lastSendTime);
106 0 : ROW(Row_LastRecvTime, "last receive time", lastReceiveTime);
107 0 : ROW(Row_NextTimeout, "next timeout", nextTimeout);
108 0 : ROW(Row_PacketsSent, "packets sent", packetsSent);
109 0 : ROW(Row_PacketsLost, "packets lost", packetsLost);
110 0 : ROW(Row_LastRTT, "last RTT", lastRoundTripTime);
111 0 : ROW(Row_RTT, "mean RTT", roundTripTime);
112 0 : ROW(Row_MTU, "MTU", mtu);
113 0 : ROW(Row_ReliableInTransit, "reliable data in transit", reliableDataInTransit);
114 :
115 0 : default:
116 0 : return "???";
117 : }
118 :
119 : #undef ROW
120 : }
121 :
122 0 : AbstractProfileTable* CNetStatsTable::GetChild(size_t UNUSED(row))
123 : {
124 0 : return 0;
125 : }
126 :
127 0 : void CNetStatsTable::LatchHostState(const ENetHost* host)
128 : {
129 0 : std::lock_guard<std::mutex> lock(m_Mutex);
130 :
131 : #define ROW(id, title, member) \
132 : m_LatchedData[i].push_back(CStr::FromUInt(host->peers[i].member));
133 :
134 0 : m_LatchedData.clear();
135 0 : m_LatchedData.resize(host->peerCount);
136 :
137 0 : for (size_t i = 0; i < host->peerCount; ++i)
138 : {
139 0 : ROW(Row_InData, "incoming bytes", incomingDataTotal);
140 0 : ROW(Row_OutData, "outgoing bytes", outgoingDataTotal);
141 0 : ROW(Row_LastSendTime, "last send time", lastSendTime);
142 0 : ROW(Row_LastRecvTime, "last receive time", lastReceiveTime);
143 0 : ROW(Row_NextTimeout, "next timeout", nextTimeout);
144 0 : ROW(Row_PacketsSent, "packets sent", packetsSent);
145 0 : ROW(Row_PacketsLost, "packets lost", packetsLost);
146 0 : ROW(Row_LastRTT, "last RTT", lastRoundTripTime);
147 0 : ROW(Row_RTT, "mean RTT", roundTripTime);
148 0 : ROW(Row_MTU, "MTU", mtu);
149 0 : ROW(Row_ReliableInTransit, "reliable data in transit", reliableDataInTransit);
150 : }
151 : #undef ROW
152 0 : }
|