26enum { magicCoordWorkerConnectionHeader = 0x712baf04 };
28static const char* startMessage =
"__ipc_st";
29static const char* killMessage =
"__ipc_k_";
30static const char* pingMessage =
"__ipc_p_";
31enum { specialMessageSize = 8, defaultTimeoutMs = 8000 };
33static bool isMessageType (
const MemoryBlock&
mb,
const char*
messageType)
noexcept
56 void pingReceived()
noexcept { countdown = timeoutMs / 1000 + 1; }
59 virtual bool sendPingMessage (
const MemoryBlock&) = 0;
60 virtual void pingFailed() = 0;
69 void handleAsyncUpdate()
override { pingFailed(); }
75 if (--countdown <= 0 || ! sendPingMessage ({ pingMessage, specialMessageSize }))
77 triggerConnectionLostMessage();
106 using ChildProcessPingThread::startPinging;
109 void connectionMade()
override {}
113 void pingFailed()
override { connectionLost(); }
115 void messageReceived (
const MemoryBlock& m)
override
119 if (m.
getSize() != specialMessageSize || ! isMessageType (m, pingMessage))
140 JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE (
"-Wdeprecated-declarations")
141 JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
142 handleMessageFromSlave (
mb);
143 JUCE_END_IGNORE_WARNINGS_GCC_LIKE
144 JUCE_END_IGNORE_WARNINGS_MSVC
149 if (connection !=
nullptr)
150 return connection->sendMessage (
mb);
170 return ChildProcessManager::getInstance()->createAndStartManagedChildProcess (args,
streamFlags);
172 auto p = std::make_shared<ChildProcess>();
180 if (childProcess !=
nullptr)
182 connection.reset (
new Connection (*
this,
pipeName, timeoutMs <= 0 ? defaultTimeoutMs : timeoutMs));
184 if (connection->isConnected())
186 connection->startPinging();
199 if (connection !=
nullptr)
202 connection->disconnect();
206 childProcess.reset();
228 using ChildProcessPingThread::startPinging;
233 void connectionMade()
override {}
237 void pingFailed()
override { connectionLost(); }
239 void messageReceived (
const MemoryBlock& m)
override
243 if (isMessageType (m, pingMessage))
246 if (isMessageType (m, killMessage))
247 return triggerConnectionLostMessage();
249 if (isMessageType (m, startMessage))
267 JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE (
"-Wdeprecated-declarations")
268 JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
269 handleMessageFromMaster (
mb);
270 JUCE_END_IGNORE_WARNINGS_GCC_LIKE
271 JUCE_END_IGNORE_WARNINGS_MSVC
276 if (connection !=
nullptr)
277 return connection->sendMessage (
mb);
292 .upToFirstOccurrenceOf (
" ",
false,
false).trim();
296 connection.reset (
new Connection (*
this,
pipeName, timeoutMs <= 0 ? defaultTimeoutMs : timeoutMs));
298 if (connection->isConnected())
299 connection->startPinging();
305 return connection !=
nullptr;
Has a callback method that is triggered asynchronously.
void triggerAsyncUpdate()
Causes the callback to be triggered at a later time.
void cancelPendingUpdate() noexcept
This will stop any pending updates from happening.
Acts as the coordinator in a coordinator/worker pair of connected processes.
void killWorkerProcess()
Sends a kill message to the worker, and disconnects from it.
ChildProcessCoordinator()
Creates an uninitialised coordinator process object.
virtual ~ChildProcessCoordinator()
Destructor.
bool sendMessageToWorker(const MemoryBlock &)
Attempts to send a message to the worker process.
virtual void handleMessageFromWorker(const MemoryBlock &)
This will be called to deliver a message from the worker process.
virtual void handleConnectionLost()
This will be called when the worker process dies or is somehow disconnected.
bool launchWorkerProcess(const File &executableToLaunch, const String &commandLineUniqueID, int timeoutMs=0, int streamFlags=ChildProcess::wantStdOut|ChildProcess::wantStdErr)
Attempts to launch and connect to a worker process.
Acts as the worker end of a coordinator/worker pair of connected processes.
bool sendMessageToCoordinator(const MemoryBlock &)
Tries to send a message to the coordinator process.
ChildProcessWorker()
Creates a non-connected worker process.
virtual ~ChildProcessWorker()
Destructor.
virtual void handleMessageFromCoordinator(const MemoryBlock &mb)
This will be called to deliver messages from the coordinator process.
bool initialiseFromCommandLine(const String &commandLine, const String &commandLineUniqueID, int timeoutMs=0)
This checks some command-line parameters to see whether they were generated by ChildProcessCoordinato...
virtual void handleConnectionLost()
This will be called when the connection to the coordinator process is lost.
virtual void handleConnectionMade()
This will be called when the coordinator process finishes connecting to this worker.
Represents a local file or directory.
Manages a simple two-way messaging connection to another process, using either a socket or a named pi...
void disconnect(int timeoutMs=-1, Notify notify=Notify::yes)
Disconnects and closes any currently-open sockets or pipes.
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.
A class to hold a resizable block of raw data.
size_t getSize() const noexcept
Returns the block's current allocated size, in bytes.
A random number generator.
A special array for holding a list of strings.
void add(String stringToAdd)
Appends a string at the end of the array.
static String toHexString(IntegerType number)
Returns a string representing this numeric value in hexadecimal.
static OperatingSystemType getOperatingSystemType()
Returns the type of operating system we're running on.
bool wait(double timeOutMilliseconds) const
Suspends the execution of this thread until either the specified timeout period has elapsed,...
bool startThread()
Attempts to start a new thread with default ('Priority::normal') priority.
bool threadShouldExit() const
Checks whether the thread has been told to stop running.
bool stopThread(int timeOutMilliseconds)
Attempts to stop the thread running.
@ low
Uses efficiency cores when possible.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
A simple wrapper around std::atomic.
void cancelPendingUpdate() noexcept
This will stop any pending updates from happening.