OpenShot Library | libopenshot-audio
0.2.0
|
26 enum { magicMastSlaveConnectionHeader = 0x712baf04 };
28 static const char* startMessage =
"__ipc_st";
29 static const char* killMessage =
"__ipc_k_";
30 static const char* pingMessage =
"__ipc_p_";
31 enum { specialMessageSize = 8, defaultTimeoutMs = 8000 };
33 static inline bool isMessageType (
const MemoryBlock& mb,
const char* messageType) noexcept
35 return mb.matches (messageType, (
size_t) specialMessageSize);
38 static String getCommandLinePrefix (
const String& commandLineUniqueID)
40 return "--" + commandLineUniqueID +
":";
54 void pingReceived() noexcept { countdown = timeoutMs / 1000 + 1; }
57 virtual bool sendPingMessage (
const MemoryBlock&) = 0;
58 virtual void pingFailed() = 0;
65 void handleAsyncUpdate()
override { pingFailed(); }
71 if (--countdown <= 0 || ! sendPingMessage ({ pingMessage, specialMessageSize }))
73 triggerConnectionLostMessage();
103 void connectionMade()
override {}
107 void pingFailed()
override { connectionLost(); }
109 void messageReceived (
const MemoryBlock& m)
override
113 if (m.
getSize() != specialMessageSize || ! isMessageType (m, pingMessage))
119 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (
Connection)
134 if (connection !=
nullptr)
135 return connection->sendMessage (mb);
142 int timeoutMs,
int streamFlags)
150 args.
add (getCommandLinePrefix (commandLineUniqueID) + pipeName);
154 if (childProcess->start (args, streamFlags))
156 connection.reset (
new Connection (*
this, pipeName, timeoutMs <= 0 ? defaultTimeoutMs : timeoutMs));
158 if (connection->isConnected())
172 if (connection !=
nullptr)
175 connection->disconnect();
179 childProcess.reset();
203 void connectionMade()
override {}
207 void pingFailed()
override { connectionLost(); }
209 void messageReceived (
const MemoryBlock& m)
override
213 if (isMessageType (m, pingMessage))
216 if (isMessageType (m, killMessage))
217 return triggerConnectionLostMessage();
219 if (isMessageType (m, startMessage))
225 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (
Connection)
237 if (connection !=
nullptr)
238 return connection->sendMessage (mb);
245 const String& commandLineUniqueID,
248 auto prefix = getCommandLinePrefix (commandLineUniqueID);
255 if (pipeName.isNotEmpty())
257 connection.reset (
new Connection (*
this, pipeName, timeoutMs <= 0 ? defaultTimeoutMs : timeoutMs));
259 if (! connection->isConnected())
264 return connection !=
nullptr;
void startThread()
Starts the thread running.
A special array for holding a list of strings.
Acts as the master in a master/slave pair of connected processes.
bool launchSlaveProcess(const File &executableToLaunch, const String &commandLineUniqueID, int timeoutMs=0, int streamFlags=ChildProcess::wantStdOut|ChildProcess::wantStdErr)
Attempts to launch and connect to a slave process.
virtual void handleConnectionLost()
This will be called when the slave process dies or is somehow disconnected.
void triggerAsyncUpdate()
Causes the callback to be triggered at a later time.
const String & getFullPathName() const noexcept
Returns the complete, absolute path of this file.
ChildProcessSlave()
Creates a non-connected slave process.
bool threadShouldExit() const
Checks whether the thread has been told to stop running.
virtual void handleConnectionLost()
This will be called when the connection to the master process is lost.
static String toHexString(IntegerType number)
Returns a string representing this numeric value in hexadecimal.
Acts as the slave end of a master/slave pair of connected processes.
InterprocessConnection(bool callbacksOnMessageThread=true, uint32 magicMessageHeaderNumber=0xf2b49e2c)
Creates a connection.
Manages a simple two-way messaging connection to another process, using either a socket or a named pi...
A random number generator.
bool startsWith(StringRef text) const noexcept
Tests whether the string begins with another string.
virtual void handleMessageFromSlave(const MemoryBlock &)=0
This will be called to deliver a message from the slave process.
size_t getSize() const noexcept
Returns the block's current allocated size, in bytes.
Represents a local file or directory.
virtual void handleMessageFromMaster(const MemoryBlock &)=0
This will be called to deliver messages from the master process.
bool connectToPipe(const String &pipeName, int pipeReceiveMessageTimeoutMs)
Tries to connect the object to an existing named pipe.
String upToFirstOccurrenceOf(StringRef substringToEndWith, bool includeSubStringInResult, bool ignoreCase) const
Returns the start of this string, up to the first occurrence of a substring.
void killSlaveProcess()
Sends a kill message to the slave, and disconnects from it.
bool sendMessageToSlave(const MemoryBlock &)
Attempts to send a message to the slave process.
bool sendMessageToMaster(const MemoryBlock &)
Tries to send a message to the master process.
bool wait(int timeOutMilliseconds) const
Suspends the execution of this thread until either the specified timeout period has elapsed,...
bool stopThread(int timeOutMilliseconds)
Attempts to stop the thread running.
Has a callback method that is triggered asynchronously.
bool initialiseFromCommandLine(const String &commandLine, const String &commandLineUniqueID, int timeoutMs=0)
This checks some command-line parameters to see whether they were generated by ChildProcessMaster::la...
virtual ~ChildProcessMaster()
Destructor.
virtual ~ChildProcessSlave()
Destructor.
bool createPipe(const String &pipeName, int pipeReceiveMessageTimeoutMs, bool mustNotExist=false)
Tries to create a new pipe for other processes to connect to.
Thread(const String &threadName, size_t threadStackSize=0)
Creates a thread.
void add(String stringToAdd)
Appends a string at the end of the array.
String fromFirstOccurrenceOf(StringRef substringToStartFrom, bool includeSubStringInResult, bool ignoreCase) const
Returns a section of the string starting from a given substring.
virtual void handleConnectionMade()
This will be called when the master process finishes connecting to this slave.
String trim() const
Returns a copy of this string with any whitespace characters removed from the start and end.
Launches and monitors a child process.
ChildProcessMaster()
Creates an uninitialised master process object.
A class to hold a resizable block of raw data.