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
140private:
142 IpcClientProperties clientProperties;
143
145 UpdateType update;
146
148 const juce::String host;
150 const int port;
152 const juce::String pipe;
154 const int timeout;
155};
156
157//==============================================================================
158
159enum IpcServerStatus
160{
161 unknown,
162 initialized,
163 startedOkay,
164 alreadyRunning,
165 errorStarting,
166 stoppedOkay,
167 alreadyStopped,
168 errorStopping
169};
170
171struct IpcServerProperties : public Object
172{
173 IpcServerProperties (const juce::String& path, Object* state);
184 void startServer (int portNum, const juce::String& address = juce::String ());
185
192 void stopServer ();
193
195 MAKE_VALUE_MEMBER (bool, running, false);
196
198 MAKE_VALUE_MEMBER (IpcServerStatus, status, IpcServerStatus::unknown);
199
201 MAKE_VALUE_MEMBER (int, portNumber, 0);
202
204 MAKE_VALUE_MEMBER (juce::String, bindAddress, {});
205};
206
207//==============================================================================
208
209class IpcServer : public juce::InterprocessConnectionServer
210{
211public:
212 IpcServer (Object& sync, IpcClient::UpdateType updateType, const juce::String& statePath, Object* state = nullptr);
213 ~IpcServer () override;
214
223 bool startServer (int portNumber, const juce::String& bindAddress = juce::String ());
224
230 bool stopServer ();
231
232protected:
243 juce::InterprocessConnection* createConnectionObject () override;
244
245private:
247 Object& syncObject;
248
250 IpcClient::UpdateType update;
251
253 std::vector<std::unique_ptr<juce::InterprocessConnection>> connections;
254
257 IpcServerProperties serverProperties;
258};
259
260} // 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:210
bool startServer(int portNumber, const juce::String &bindAddress=juce::String())
Launch the thread that starts listening for incoming socket connections.
Definition cello_ipc.cpp:203
bool stopServer()
Stop the server.
Definition cello_ipc.cpp:223
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:240
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:172
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:166
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:153