Plasma Engine  2.0
Loading...
Searching...
No Matches
IpcChannel.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/Communication/RemoteInterface.h>
5#include <Foundation/Threading/ThreadSignal.h>
6#include <Foundation/Types/UniquePtr.h>
7
8class plIpcChannel;
9class plMessageLoop;
10
12struct PL_FOUNDATION_DLL plIpcChannelEvent
13{
21
22 plIpcChannelEvent() = default;
23
24 plIpcChannelEvent(Type type, plIpcChannel* pChannel)
25 : m_Type(type)
26 , m_pChannel(pChannel)
27 {
28 }
29
30 Type m_Type = NewMessages;
31 plIpcChannel* m_pChannel = nullptr;
32};
33
34
35
43class PL_FOUNDATION_DLL plIpcChannel
44{
45public:
46 struct Mode
47 {
48 using StorageType = plUInt8;
49 enum Enum
50 {
51 Server,
52 Client,
53 Default = Server
54 };
55 };
56
58 {
59 using StorageType = plUInt8;
60 enum Enum
61 {
62 Disconnected,
64 Connected,
65 Default = Disconnected
66 };
67 };
68
69 virtual ~plIpcChannel();
70
74 static plInternal::NewInstance<plIpcChannel> CreatePipeChannel(plStringView sAddress, Mode::Enum mode);
75
76 static plInternal::NewInstance<plIpcChannel> CreateNetworkChannel(plStringView sAddress, Mode::Enum mode);
77
78
80 void Connect();
82 void Disconnect();
84 bool IsConnected() const { return m_iConnectionState == ConnectionState::Connected; }
87
89 bool Send(plArrayPtr<const plUInt8> data);
90
91 using ReceiveCallback = plDelegate<void(plArrayPtr<const plUInt8> message)>;
92 void SetReceiveCallback(ReceiveCallback callback);
93
95 plResult WaitForMessages(plTime timeout);
96
97public:
99
100protected:
101 plIpcChannel(plStringView sAddress, Mode::Enum mode);
102
104 virtual bool RequiresRegularTick() { return false; }
106 virtual void Tick() {}
107
109 virtual void InternalConnect() = 0;
111 virtual void InternalDisconnect() = 0;
113 virtual void InternalSend() = 0;
115 virtual bool NeedWakeup() const = 0;
116
117 void SetConnectionState(plEnum<ConnectionState> state);
120 void ReceiveData(plArrayPtr<const plUInt8> data);
121 void FlushPendingOperations();
122
123private:
124protected:
125 enum Constants : plUInt32
126 {
127 HEADER_SIZE = 8,
128 MAGIC_VALUE = 'USED',
129 MAX_MESSAGE_SIZE = 1024 * 1024 * 16,
130 };
131
132 friend class plMessageLoop;
133 plThreadID m_ThreadId = 0;
134
135 plAtomicInteger<ConnectionState::Enum> m_iConnectionState = ConnectionState::Disconnected;
136
137 // Setup in ctor
138 plString m_sAddress;
139 const plEnum<Mode> m_Mode;
140 plMessageLoop* m_pOwner = nullptr;
141
142 // Mutex locked
143 plMutex m_OutputQueueMutex;
145
146 // Only accessed from worker thread
148
149 // Mutex locked
150 plMutex m_ReceiveCallbackMutex;
151 ReceiveCallback m_ReceiveCallback;
152 plThreadSignal m_IncomingMessages;
153};
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
Integer class that can be manipulated in an atomic (i.e. thread-safe) fashion.
Definition AtomicInteger.h:39
Definition Deque.h:270
Definition DynamicArray.h:81
Definition Event.h:177
Base class for a communication channel between processes.
Definition IpcChannel.h:44
bool IsConnected() const
Returns whether we have a connection.
Definition IpcChannel.h:84
plDynamicArray< plUInt8 > m_MessageAccumulator
Message is assembled in here.
Definition IpcChannel.h:147
plEnum< ConnectionState > GetConnectionState() const
Returns the current state of the connection.
Definition IpcChannel.h:86
virtual bool RequiresRegularTick()
Override this and return true, if the surrounding infrastructure should call the 'Tick()' function.
Definition IpcChannel.h:104
plEvent< const plIpcChannelEvent &, plMutex > m_Events
Will be sent from any thread.
Definition IpcChannel.h:98
virtual void InternalConnect()=0
Called on worker thread after Connect was called.
virtual void InternalDisconnect()=0
Called on worker thread after Disconnect was called.
virtual void Tick()
Can implement regular updates, e.g. for polling network state.
Definition IpcChannel.h:106
Constants
Definition IpcChannel.h:126
virtual bool NeedWakeup() const =0
Called by Send to determine whether the message loop need to be woken up.
virtual void InternalSend()=0
Called on worker thread to sent pending messages.
Internal sub-system used by plIpcChannel.
Definition MessageLoop.h:20
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition Mutex.h:13
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
Waiting on a thread signal puts the waiting thread to sleep. Other threads can wake it up by raising ...
Definition ThreadSignal.h:19
A generic delegate class which supports static functions and member functions.
Definition Delegate.h:76
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Definition Allocator_inl.h:18
Definition IpcChannel.h:58
Enum
Definition IpcChannel.h:61
@ Connecting
In case of the server, this state indicates that the server is ready to be connected to.
Definition IpcChannel.h:63
Definition IpcChannel.h:47
Event data for plIpcChannel::m_Events.
Definition IpcChannel.h:13
Type
Definition IpcChannel.h:15
@ NewMessages
Sent when a new messages have been received or when disconnected to wake up any thread waiting for me...
Definition IpcChannel.h:19
@ Disconnected
Server or client are in a dorment state.
Definition IpcChannel.h:16
@ Connected
Client and server are connected to each other.
Definition IpcChannel.h:18
@ Connecting
The server is listening for clients or the client is trying to find the server.
Definition IpcChannel.h:17
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