Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
traffic.hpp
Go to the documentation of this file.
1
5#pragma once
6#include <aegis2d/net/export.hpp>
8
9#include <array>
10#include <chrono>
11#include <cstddef>
12#include <cstdint>
13
14namespace aegis2d::net {
15
17enum class Direction : std::uint8_t {
19 Sent = 0,
21 Received = 1,
22};
23
27 std::uint64_t datagrams = 0;
30 std::uint64_t bytes = 0;
31
35 TrafficCount& operator+=(const TrafficCount& other) noexcept {
36 datagrams += other.datagrams;
37 bytes += other.bytes;
38 return *this;
39 }
40
43 bool operator==(const TrafficCount&) const = default;
44};
45
54class AEGIS2D_NET_EXPORT TrafficCounters {
55public:
57 using Clock = std::chrono::steady_clock;
58
64 void record(Direction direction, PacketType type, std::size_t bytes,
65 Clock::time_point now) noexcept;
66
71 void record(Direction direction, std::size_t bytes, Clock::time_point now) noexcept;
72
76 [[nodiscard]] TrafficCount total(Direction direction) const noexcept;
77
82 [[nodiscard]] TrafficCount total(Direction direction, PacketType type) const noexcept;
83
88 [[nodiscard]] TrafficCount last_second(Direction direction,
89 Clock::time_point now) const noexcept;
90
96 [[nodiscard]] TrafficCount last_second(Direction direction, PacketType type,
97 Clock::time_point now) const noexcept;
98
99private:
101 static constexpr std::size_t slots = 8;
102 using Table = std::array<std::array<TrafficCount, slots>, 2>;
103
104 void count(Direction direction, std::size_t slot, std::size_t bytes,
105 Clock::time_point now) noexcept;
106 [[nodiscard]] TrafficCount window(Direction direction, std::size_t first, std::size_t last,
107 Clock::time_point now) const noexcept;
108
109 Table total_{};
110 Table current_{};
111 Table previous_{};
112 Clock::time_point window_start_{};
113 bool started_ = false;
114};
115
116} // namespace aegis2d::net
Counts the datagrams exchanged with one peer, by direction and packet type.
Definition traffic.hpp:54
void record(Direction direction, PacketType type, std::size_t bytes, Clock::time_point now) noexcept
Records a datagram whose header was read.
TrafficCount last_second(Direction direction, Clock::time_point now) const noexcept
Every datagram of a direction during the last whole second.
TrafficCount last_second(Direction direction, PacketType type, Clock::time_point now) const noexcept
The datagrams of one type and direction during the last whole second.
void record(Direction direction, std::size_t bytes, Clock::time_point now) noexcept
Records a datagram of unknown type, such as one too short for a header.
TrafficCount total(Direction direction) const noexcept
Every datagram of a direction since the counters were created.
TrafficCount total(Direction direction, PacketType type) const noexcept
The datagrams of one type and direction since the counters were created.
std::chrono::steady_clock Clock
Clock of the one-second windows.
Definition traffic.hpp:57
Binary serialization, UDP/TCP sockets and the network thread.
Definition binary.hpp:14
Direction
Way a datagram went.
Definition traffic.hpp:17
@ Sent
Sent to the peer.
@ Received
Received from the peer.
PacketType
What a datagram carries; stored on one byte after the version.
Definition packet.hpp:22
Header carried by every UDP datagram, and the packet types.
A number of datagrams and the bytes they hold.
Definition traffic.hpp:25
std::uint64_t datagrams
Datagrams.
Definition traffic.hpp:27
std::uint64_t bytes
Bytes of the datagrams, the 18-byte header included; the IP and UDP headers (28 bytes in IPv4,...
Definition traffic.hpp:30
TrafficCount & operator+=(const TrafficCount &other) noexcept
Adds another count.
Definition traffic.hpp:35
bool operator==(const TrafficCount &) const =default
Field-by-field equality.