r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Event.hpp
Go to the documentation of this file.
1///
2/// @file Event.hpp
3/// @brief Event structures and types for event-driven communication
4/// @namespace utl
5///
6
7#pragma once
8
9#include <chrono>
10#include <cstdint>
11#include <unordered_map>
12#include <vector>
13
14namespace utl
15{
16
17 static constexpr std::uint32_t NETWORK_CLIENT = 1;
18 static constexpr std::uint32_t NETWORK_SERVER = 2;
19 static constexpr std::uint32_t GAME_LOGIC = 3;
20 static constexpr std::uint32_t RENDERING_ENGINE = 4;
21
22 ///
23 /// @brief Event types for inter-component communication
24 ///
25 enum class EventType : std::uint32_t
26 {
27 // Network → Game events (0x1000 - 0x1FFF)
28 WORLD_STATE_RECEIVED = 0x1000,
30 PLAYER_CONNECTED = 0x1002,
31 PLAYER_DISCONNECTED = 0x1003,
32 PLAYER_INPUT_RECEIVED = 0x1010,
33 ENTITY_EVENT_RECEIVED = 0x1011,
34 CONNECTION_ACCEPTED = 0x1020,
35 CONNECTION_REJECTED = 0x1021,
36 SERVER_DISCONNECTED = 0x1022,
37
38 // Game → Network events (0x2000 - 0x2FFF)
39 SEND_PLAYER_INPUT = 0x2000,
40 SEND_ENTITY_EVENT = 0x2001,
41 SEND_ENTITY_EVENTS = 0x2002,
42 BROADCAST_WORLD_STATE = 0x2003,
43 BROADCAST_TO_CLIENTS = 0x2004,
44 SEND_TO_CLIENT = 0x2010,
45 DISCONNECT_CLIENT = 0x2011,
46 REQUEST_CONNECT = 0x2020,
47 REQUEST_DISCONNECT = 0x2021,
48
49 // Server Management events (0x3000 - 0x3FFF)
50 SERVER_START = 0x3000,
51 SERVER_STOP = 0x3001,
52 SERVER_STATUS_UPDATE = 0x3002,
53
54 // Lobby Management events (0x4000 - 0x4FFF)
55 LOBBY_LIST_REQUEST = 0x4000,
56 LOBBY_LIST_RESPONSE = 0x4001,
57 LOBBY_CREATE = 0x4010,
58 LOBBY_CREATE_RESPONSE = 0x4011,
59 LOBBY_JOIN = 0x4020,
60 LOBBY_JOIN_RESPONSE = 0x4021,
61 LOBBY_LEAVE = 0x4030,
62 LOBBY_UPDATE = 0x4040,
63 GAME_START = 0x4050,
64 GAME_OVER = 0x4060,
65 };
66
67 ///
68 /// @brief Event priority levels
69 ///
70 enum class EventPriority : std::uint8_t
71 {
72 LOW = 0,
73 NORMAL = 1,
74 HIGH = 2,
75 CRITICAL = 3
76 };
77
79 {
80 std::string playerName;
81 std::string serverIP;
82 std::string serverPort;
83 };
84
85 ///
86 /// @brief Event structure for inter-component communication
87 ///
88 class Event
89 {
90 public:
91 EventType type; ///< Type of the event
92 std::uint32_t sourceId = 0; ///< ID of the component that sent the event (0 = system)
93 std::uint32_t targetId = 0; ///< ID of the target component (0 = broadcast)
95 std::chrono::steady_clock::time_point timestamp; ///< Timestamp when event was created
96 std::vector<std::uint8_t> data; ///< Serialized event data
97
98 ///
99 /// @brief Default constructor with current timestamp
100 ///
101 Event() : type(), timestamp(std::chrono::steady_clock::now()) {}
102
103 ///
104 /// @brief Constructor with event type
105 /// @param t Event type
106 ///
107 explicit Event(EventType t) : type(t), timestamp(std::chrono::steady_clock::now()) {}
108
109 ///
110 /// @brief Constructor with event type and data
111 /// @param t Event type
112 /// @param d Event data
113 ///
114 Event(EventType t, const std::vector<std::uint8_t> &d)
115 : type(t), timestamp(std::chrono::steady_clock::now()), data(d)
116 {
117 }
118
119 ///
120 /// @brief Constructor with all parameters
121 /// @param t Event type
122 /// @param src Source component ID
123 /// @param tgt Target component ID (0 for broadcast)
124 /// @param prio Event priority
125 ///
126 Event(EventType t, std::uint32_t src, std::uint32_t tgt = 0, EventPriority prio = EventPriority::NORMAL)
127 : type(t), sourceId(src), targetId(tgt), priority(prio), timestamp(std::chrono::steady_clock::now())
128 {
129 }
130
131 ///
132 /// @brief Get event age in milliseconds
133 /// @return Age in milliseconds
134 ///
135 std::uint64_t getAge() const
136 {
137 auto now = std::chrono::steady_clock::now();
138 return static_cast<std::uint64_t>(
139 std::chrono::duration_cast<std::chrono::milliseconds>(now - timestamp).count());
140 }
141
142 ///
143 /// @brief Check if event has expired
144 /// @param maxAgeMs Maximum age in milliseconds
145 /// @return True if event has expired
146 ///
147 bool hasExpired(std::uint64_t maxAgeMs = 5000) const { return getAge() > maxAgeMs; }
148
149 ///
150 /// @brief Check if event is targeted to a specific component
151 /// @return True if event has a specific target
152 ///
153 bool isTargeted() const { return targetId != 0; }
154
155 ///
156 /// @brief Check if event is a broadcast
157 /// @return True if event is broadcast to all components
158 ///
159 bool isBroadcast() const { return targetId == 0; }
160 };
161
162 ///
163 /// @brief Event statistics structure
164 ///
166 {
167 public:
168 std::uint64_t totalEventsPublished = 0; ///< Total events published
169 std::uint64_t totalEventsConsumed = 0; ///< Total events consumed
170 std::uint64_t totalEventsExpired = 0; ///< Total events that expired
171 std::uint64_t currentQueueSize = 0; ///< Current queue size
172 std::uint64_t maxQueueSize = 0; ///< Maximum queue size reached
173 std::chrono::steady_clock::time_point startTime; ///< Statistics start time
174 std::unordered_map<EventType, std::uint64_t> eventTypeCount; ///< Count per event type
175 std::unordered_map<EventPriority, std::uint64_t> priorityCount; ///< Count per priority
176 std::unordered_map<std::uint32_t, std::uint64_t> sourceCount; ///< Count per source component
177 std::unordered_map<std::uint32_t, std::uint64_t> targetCount; ///< Count per target component
178
179 ///
180 /// @brief Reset all statistics
181 ///
182 void reset()
183 {
188 maxQueueSize = 0;
189 startTime = std::chrono::steady_clock::now();
190 eventTypeCount.clear();
191 priorityCount.clear();
192 sourceCount.clear();
193 targetCount.clear();
194 }
195
196 ///
197 /// @brief Get events per second rate
198 /// @return Events per second
199 ///
200 double getEventsPerSecond() const
201 {
202 auto elapsed =
203 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - startTime);
204 if (elapsed.count() == 0)
205 return 0.0;
206 return static_cast<double>(totalEventsPublished) / elapsed.count();
207 }
208 };
209
210} // namespace utl
Event statistics structure.
Definition Event.hpp:166
std::uint64_t totalEventsConsumed
Total events consumed.
Definition Event.hpp:169
std::unordered_map< std::uint32_t, std::uint64_t > targetCount
Count per target component.
Definition Event.hpp:177
std::uint64_t totalEventsPublished
Total events published.
Definition Event.hpp:168
std::uint64_t maxQueueSize
Maximum queue size reached.
Definition Event.hpp:172
std::uint64_t currentQueueSize
Current queue size.
Definition Event.hpp:171
std::unordered_map< EventPriority, std::uint64_t > priorityCount
Count per priority.
Definition Event.hpp:175
double getEventsPerSecond() const
Get events per second rate.
Definition Event.hpp:200
std::chrono::steady_clock::time_point startTime
Statistics start time.
Definition Event.hpp:173
std::unordered_map< EventType, std::uint64_t > eventTypeCount
Count per event type.
Definition Event.hpp:174
std::uint64_t totalEventsExpired
Total events that expired.
Definition Event.hpp:170
void reset()
Reset all statistics.
Definition Event.hpp:182
std::unordered_map< std::uint32_t, std::uint64_t > sourceCount
Count per source component.
Definition Event.hpp:176
Event structure for inter-component communication.
Definition Event.hpp:89
std::uint64_t getAge() const
Get event age in milliseconds.
Definition Event.hpp:135
bool isTargeted() const
Check if event is targeted to a specific component.
Definition Event.hpp:153
std::uint32_t sourceId
ID of the component that sent the event (0 = system)
Definition Event.hpp:92
std::vector< std::uint8_t > data
Serialized event data.
Definition Event.hpp:96
std::chrono::steady_clock::time_point timestamp
Timestamp when event was created.
Definition Event.hpp:95
EventType type
Type of the event.
Definition Event.hpp:91
std::uint32_t targetId
ID of the target component (0 = broadcast)
Definition Event.hpp:93
Event()
Default constructor with current timestamp.
Definition Event.hpp:101
Event(EventType t, std::uint32_t src, std::uint32_t tgt=0, EventPriority prio=EventPriority::NORMAL)
Constructor with all parameters.
Definition Event.hpp:126
EventPriority priority
Event priority.
Definition Event.hpp:94
Event(EventType t)
Constructor with event type.
Definition Event.hpp:107
Event(EventType t, const std::vector< std::uint8_t > &d)
Constructor with event type and data.
Definition Event.hpp:114
bool hasExpired(std::uint64_t maxAgeMs=5000) const
Check if event has expired.
Definition Event.hpp:147
bool isBroadcast() const
Check if event is a broadcast.
Definition Event.hpp:159
static constexpr std::uint32_t GAME_LOGIC
Definition Event.hpp:19
EventType
Event types for inter-component communication.
Definition Event.hpp:26
static constexpr std::uint32_t RENDERING_ENGINE
Definition Event.hpp:20
EventPriority
Event priority levels.
Definition Event.hpp:71
static constexpr std::uint32_t NETWORK_SERVER
Definition Event.hpp:18
static constexpr std::uint32_t NETWORK_CLIENT
Definition Event.hpp:17
std::string serverIP
Definition Event.hpp:81
std::string playerName
Definition Event.hpp:80
std::string serverPort
Definition Event.hpp:82