Plasma Engine  2.0
Loading...
Searching...
No Matches
Telemetry.h
1#pragma once
2
3#include <Foundation/Communication/Implementation/TelemetryMessage.h>
4#include <Foundation/Containers/Deque.h>
5#include <Foundation/Containers/HybridArray.h>
6#include <Foundation/Containers/Map.h>
7#include <Foundation/Logging/Log.h>
8#include <Foundation/Strings/String.h>
9#include <Foundation/Threading/Mutex.h>
10#include <Foundation/Time/Time.h>
11
12
14class PL_FOUNDATION_DLL plTelemetry
15{
16public:
18 static plUInt16 s_uiPort /* = 1040*/;
19
27
30 {
32 Unreliable,
34 };
35
38
47 static plResult ConnectToServer(plStringView sConnectTo = {});
48
52 static void CreateServer();
53
58 static void CloseConnection();
59
60
62
65
66 static void Broadcast(TransmitMode tm, plUInt32 uiSystemID, plUInt32 uiMsgID, const void* pData, plUInt32 uiDataBytes);
67 static void Broadcast(TransmitMode tm, plUInt32 uiSystemID, plUInt32 uiMsgID, plStreamReader& inout_stream, plInt32 iDataBytes = -1);
68 static void Broadcast(TransmitMode tm, plTelemetryMessage& ref_msg);
69
70 static void SendToServer(plUInt32 uiSystemID, plUInt32 uiMsgID, const void* pData = nullptr, plUInt32 uiDataBytes = 0);
71 static void SendToServer(plUInt32 uiSystemID, plUInt32 uiMsgID, plStreamReader& inout_stream, plInt32 iDataBytes = -1);
72 static void SendToServer(plTelemetryMessage& ref_msg);
73
75
78
80 static ConnectionMode GetConnectionMode() { return s_ConnectionMode; }
81
83 static bool IsConnectedToServer() { return s_bConnectedToServer; }
84
86 static bool IsConnectedToClient() { return s_bConnectedToClient; }
87
89 static bool IsConnectedToOther();
90
92 static plTime GetPingToServer() { return s_PingToServer; }
93
96 static plStringView GetServerName() { return s_sServerName; }
97
103 static void SetServerName(plStringView sName);
104
107 static plStringView GetServerIP() { return s_sServerIP; }
108
115 static plUInt32 GetServerID() { return s_uiServerID; }
116
122 static plMutex& GetTelemetryMutex();
123
125
128
137 static plResult RetrieveMessage(plUInt32 uiSystemID, plTelemetryMessage& out_message);
138
145 static void UpdateNetwork();
146
147 using ProcessMessagesCallback = void (*)(void*);
148
149 static void AcceptMessagesForSystem(plUInt32 uiSystemID, bool bAccept, ProcessMessagesCallback callback = nullptr, void* pPassThrough = nullptr);
150
152 static void PerFrameUpdate();
153
161 static void SetOutgoingQueueSize(plUInt32 uiSystemID, plUInt16 uiMaxQueued);
162
164
167
181
182 using plEventTelemetry = plEvent<const TelemetryEventData&, plMutex>;
183
185 static void AddEventHandler(plEventTelemetry::Handler handler) { s_TelemetryEvents.AddEventHandler(handler); }
186
188 static void RemoveEventHandler(plEventTelemetry::Handler handler) { s_TelemetryEvents.RemoveEventHandler(handler); }
189
191
192private:
193 static void UpdateServerPing();
194
195 static plResult OpenConnection(ConnectionMode Mode, plStringView sConnectTo = {});
196
197 static void Transmit(TransmitMode tm, const void* pData, plUInt32 uiDataBytes);
198
199 static void Send(TransmitMode tm, plUInt32 uiSystemID, plUInt32 uiMsgID, const void* pData, plUInt32 uiDataBytes);
200 static void Send(TransmitMode tm, plUInt32 uiSystemID, plUInt32 uiMsgID, plStreamReader& Stream, plInt32 iDataBytes = -1);
201 static void Send(TransmitMode tm, plTelemetryMessage& msg);
202
203 friend class plTelemetryThread;
204
205 static void FlushOutgoingQueues();
206
207 static void InitializeAsServer();
208 static plResult InitializeAsClient(plStringView sConnectTo);
209 static ConnectionMode s_ConnectionMode;
210
211 static plUInt32 s_uiApplicationID;
212 static plUInt32 s_uiServerID;
213
214 static plString s_sServerName;
215 static plString s_sServerIP;
216
217 static bool s_bConnectedToServer;
218 static bool s_bConnectedToClient;
219 static bool s_bAllowNetworkUpdate;
220
221 static void QueueOutgoingMessage(TransmitMode tm, plUInt32 uiSystemID, plUInt32 uiMsgID, const void* pData, plUInt32 uiDataBytes);
222
223 static void SendServerName();
224
225 static plTime s_PingToServer;
226
227 using MessageDeque = plDeque<plTelemetryMessage>;
228
229 struct MessageQueue
230 {
231 MessageQueue()
232 {
233 m_bAcceptMessages = false;
234 m_uiMaxQueuedOutgoing = 1000;
235 m_Callback = nullptr;
236 m_pPassThrough = nullptr;
237 }
238
239 bool m_bAcceptMessages;
240 ProcessMessagesCallback m_Callback;
241 void* m_pPassThrough;
242 plUInt32 m_uiMaxQueuedOutgoing;
243
244 MessageDeque m_IncomingQueue;
245 MessageDeque m_OutgoingQueue;
246 };
247
248 static plMap<plUInt64, MessageQueue> s_SystemMessages;
249
250 static plEventTelemetry s_TelemetryEvents;
251
252private:
253 static plMutex s_TelemetryMutex;
254 static void StartTelemetryThread();
255 static void StopTelemetryThread();
256};
Definition Deque.h:270
Definition Event.h:177
Definition Map.h:408
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition Mutex.h:13
Interface for binary in (read) streams.
Definition Stream.h:22
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
Definition Telemetry.h:15
static plTime GetPingToServer()
Returns the last round trip time ('Ping') to the Server. Only meaningful if there is an active connec...
Definition Telemetry.h:92
static bool IsConnectedToServer()
Returns whether a Client has an active connection to a Server.
Definition Telemetry.h:83
static plStringView GetServerIP()
Returns the IP address of the machine on which the Server is running. Only meaningful if there is an ...
Definition Telemetry.h:107
static void AddEventHandler(plEventTelemetry::Handler handler)
Adds an event handler that is called for every plTelemetry event.
Definition Telemetry.h:185
static bool IsConnectedToClient()
Returns whether a Server has an active connection to at least one Client.
Definition Telemetry.h:86
static plUInt32 GetServerID()
Returns a 'unique' ID for the application instance to which this Client is connected.
Definition Telemetry.h:115
static plStringView GetServerName()
Returns the name of the machine on which the Server is running. Only meaningful if there is an active...
Definition Telemetry.h:96
TransmitMode
Describes how to send messages.
Definition Telemetry.h:30
@ Reliable
Messages should definitely arrive at the target, if necessary they are send several times,...
Definition Telemetry.h:31
static void RemoveEventHandler(plEventTelemetry::Handler handler)
Removes a previously added event handler.
Definition Telemetry.h:188
static ConnectionMode GetConnectionMode()
Returns whether the telemetry system is set up as Server, Client or not initialized at all.
Definition Telemetry.h:80
ConnectionMode
Defines how the plTelemetry system was configured.
Definition Telemetry.h:22
@ Client
Set up as a Client, i.e. this is a tool that gathers information from a Server, usually for debugging...
Definition Telemetry.h:25
@ Server
Set up as a Server, i.e. this is an application that broadcasts information about its current state t...
Definition Telemetry.h:24
@ None
Not configured yet, at all.
Definition Telemetry.h:23
Definition TelemetryMessage.h:6
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
Definition Telemetry.h:169
EventType
Definition Telemetry.h:171
@ DisconnectedFromClient
Send every time the connection to a client is dropped.
Definition Telemetry.h:174
@ ConnectedToClient
brief Send whenever a new connection to a client has been established.
Definition Telemetry.h:172
@ ConnectedToServer
brief Send whenever a connection to the server has been established.
Definition Telemetry.h:173
@ PerFrameUpdate
Send once per frame, react to this to send per-frame statistics.
Definition Telemetry.h:176
@ DisconnectedFromServer
Send when the connection to the server has been lost.
Definition Telemetry.h:175
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12