Plasma Engine  2.0
Loading...
Searching...
No Matches
RemoteInterface.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/Communication/Event.h>
5#include <Foundation/Communication/RemoteMessage.h>
6#include <Foundation/Containers/Deque.h>
7#include <Foundation/Containers/HashTable.h>
8#include <Foundation/Threading/Mutex.h>
9#include <Foundation/Threading/Thread.h>
10#include <Foundation/Time/Time.h>
11#include <Foundation/Types/Delegate.h>
12
14enum class plRemoteMode
15{
16 None,
17 Server,
18 Client
19};
20
24enum class plRemoteTransmitMode
25{
26 Reliable,
27 Unreliable,
29};
30
32struct PL_FOUNDATION_DLL plRemoteEvent
33{
41
42 Type m_Type;
43 plUInt32 m_uiOtherAppID;
44};
45
47
58
59class PL_FOUNDATION_DLL plRemoteInterface
60{
61public:
62 virtual ~plRemoteInterface();
63
65 plMutex& GetMutex() const { return m_Mutex; }
66
67
70
78 plResult StartServer(plUInt32 uiConnectionToken, plStringView sAddress, bool bStartUpdateThread = true);
79
89 plResult ConnectToServer(plUInt32 uiConnectionToken, plStringView sAddress, bool bStartUpdateThread = true);
90
94 plResult WaitForConnectionToServer(plTime timeout = plTime::MakeFromSeconds(10));
95
97 void ShutdownConnection();
98
100 bool IsConnectedToServer() const { return m_uiConnectedToServerWithID != 0; }
101
103 bool IsConnectedToClients() const { return m_iConnectionsToClients > 0; }
104
106 bool IsConnectedToOther() const { return IsConnectedToServer() || IsConnectedToClients(); }
107
109 plRemoteMode GetRemoteMode() const { return m_RemoteMode; }
110
112 const plString& GetServerAddress() const { return m_sServerAddress; }
113
115 plUInt32 GetApplicationID() const { return m_uiApplicationID; }
116
118 plUInt32 GetConnectionToken() const { return m_uiConnectionToken; }
119
121
124
126 // const plString& GetServerInfoName() const { return m_ServerInfoName; }
127
129 const plString& GetServerInfoIP() const { return m_sServerInfoIP; }
130
132 plUInt32 GetServerID() const { return m_uiConnectedToServerWithID; }
133
135 plTime GetPingToServer() const { return m_PingToServer; }
136
138
141
143 void UpdateRemoteInterface();
144
146 void UpdatePingToServer();
147
149
152
156 void Send(plUInt32 uiSystemID, plUInt32 uiMsgID);
157
161 void Send(plRemoteTransmitMode tm, plUInt32 uiSystemID, plUInt32 uiMsgID, const plArrayPtr<const plUInt8>& data);
162
163 void Send(plRemoteTransmitMode tm, plUInt32 uiSystemID, plUInt32 uiMsgID, const plContiguousMemoryStreamStorage& data);
164
168 void Send(plRemoteTransmitMode tm, plUInt32 uiSystemID, plUInt32 uiMsgID, const void* pData = nullptr, plUInt32 uiDataBytes = 0);
169
173 void Send(plRemoteTransmitMode tm, plRemoteMessage& ref_msg);
174
176
179
181 void SetMessageHandler(plUInt32 uiSystemID, plRemoteMessageHandler messageHandler);
182
184 void SetUnhandledMessageHandler(plRemoteMessageHandler messageHandler);
185
187 plUInt32 ExecuteMessageHandlers(plUInt32 uiSystem);
188
190 plUInt32 ExecuteAllMessageHandlers();
191
193
196
199
201
202protected:
205
207 virtual plResult InternalCreateConnection(plRemoteMode mode, plStringView sServerAddress) = 0;
208
210 virtual void InternalShutdownConnection() = 0;
211
214
217
219 virtual plResult InternalTransmit(plRemoteTransmitMode tm, const plArrayPtr<const plUInt8>& data) = 0;
220
222 virtual plResult DetermineTargetAddress(plStringView sConnectTo, plUInt32& out_IP, plUInt16& out_Port);
223
225 // plString m_ServerInfoName;
228
230 void ReportConnectionToServer(plUInt32 uiServerID);
232 void ReportConnectionToClient(plUInt32 uiApplicationID);
234 void ReportDisconnectedFromServer();
236 void ReportDisconnectedFromClient(plUInt32 uiApplicationID);
238 void ReportMessage(plUInt32 uiApplicationID, plUInt32 uiSystemID, plUInt32 uiMsgID, const plArrayPtr<const plUInt8>& data);
239
241
242
243private:
244 void StartUpdateThread();
245 void StopUpdateThread();
246 plResult Transmit(plRemoteTransmitMode tm, const plArrayPtr<const plUInt8>& data);
247 plResult CreateConnection(plUInt32 uiConnectionToken, plRemoteMode mode, plStringView sServerAddress, bool bStartUpdateThread);
248 plUInt32 ExecuteMessageHandlersForQueue(plRemoteMessageQueue& queue);
249
250 mutable plMutex m_Mutex;
251 class plRemoteThread* m_pUpdateThread = nullptr;
252 plRemoteMode m_RemoteMode = plRemoteMode::None;
253 plString m_sServerAddress;
254 plTime m_PingToServer;
255 plUInt32 m_uiApplicationID = 0; // sent when connecting to identify the sending instance
256 plUInt32 m_uiConnectionToken = 0;
257 plUInt32 m_uiConnectedToServerWithID = 0;
258 plInt32 m_iConnectionsToClients = 0;
259 plDynamicArray<plUInt8> m_TempSendBuffer;
261 plRemoteMessageHandler m_UnhandledMessageHandler;
262};
263
268class PL_FOUNDATION_DLL plRemoteThread : public plThread
269{
270public:
272
273 plRemoteInterface* m_pRemoteInterface = nullptr;
274 volatile bool m_bKeepRunning = true;
275
276private:
277 virtual plUInt32 Run();
278};
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
Definition MemoryStream.h:145
Definition Deque.h:270
Definition DynamicArray.h:81
Definition Event.h:177
Definition HashTable.h:333
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition Mutex.h:13
Definition RemoteInterface.h:60
virtual void InternalUpdateRemoteInterface()=0
Derived classes have to implement this to update.
plRemoteMode GetRemoteMode() const
Whether the remote interface is inactive, a client or a server.
Definition RemoteInterface.h:109
virtual plResult InternalCreateConnection(plRemoteMode mode, plStringView sServerAddress)=0
Derived classes have to implement this to start a network connection.
plUInt32 GetServerID() const
Some random identifier, that allows to determine after a reconnect, whether the connected instance is...
Definition RemoteInterface.h:132
plTime GetPingToServer() const
Returns the current ping to the server.
Definition RemoteInterface.h:135
virtual plTime InternalGetPingToServer()=0
Derived classes have to implement this to get the ping to the server (client mode only)
const plString & GetServerInfoIP() const
For the client to display the name of the server.
Definition RemoteInterface.h:129
virtual plResult InternalTransmit(plRemoteTransmitMode tm, const plArrayPtr< const plUInt8 > &data)=0
Derived classes have to implement this to deliver messages to the server or client.
virtual void InternalShutdownConnection()=0
Derived classes have to implement this to shutdown a network connection.
plEvent< const plRemoteEvent & > m_RemoteEvents
Broadcasts events about connections.
Definition RemoteInterface.h:198
plUInt32 GetConnectionToken() const
Returns the connection token used to identify compatible servers/clients.
Definition RemoteInterface.h:118
bool IsConnectedToServer() const
Whether the client is connected to a server.
Definition RemoteInterface.h:100
const plString & GetServerAddress() const
The address through which the connection was started.
Definition RemoteInterface.h:112
bool IsConnectedToClients() const
Whether the server is connected to any client.
Definition RemoteInterface.h:103
plString m_sServerInfoIP
Derived classes should update this when the information is available.
Definition RemoteInterface.h:227
bool IsConnectedToOther() const
Whether the client or server is connected its counterpart.
Definition RemoteInterface.h:106
plMutex & GetMutex() const
Exposes the mutex that is internally used to secure multi-threaded access.
Definition RemoteInterface.h:65
plUInt32 GetApplicationID() const
Returns the own (random) application ID used to identify this instance.
Definition RemoteInterface.h:115
Encapsulates all the data that is transmitted when sending or receiving a message with plRemoteInterf...
Definition RemoteMessage.h:11
The remote interface thread updates in regular intervals to keep the connection alive.
Definition RemoteInterface.h:269
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
This class is the base class for platform independent long running threads.
Definition Thread.h:40
Event type for connections.
Definition RemoteInterface.h:33
Type
Definition RemoteInterface.h:35
@ ConnectedToClient
brief Sent whenever a new connection to a client has been established.
Definition RemoteInterface.h:36
@ ConnectedToServer
brief Sent whenever a connection to the server has been established.
Definition RemoteInterface.h:37
@ DisconnectedFromClient
Sent every time the connection to a client is dropped.
Definition RemoteInterface.h:38
@ DisconnectedFromServer
Sent when the connection to the server has been lost.
Definition RemoteInterface.h:39
Definition RemoteInterface.h:49
plDeque< plRemoteMessage > m_MessageQueueOut
To flush the message queue, m_MessageQueueIn and m_MessageQueueOut are swapped. Thus new messages can...
Definition RemoteInterface.h:56
plDeque< plRemoteMessage > m_MessageQueueIn
Messages are pushed into this container on arrival.
Definition RemoteInterface.h:52
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
PL_ALWAYS_INLINE static constexpr plTime MakeFromSeconds(double fSeconds)
Creates an instance of plTime that was initialized from seconds.
Definition Time.h:30