cello
JUCE ValueTrees for Humans
Loading...
Searching...
No Matches
cello_ipc.h
1/*
2 Copyright (c) 2023 Brett g Porter
3 Permission is hereby granted, free of charge, to any person obtaining a copy
4 of this software and associated documentation files (the "Software"), to deal
5 in the Software without restriction, including without limitation the rights
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
9 The above copyright notice and this permission notice shall be included in all
10 copies or substantial portions of the Software.
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 SOFTWARE.
18*/
19
20#pragma once
21
22#include <juce_events/juce_events.h>
23
24#include "cello_object.h"
25#include "cello_sync.h"
26#include "cello_value.h"
27
28namespace cello
29{
30
36struct IpcClientProperties : public Object
37{
38 IpcClientProperties (const juce::String& name, Object* state)
39 : cello::Object (name + "IpcClient", state)
40 {
41 }
42 MAKE_VALUE_MEMBER (bool, connected, false);
43 MAKE_VALUE_MEMBER (int, rxCount, 0);
44 MAKE_VALUE_MEMBER (int, txCount, 0);
45};
46
47//==============================================================================
48
49class IpcClient : public juce::InterprocessConnection,
50 public juce::ValueTreeSynchroniser,
51 public UpdateQueue
52{
53public:
54 enum UpdateType
55 {
56 send = 0x01,
57 receive = 0x02,
58 fullUpdateOnConnect = 0x04
59 };
60
68
79 IpcClient (Object& objectToWatch, const juce::String& hostName, int portNum, int msTimeout, UpdateType updateType,
80 Object* state = nullptr);
81
91 IpcClient (Object& objectToWatch, const juce::String& pipeName, int msTimeout, UpdateType updateType,
92 Object* state = nullptr);
93
94 ~IpcClient () override;
95
104
105private:
106 friend class IpcServer;
118 IpcClient (Object& objectToWatch, UpdateType updateType, const juce::String& hostName, int portNum,
119 const juce::String& pipeName, int msTimeout, Object* state = nullptr);
120
121 void connectionMade () override;
122 void connectionLost () override;
129 void messageReceived (const juce::MemoryBlock& message) override;
130
138 void stateChanged (const void* encodedChange, size_t encodedSize) override;
139 // !!! IMPLEMENT THESE
140 void startUpdate (void* data, size_t size) override;
141 void endUpdate () override;
142
143
144private:
146 IpcClientProperties clientProperties;
147
149 UpdateType update;
150
152 const juce::String host;
154 const int port;
156 const juce::String pipe;
158 const int timeout;
159
160 SyncData updateData;
161};
162
163//==============================================================================
164
165enum IpcServerStatus
166{
167 unknown,
168 initialized,
169 startedOkay,
170 alreadyRunning,
171 errorStarting,
172 stoppedOkay,
173 alreadyStopped,
174 errorStopping
175};
176
177struct IpcServerProperties : public Object
178{
179 IpcServerProperties (const juce::String& path, Object* state);
190 void startServer (int portNum, const juce::String& address = juce::String ());
191
198 void stopServer ();
199
201 MAKE_VALUE_MEMBER (bool, running, false);
202
204 MAKE_VALUE_MEMBER (IpcServerStatus, status, IpcServerStatus::unknown);
205
207 MAKE_VALUE_MEMBER (int, portNumber, 0);
208
210 MAKE_VALUE_MEMBER (juce::String, bindAddress, {});
211};
212
213//==============================================================================
214
215class IpcServer : public juce::InterprocessConnectionServer
216{
217public:
218 IpcServer (Object& sync, IpcClient::UpdateType updateType, const juce::String& statePath, Object* state = nullptr);
219 ~IpcServer () override;
220
229 bool startServer (int portNumber, const juce::String& bindAddress = juce::String ());
230
236 bool stopServer ();
237
238protected:
249 juce::InterprocessConnection* createConnectionObject () override;
250
251private:
253 Object& syncObject;
254
256 IpcClient::UpdateType update;
257
259 std::vector<std::unique_ptr<juce::InterprocessConnection>> connections;
260
263 IpcServerProperties serverProperties;
264};
265
266} // namespace cello
Definition cello_ipc.h:52
IpcClient(Object &objectToWatch, const juce::String &hostName, int portNum, int msTimeout, UpdateType updateType, Object *state=nullptr)
Construct a new Ipc Client object that connects using sockets.
Definition cello_ipc.cpp:64
bool connect(ConnectOptions option=ConnectOptions::noOptions)
Attempt to make a connection to another IpcClient running in another process.
Definition cello_ipc.cpp:83
ConnectOptions
Definition cello_ipc.h:62
@ mustExist
pipe must already exist, fail if it doesn't
Definition cello_ipc.h:65
@ noOptions
default, used for sockets, which have no options.
Definition cello_ipc.h:63
@ createIfNeeded
If pipe exists, use it, otherwise create.
Definition cello_ipc.h:66
@ createOrFail
create the pipe, fail if we couldn't
Definition cello_ipc.h:64
Definition cello_ipc.h:216
bool startServer(int portNumber, const juce::String &bindAddress=juce::String())
Launch the thread that starts listening for incoming socket connections.
Definition cello_ipc.cpp:216
bool stopServer()
Stop the server.
Definition cello_ipc.cpp:236
juce::InterprocessConnection * createConnectionObject() override
When we get a connection, the base server class will call this so that we can create and return an in...
Definition cello_ipc.cpp:253
Definition cello_object.h:34
Object(const juce::String &type, const Object *state)
Construct a new cello::Object object, which will attempt to initialize from the 'state' parameter....
Definition cello_object.cpp:27
Properties struct to monitor an IPC client. Created automatically when creating an IpcClient object,...
Definition cello_ipc.h:37
Definition cello_ipc.h:178
MAKE_VALUE_MEMBER(bool, running, false)
will be updated whenever the server is started or stopped.
MAKE_VALUE_MEMBER(juce::String, bindAddress, {})
address to bind to, optional, see the JUCE docs
void stopServer()
Tell the server object we're controlling to stop.
Definition cello_ipc.cpp:179
MAKE_VALUE_MEMBER(int, portNumber, 0)
TCP port number to use (or in use, if we're running)
MAKE_VALUE_MEMBER(IpcServerStatus, status, IpcServerStatus::unknown)
will be updated with status info on an attempt to start/stop
void startServer(int portNum, const juce::String &address=juce::String())
Tell the server object we're controlling to start on the specified port (and optionally which address...
Definition cello_ipc.cpp:166
Data structure for holding synchronization update information.
Definition cello_sync.h:109