JUCE  v6.1.6 (6.0.8-1114)
JUCE API
Looking for a senior C++ dev?
I'm looking for work. Hire me!
juce::ChildProcessWorker Class Reference

Acts as the worker end of a coordinator/worker pair of connected processes. More...

#include <juce_ConnectedChildProcess.h>

Collaboration diagram for juce::ChildProcessWorker:

Public Member Functions

 ChildProcessWorker ()
 Creates a non-connected worker process. More...
 
virtual ~ChildProcessWorker ()
 Destructor. More...
 
virtual void handleConnectionLost ()
 This will be called when the connection to the coordinator process is lost. More...
 
virtual void handleConnectionMade ()
 This will be called when the coordinator process finishes connecting to this worker. More...
 
virtual void handleMessageFromCoordinator (const MemoryBlock &mb)
 This will be called to deliver messages from the coordinator process. More...
 
virtual void handleMessageFromMaster (const MemoryBlock &)
 
bool initialiseFromCommandLine (const String &commandLine, const String &commandLineUniqueID, int timeoutMs=0)
 This checks some command-line parameters to see whether they were generated by ChildProcessCoordinator::launchWorkerProcess(), and if so, connects to that coordinator process. More...
 
bool sendMessageToCoordinator (const MemoryBlock &)
 Tries to send a message to the coordinator process. More...
 
bool sendMessageToMaster (const MemoryBlock &mb)
 

Private Attributes

std::unique_ptr< Connection > connection
 

Detailed Description

Acts as the worker end of a coordinator/worker pair of connected processes.

The ChildProcessWorker and ChildProcessCoordinator classes make it easy for an app to spawn a child process, and to manage a 2-way messaging connection to control it.

To use the system, you need to create subclasses of both ChildProcessWorker and ChildProcessCoordinator. To instantiate the ChildProcessWorker object, you must add some code to your main() or JUCEApplication::initialise() function that calls the initialiseFromCommandLine() method to check the app's command-line parameters to see whether it's being launched as a child process. If this returns true then the worker process can be allowed to run, and its handleMessageFromCoordinator() method will be called whenever a message arrives.

The juce demo app has a good example of this class in action.

See also
ChildProcessCoordinator, InterprocessConnection, ChildProcess

@tags{Events}

Constructor & Destructor Documentation

◆ ChildProcessWorker()

juce::ChildProcessWorker::ChildProcessWorker ( )

Creates a non-connected worker process.

Use initialiseFromCommandLine to connect to a coordinator process.

◆ ~ChildProcessWorker()

virtual juce::ChildProcessWorker::~ChildProcessWorker ( )
virtual

Destructor.

Member Function Documentation

◆ handleConnectionLost()

virtual void juce::ChildProcessWorker::handleConnectionLost ( )
virtual

This will be called when the connection to the coordinator process is lost.

The call may be made from any thread (including the message thread). Typically, if your process only exists to act as a worker, you should probably exit when this happens.

◆ handleConnectionMade()

virtual void juce::ChildProcessWorker::handleConnectionMade ( )
virtual

This will be called when the coordinator process finishes connecting to this worker.

The call will probably be made on a background thread, so be careful with your thread-safety!

◆ handleMessageFromCoordinator()

virtual void juce::ChildProcessWorker::handleMessageFromCoordinator ( const MemoryBlock mb)
virtual

This will be called to deliver messages from the coordinator process.

The call will probably be made on a background thread, so be careful with your thread-safety! You may want to respond by sending back a message with sendMessageToCoordinator()

◆ handleMessageFromMaster()

virtual void juce::ChildProcessWorker::handleMessageFromMaster ( const MemoryBlock )
inlinevirtual

◆ initialiseFromCommandLine()

bool juce::ChildProcessWorker::initialiseFromCommandLine ( const String commandLine,
const String commandLineUniqueID,
int  timeoutMs = 0 
)

This checks some command-line parameters to see whether they were generated by ChildProcessCoordinator::launchWorkerProcess(), and if so, connects to that coordinator process.

In an exe that can be used as a child process, you should add some code to your main() or JUCEApplication::initialise() that calls this method.

The commandLineUniqueID should be a short alphanumeric identifier (no spaces!) that matches the string passed to ChildProcessCoordinator::launchWorkerProcess().

The timeoutMs parameter lets you specify how long the child process is allowed to run without receiving a ping from the coordinator before the coordinator is considered to have died, and handleConnectionLost() will be called. Passing <= 0 for this timeout makes it use a default value.

Returns true if the command-line matches and the connection is made successfully.

◆ sendMessageToCoordinator()

bool juce::ChildProcessWorker::sendMessageToCoordinator ( const MemoryBlock )

Tries to send a message to the coordinator process.

This returns true if the message was sent, but doesn't check that it actually gets delivered at the other end. If successful, the data will emerge in a call to your ChildProcessCoordinator::handleMessageFromWorker().

◆ sendMessageToMaster()

bool juce::ChildProcessWorker::sendMessageToMaster ( const MemoryBlock mb)
inline

Member Data Documentation

◆ connection

std::unique_ptr<Connection> juce::ChildProcessWorker::connection
private

The documentation for this class was generated from the following file: