Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_ConnectedChildProcess.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Acts as the worker end of a coordinator/worker pair of connected processes.
  22. The ChildProcessWorker and ChildProcessCoordinator classes make it easy for an app
  23. to spawn a child process, and to manage a 2-way messaging connection to control it.
  24. To use the system, you need to create subclasses of both ChildProcessWorker and
  25. ChildProcessCoordinator. To instantiate the ChildProcessWorker object, you must
  26. add some code to your main() or JUCEApplication::initialise() function that
  27. calls the initialiseFromCommandLine() method to check the app's command-line
  28. parameters to see whether it's being launched as a child process. If this returns
  29. true then the worker process can be allowed to run, and its handleMessageFromCoordinator()
  30. method will be called whenever a message arrives.
  31. The juce demo app has a good example of this class in action.
  32. @see ChildProcessCoordinator, InterprocessConnection, ChildProcess
  33. @tags{Events}
  34. */
  35. class JUCE_API ChildProcessWorker
  36. {
  37. public:
  38. /** Creates a non-connected worker process.
  39. Use initialiseFromCommandLine to connect to a coordinator process.
  40. */
  41. ChildProcessWorker();
  42. /** Destructor. */
  43. virtual ~ChildProcessWorker();
  44. /** This checks some command-line parameters to see whether they were generated by
  45. ChildProcessCoordinator::launchWorkerProcess(), and if so, connects to that coordinator process.
  46. In an exe that can be used as a child process, you should add some code to your
  47. main() or JUCEApplication::initialise() that calls this method.
  48. The commandLineUniqueID should be a short alphanumeric identifier (no spaces!)
  49. that matches the string passed to ChildProcessCoordinator::launchWorkerProcess().
  50. The timeoutMs parameter lets you specify how long the child process is allowed
  51. to run without receiving a ping from the coordinator before the coordinator is considered to
  52. have died, and handleConnectionLost() will be called. Passing <= 0 for this timeout
  53. makes it use a default value.
  54. Returns true if the command-line matches and the connection is made successfully.
  55. */
  56. bool initialiseFromCommandLine (const String& commandLine,
  57. const String& commandLineUniqueID,
  58. int timeoutMs = 0);
  59. //==============================================================================
  60. /** This will be called to deliver messages from the coordinator process.
  61. The call will probably be made on a background thread, so be careful with your
  62. thread-safety! You may want to respond by sending back a message with
  63. sendMessageToCoordinator()
  64. */
  65. virtual void handleMessageFromCoordinator (const MemoryBlock& mb);
  66. [[deprecated ("Replaced by handleMessageFromCoordinator.")]]
  67. virtual void handleMessageFromMaster (const MemoryBlock&) {}
  68. /** This will be called when the coordinator process finishes connecting to this worker.
  69. The call will probably be made on a background thread, so be careful with your thread-safety!
  70. */
  71. virtual void handleConnectionMade();
  72. /** This will be called when the connection to the coordinator process is lost.
  73. The call may be made from any thread (including the message thread).
  74. Typically, if your process only exists to act as a worker, you should probably exit
  75. when this happens.
  76. */
  77. virtual void handleConnectionLost();
  78. /** Tries to send a message to the coordinator process.
  79. This returns true if the message was sent, but doesn't check that it actually gets
  80. delivered at the other end. If successful, the data will emerge in a call to your
  81. ChildProcessCoordinator::handleMessageFromWorker().
  82. */
  83. bool sendMessageToCoordinator (const MemoryBlock&);
  84. [[deprecated ("Replaced by sendMessageToCoordinator.")]]
  85. bool sendMessageToMaster (const MemoryBlock& mb) { return sendMessageToCoordinator (mb); }
  86. private:
  87. struct Connection;
  88. std::unique_ptr<Connection> connection;
  89. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcessWorker)
  90. };
  91. using ChildProcessSlave [[deprecated ("Replaced by ChildProcessWorker.")]] = ChildProcessWorker;
  92. //==============================================================================
  93. /**
  94. Acts as the coordinator in a coordinator/worker pair of connected processes.
  95. The ChildProcessWorker and ChildProcessCoordinator classes make it easy for an app
  96. to spawn a child process, and to manage a 2-way messaging connection to control it.
  97. To use the system, you need to create subclasses of both ChildProcessWorker and
  98. ChildProcessCoordinator. When you want your coordinator process to launch the worker, you
  99. just call launchWorkerProcess(), and it'll attempt to launch the executable that
  100. you specify (which may be the same exe), and assuming it has been set-up to
  101. correctly parse the command-line parameters (see ChildProcessWorker) then a
  102. two-way connection will be created.
  103. The juce demo app has a good example of this class in action.
  104. @see ChildProcessWorker, InterprocessConnection, ChildProcess
  105. @tags{Events}
  106. */
  107. class JUCE_API ChildProcessCoordinator
  108. {
  109. public:
  110. /** Creates an uninitialised coordinator process object.
  111. Use launchWorkerProcess to launch and connect to a child process.
  112. */
  113. ChildProcessCoordinator();
  114. /** Destructor.
  115. Note that the destructor calls killWorkerProcess(), but doesn't wait for
  116. the child process to finish terminating.
  117. */
  118. virtual ~ChildProcessCoordinator();
  119. /** Attempts to launch and connect to a worker process.
  120. This will start the given executable, passing it a special command-line
  121. parameter based around the commandLineUniqueID string, which must be a
  122. short alphanumeric string (no spaces!) that identifies your app. The exe
  123. that gets launched must respond by calling ChildProcessWorker::initialiseFromCommandLine()
  124. in its startup code, and must use a matching ID to commandLineUniqueID.
  125. The timeoutMs parameter lets you specify how long the child process is allowed
  126. to go without sending a ping before it is considered to have died and
  127. handleConnectionLost() will be called. Passing <= 0 for this timeout makes
  128. it use a default value.
  129. If this all works, the method returns true, and you can begin sending and
  130. receiving messages with the worker process.
  131. If a child process is already running, this will call killWorkerProcess() and
  132. start a new one.
  133. */
  134. bool launchWorkerProcess (const File& executableToLaunch,
  135. const String& commandLineUniqueID,
  136. int timeoutMs = 0,
  137. int streamFlags = ChildProcess::wantStdOut | ChildProcess::wantStdErr);
  138. [[deprecated ("Replaced by launchWorkerProcess.")]]
  139. bool launchSlaveProcess (const File& executableToLaunch,
  140. const String& commandLineUniqueID,
  141. int timeoutMs = 0,
  142. int streamFlags = ChildProcess::wantStdOut | ChildProcess::wantStdErr)
  143. {
  144. return launchWorkerProcess (executableToLaunch, commandLineUniqueID, timeoutMs, streamFlags);
  145. }
  146. /** Sends a kill message to the worker, and disconnects from it.
  147. Note that this won't wait for it to terminate.
  148. */
  149. void killWorkerProcess();
  150. [[deprecated ("Replaced by killWorkerProcess.")]]
  151. void killSlaveProcess() { killWorkerProcess(); }
  152. /** This will be called to deliver a message from the worker process.
  153. The call will probably be made on a background thread, so be careful with your thread-safety!
  154. */
  155. virtual void handleMessageFromWorker (const MemoryBlock&);
  156. [[deprecated ("Replaced by handleMessageFromWorker")]]
  157. virtual void handleMessageFromSlave (const MemoryBlock&) {}
  158. /** This will be called when the worker process dies or is somehow disconnected.
  159. The call will probably be made on a background thread, so be careful with your thread-safety!
  160. */
  161. virtual void handleConnectionLost();
  162. /** Attempts to send a message to the worker process.
  163. This returns true if the message was dispatched, but doesn't check that it actually
  164. gets delivered at the other end. If successful, the data will emerge in a call to
  165. your ChildProcessWorker::handleMessageFromCoordinator().
  166. */
  167. bool sendMessageToWorker (const MemoryBlock&);
  168. [[deprecated ("Replaced by sendMessageToWorker.")]]
  169. bool sendMessageToSlave (const MemoryBlock& mb) { return sendMessageToWorker (mb); }
  170. private:
  171. std::unique_ptr<ChildProcess> childProcess;
  172. struct Connection;
  173. std::unique_ptr<Connection> connection;
  174. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcessCoordinator)
  175. };
  176. using ChildProcessMaster [[deprecated ("Replaced by ChildProcessCoordinator.")]] = ChildProcessCoordinator;
  177. } // namespace juce