ezEngine  Milestone 7
Telemetry.h
1 #pragma once
2 
3 #include <Foundation/Logging/Log.h>
4 #include <Foundation/Containers/Deque.h>
5 #include <Foundation/Containers/HybridArray.h>
6 #include <Foundation/Time/Time.h>
7 #include <Foundation/Strings/String.h>
8 #include <Foundation/Containers/Map.h>
9 #include <Foundation/Communication/Implementation/TelemetryMessage.h>
10 #include <Foundation/Threading/Mutex.h>
11 
12 
14 class EZ_FOUNDATION_DLL ezTelemetry
15 {
16 public:
17 
19  static ezUInt16 s_uiPort;
20 
23  {
24  None,
27  };
28 
31  {
34  };
35 
38 
47  static ezResult ConnectToServer(const char* szConnectTo = nullptr);
48 
52  static void CreateServer();
53 
58  static void CloseConnection();
59 
60 
62 
65 
66  static void Broadcast(TransmitMode tm, ezUInt32 uiSystemID, ezUInt32 uiMsgID, const void* pData, ezUInt32 uiDataBytes);
67  static void Broadcast(TransmitMode tm, ezUInt32 uiSystemID, ezUInt32 uiMsgID, ezStreamReaderBase& Stream, ezInt32 iDataBytes = -1);
68  static void Broadcast(TransmitMode tm, ezTelemetryMessage& Msg);
69 
70  static void SendToServer(ezUInt32 uiSystemID, ezUInt32 uiMsgID, const void* pData = nullptr, ezUInt32 uiDataBytes = 0);
71  static void SendToServer(ezUInt32 uiSystemID, ezUInt32 uiMsgID, ezStreamReaderBase& Stream, ezInt32 iDataBytes = -1);
72  static void SendToServer(ezTelemetryMessage& 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 ezTime GetPingToServer() { return s_PingToServer; }
93 
95  static const char* GetServerName() { return s_ServerName.GetData(); }
96 
98  static const char* GetServerIP() { return s_ServerIP.GetData(); }
99 
106  static ezUInt32 GetServerID() { return s_uiServerID; }
107 
113  static ezMutex& GetTelemetryMutex();
114 
116 
119 
128  static ezResult RetrieveMessage(ezUInt32 uiSystemID, ezTelemetryMessage& out_Message);
129 
136  static void UpdateNetwork();
137 
138  typedef void(*ProcessMessagesCallback)(void* pPassThrough);
139 
140  static void AcceptMessagesForSystem(ezUInt32 uiSystemID, bool bAccept, ProcessMessagesCallback Callback = nullptr, void* pPassThrough = nullptr);
141 
143  static void PerFrameUpdate();
144 
152  static void SetOutgoingQueueSize(ezUInt32 uiSystemID, ezUInt16 uiMaxQueued);
153 
155 
158 
160  {
162  {
168  };
169 
170  EventType m_EventType;
171  };
172 
173  typedef ezEvent<const TelemetryEventData&, ezMutex> ezEventTelemetry;
174 
176  static void AddEventHandler(ezEventTelemetry::Handler handler) { s_TelemetryEvents.AddEventHandler (handler); }
177 
179  static void RemoveEventHandler(ezEventTelemetry::Handler handler) { s_TelemetryEvents.RemoveEventHandler (handler); }
180 
182 
183 private:
184  static void UpdateServerPing();
185 
186  static ezResult OpenConnection(ConnectionMode Mode, const char* szConnectTo = nullptr);
187 
188  static void Transmit(TransmitMode tm, const void* pData, ezUInt32 uiDataBytes);
189 
190  static void Send(TransmitMode tm, ezUInt32 uiSystemID, ezUInt32 uiMsgID, const void* pData, ezUInt32 uiDataBytes);
191  static void Send(TransmitMode tm, ezUInt32 uiSystemID, ezUInt32 uiMsgID, ezStreamReaderBase& Stream, ezInt32 iDataBytes = -1);
192  static void Send(TransmitMode tm, ezTelemetryMessage& msg);
193 
194  friend class ezTelemetryThread;
195 
196  static void FlushOutgoingQueues();
197 
198  static void InitializeAsServer();
199  static ezResult InitializeAsClient(const char* szConnectTo);
200  static ConnectionMode s_ConnectionMode;
201 
202  static ezUInt32 s_uiApplicationID;
203  static ezUInt32 s_uiServerID;
204 
205  static ezString s_ServerName;
206  static ezString s_ServerIP;
207 
208  static bool s_bConnectedToServer;
209  static bool s_bConnectedToClient;
210  static bool s_bAllowNetworkUpdate;
211 
212  static void QueueOutgoingMessage(TransmitMode tm, ezUInt32 uiSystemID, ezUInt32 uiMsgID, const void* pData, ezUInt32 uiDataBytes);
213 
214  static ezTime s_PingToServer;
215 
216  typedef ezDeque<ezTelemetryMessage> MessageDeque;
217 
219  {
220  MessageQueue()
221  {
222  m_bAcceptMessages = false;
223  m_uiMaxQueuedOutgoing = 1000;
224  m_Callback = nullptr;
225  m_pPassThrough = nullptr;
226  }
227 
228  bool m_bAcceptMessages;
229  ProcessMessagesCallback m_Callback;
230  void* m_pPassThrough;
231  ezUInt32 m_uiMaxQueuedOutgoing;
232 
233  MessageDeque m_IncomingQueue;
234  MessageDeque m_OutgoingQueue;
235  };
236 
237  static ezMap<ezUInt64, MessageQueue> s_SystemMessages;
238 
239  static ezEventTelemetry s_TelemetryEvents;
240 
241 private:
242  static ezMutex s_TelemetryMutex;
243  static void StartTelemetryThread();
244  static void StopTelemetryThread();
245 };
246