29 void run()
override { owner.runThread(); }
41 template <
typename Fn>
70 using SafeActionImpl::SafeActionImpl;
89 jassert (! safeAction->isSafe());
91 callbackConnectionState =
false;
102 auto s = std::make_unique<StreamingSocket>();
107 initialiseWithSocket (std::move (s));
118 auto newPipe = std::make_unique<NamedPipe>();
123 pipeReceiveMessageTimeout = timeoutMs;
124 initialiseWithPipe (std::move (
newPipe));
135 auto newPipe = std::make_unique<NamedPipe>();
140 pipeReceiveMessageTimeout = timeoutMs;
141 initialiseWithPipe (std::move (
newPipe));
150 thread->signalThreadShouldExit();
154 if (socket !=
nullptr) socket->close();
155 if (pipe !=
nullptr) pipe->close();
158 thread->stopThread (timeoutMs);
159 deletePipeAndSocket();
161 if (notify == Notify::yes)
164 callbackConnectionState =
false;
165 safeAction->setSafe (
false);
168void InterprocessConnection::deletePipeAndSocket()
179 return ((socket !=
nullptr && socket->isConnected())
180 || (pipe !=
nullptr && pipe->isOpen()))
189 if (pipe ==
nullptr && socket ==
nullptr)
192 if (socket !=
nullptr && ! socket->isLocal())
193 return socket->getHostName();
212int InterprocessConnection::writeData (
void* data,
int dataSize)
216 if (socket !=
nullptr)
217 return socket->write (data, dataSize);
220 return pipe->write (data, dataSize, pipeReceiveMessageTimeout);
226void InterprocessConnection::initialise()
228 safeAction->setSafe (
true);
229 threadIsRunning =
true;
231 thread->startThread();
236 jassert (socket ==
nullptr && pipe ==
nullptr);
243 jassert (socket ==
nullptr && pipe ==
nullptr);
252 : safeAction (
ipc), connectionMade (connected)
255 void messageCallback()
override
272void InterprocessConnection::connectionMadeInt()
274 if (! callbackConnectionState)
276 callbackConnectionState =
true;
278 if (useMessageThread)
285void InterprocessConnection::connectionLostInt()
287 if (callbackConnectionState)
289 callbackConnectionState =
false;
291 if (useMessageThread)
292 (
new ConnectionStateMessage (safeAction,
false))->post();
301 : safeAction (
ipc), data (d)
304 void messageCallback()
override
316void InterprocessConnection::deliverDataInt (
const MemoryBlock& data)
318 jassert (callbackConnectionState);
320 if (useMessageThread)
327int InterprocessConnection::readData (
void* data,
int num)
329 const ScopedReadLock
sl (pipeAndSocketLock);
331 if (socket !=
nullptr)
332 return socket->read (data, num,
true);
335 return pipe->read (data, num, pipeReceiveMessageTimeout);
341bool InterprocessConnection::readNextMessage()
358 if (thread->threadShouldExit())
380 if (socket !=
nullptr)
381 deletePipeAndSocket();
389void InterprocessConnection::runThread()
391 while (! thread->threadShouldExit())
393 if (socket !=
nullptr)
395 auto ready = socket->waitUntilReady (
true, 100);
399 deletePipeAndSocket();
410 else if (pipe !=
nullptr)
412 if (! pipe->isOpen())
414 deletePipeAndSocket();
424 if (thread->threadShouldExit() || ! readNextMessage())
428 threadIsRunning =
false;
static Type swapIfBigEndian(Type value) noexcept
Swaps the byte order of a signed or unsigned integer if the CPU is big-endian.
Automatically locks and unlocks a mutex object.
static IPAddress local(bool IPv6=false) noexcept
Returns an IPv4 or IPv6 address meaning "localhost", equivalent to 127.0.0.1 (IPv4) or ::1 (IPv6)
String toString() const
Returns a dot- or colon-separated string in the form "1.2.3.4" (IPv4) or "1:2:3:4:5:6:7:8" (IPv6).
Manages a simple two-way messaging connection to another process, using either a socket or a named pi...
virtual void connectionMade()=0
Called when the connection is first connected.
virtual void messageReceived(const MemoryBlock &message)=0
Called when a message arrives.
void disconnect(int timeoutMs=-1, Notify notify=Notify::yes)
Disconnects and closes any currently-open sockets or pipes.
String getConnectedHostName() const
Returns the name of the machine at the other end of this connection.
InterprocessConnection(bool callbacksOnMessageThread=true, uint32 magicMessageHeaderNumber=0xf2b49e2c)
Creates a connection.
bool createPipe(const String &pipeName, int pipeReceiveMessageTimeoutMs, bool mustNotExist=false)
Tries to create a new pipe for other processes to connect to.
bool connectToPipe(const String &pipeName, int pipeReceiveMessageTimeoutMs)
Tries to connect the object to an existing named pipe.
bool isConnected() const
True if a socket or pipe is currently active.
virtual ~InterprocessConnection()
Destructor.
Notify
Whether the disconnect call should trigger callbacks.
virtual void connectionLost()=0
Called when the connection is broken.
bool sendMessage(const MemoryBlock &message)
Tries to send a message to the other end of this connection.
bool connectToSocket(const String &hostName, int portNumber, int timeOutMillisecs)
Tries to connect this object to a socket.
A class to hold a resizable block of raw data.
void * getData() noexcept
Returns a void pointer to the data.
size_t getSize() const noexcept
Returns the block's current allocated size, in bytes.
Internal class used as the base class for all message objects.
The base class for objects that can be sent to a MessageListener.
Automatically locks and unlocks a ReadWriteLock object.
Automatically locks and unlocks a ReadWriteLock object.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
Type * addBytesToPointer(Type *basePointer, IntegerType bytes) noexcept
A handy function which adds a number of bytes to any type of pointer and returns the result.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
void run() override
Must be implemented to perform the thread's actual code.