R-Type
R-Type project
NetworkStatistics.hpp
1 #pragma once
2 
3 #include "KapMirror/Components/NetworkComponent.hpp"
4 #include <functional>
5 
6 namespace KapMirror::Experimental {
8  private:
9  long long lastIntervalTime = 0;
10 
11  public:
12  // ---------------------------------------------------------------------
13 
14  // CLIENT (public fields for other components to grab statistics)
15  // long bytes to support >2GB
16  int clientIntervalReceivedPackets = 0;
17  long clientIntervalReceivedBytes = 0;
18  int clientIntervalSentPackets = 0;
19  long clientIntervalSentBytes = 0;
20 
21  // total packets
22  int clientTotalReceivedPackets = 0;
23  int clientTotalSentPackets = 0;
24 
25  // total bytes to support >2GB
26  long clientReceivedBytesTotal = 0;
27  long clientSentBytesTotal = 0;
28 
29  // results from last interval
30  // long bytes to support >2GB
31  int clientReceivedPacketsPerSecond = 0;
32  long clientReceivedBytesPerSecond = 0;
33  int clientSentPacketsPerSecond = 0;
34  long clientSentBytesPerSecond = 0;
35 
36  // ---------------------------------------------------------------------
37 
38  // SERVER (public fields for other components to grab statistics)
39  // capture interval
40  // long bytes to support >2GB
41  int serverIntervalReceivedPackets = 0;
42  long serverIntervalReceivedBytes = 0;
43  int serverIntervalSentPackets = 0;
44  long serverIntervalSentBytes = 0;
45 
46  // total packets
47  int serverTotalReceivedPackets = 0;
48  int serverTotalSentPackets = 0;
49 
50  // total bytes to support >2GB
51  long serverReceivedBytesTotal = 0;
52  long serverSentBytesTotal = 0;
53 
54  // results from last interval
55  // long bytes to support >2GB
56  int serverReceivedPacketsPerSecond = 0;
57  long serverReceivedBytesPerSecond = 0;
58  int serverSentPacketsPerSecond = 0;
59  long serverSentBytesPerSecond = 0;
60 
61  public:
62  explicit NetworkStatistics(std::shared_ptr<KapEngine::GameObject> go);
63  ~NetworkStatistics() = default;
64 
65  void onStart() override;
66 
67  void onDestroy() override;
68 
69  void onUpdate() override;
70 
74  void reset();
75 
76  private:
77  void onClientReceive(const std::shared_ptr<ArraySegment<byte>>& data);
78 
79  void onClientSend(const std::shared_ptr<ArraySegment<byte>>& data);
80 
81  void onServerReceive(const std::shared_ptr<ArraySegment<byte>>& data);
82 
83  void onServerSend(const std::shared_ptr<ArraySegment<byte>>& data);
84 
85  void updateClient();
86 
87  void updateServer();
88 
89  private:
90  std::function<void(Transport&, const std::shared_ptr<ArraySegment<byte>>&)> onClientDataReceived;
91  std::function<void(Transport&, const std::shared_ptr<ArraySegment<byte>>&)> onClientDataSent;
92  std::function<void(Transport&, int, const std::shared_ptr<ArraySegment<byte>>&)> onServerDataReceived;
93  std::function<void(Transport&, int, const std::shared_ptr<ArraySegment<byte>>&)> onServerDataSent;
94  };
95 } // namespace KapMirror::Experimental
KapMirror::Transport
Definition: Transport.hpp:10
KapMirror::Experimental::NetworkStatistics::onDestroy
void onDestroy() override
call when component destroyed
Definition: NetworkStatistics.cpp:24
KapMirror::Experimental::NetworkStatistics::reset
void reset()
Reset all statistics.
Definition: NetworkStatistics.cpp:100
KapMirror::Experimental::NetworkStatistics
Definition: NetworkStatistics.hpp:7
KapEngine::Component
Definition: Component.hpp:38
KapMirror::Experimental::NetworkStatistics::onStart
void onStart() override
call component start
Definition: NetworkStatistics.cpp:8
KapMirror::Experimental::NetworkStatistics::onUpdate
void onUpdate() override
call each frame
Definition: NetworkStatistics.cpp:33
KapMirror::ArraySegment
Definition: ArraySegment.hpp:9