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.

192 lines
8.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_CONNECTEDCHILDPROCESS_H_INCLUDED
  18. #define JUCE_CONNECTEDCHILDPROCESS_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Acts as the slave end of a master/slave pair of connected processes.
  22. The ChildProcessSlave and ChildProcessMaster 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 ChildProcessSlave and
  25. ChildProcessMaster. To instantiate the ChildProcessSlave 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 slave process can be allowed to run, and its handleMessageFromMaster()
  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 ChildProcessMaster, InterprocessConnection, ChildProcess
  33. */
  34. class JUCE_API ChildProcessSlave
  35. {
  36. public:
  37. /** Creates a non-connected slave process.
  38. Use initialiseFromCommandLine to connect to a master process.
  39. */
  40. ChildProcessSlave();
  41. /** Destructor. */
  42. virtual ~ChildProcessSlave();
  43. /** This checks some command-line parameters to see whether they were generated by
  44. ChildProcessMaster::launchSlaveProcess(), and if so, connects to that master process.
  45. In an exe that can be used as a child process, you should add some code to your
  46. main() or JUCEApplication::initialise() that calls this method.
  47. The commandLineUniqueID should be a short alphanumeric identifier (no spaces!)
  48. that matches the string passed to ChildProcessMaster::launchSlaveProcess().
  49. The timeoutMs parameter lets you specify how long the child process is allowed
  50. to run without receiving a ping from the master before the master is considered to
  51. have died, and handleConnectionLost() will be called. Passing <= 0 for this timeout
  52. makes it use a default value.
  53. Returns true if the command-line matches and the connection is made successfully.
  54. */
  55. bool initialiseFromCommandLine (const String& commandLine,
  56. const String& commandLineUniqueID,
  57. int timeoutMs = 0);
  58. //==============================================================================
  59. /** This will be called to deliver messages from the master process.
  60. The call will probably be made on a background thread, so be careful with your
  61. thread-safety! You may want to respond by sending back a message with
  62. sendMessageToMaster()
  63. */
  64. virtual void handleMessageFromMaster (const MemoryBlock&) = 0;
  65. /** This will be called when the master process finishes connecting to this slave.
  66. The call will probably be made on a background thread, so be careful with your thread-safety!
  67. */
  68. virtual void handleConnectionMade();
  69. /** This will be called when the connection to the master process is lost.
  70. The call may be made from any thread (including the message thread).
  71. Typically, if your process only exists to act as a slave, you should probably exit
  72. when this happens.
  73. */
  74. virtual void handleConnectionLost();
  75. /** Tries to send a message to the master process.
  76. This returns true if the message was sent, but doesn't check that it actually gets
  77. delivered at the other end. If successful, the data will emerge in a call to your
  78. ChildProcessMaster::handleMessageFromSlave().
  79. */
  80. bool sendMessageToMaster (const MemoryBlock&);
  81. private:
  82. struct Connection;
  83. friend struct Connection;
  84. friend struct ContainerDeletePolicy<Connection>;
  85. ScopedPointer<Connection> connection;
  86. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcessSlave)
  87. };
  88. //==============================================================================
  89. /**
  90. Acts as the master in a master/slave pair of connected processes.
  91. The ChildProcessSlave and ChildProcessMaster classes make it easy for an app
  92. to spawn a child process, and to manage a 2-way messaging connection to control it.
  93. To use the system, you need to create subclasses of both ChildProcessSlave and
  94. ChildProcessMaster. When you want your master process to launch the slave, you
  95. just call launchSlaveProcess(), and it'll attempt to launch the executable that
  96. you specify (which may be the same exe), and assuming it has been set-up to
  97. correctly parse the command-line parameters (see ChildProcessSlave) then a
  98. two-way connection will be created.
  99. The juce demo app has a good example of this class in action.
  100. @see ChildProcessSlave, InterprocessConnection, ChildProcess
  101. */
  102. class JUCE_API ChildProcessMaster
  103. {
  104. public:
  105. /** Creates an uninitialised master process object.
  106. Use launchSlaveProcess to launch and connect to a child process.
  107. */
  108. ChildProcessMaster();
  109. /** Destructor. */
  110. virtual ~ChildProcessMaster();
  111. /** Attempts to launch and connect to a slave process.
  112. This will start the given executable, passing it a special command-line
  113. parameter based around the commandLineUniqueID string, which must be a
  114. short alphanumeric string (no spaces!) that identifies your app. The exe
  115. that gets launched must respond by calling ChildProcessSlave::initialiseFromCommandLine()
  116. in its startup code, and must use a matching ID to commandLineUniqueID.
  117. The timeoutMs parameter lets you specify how long the child process is allowed
  118. to go without sending a ping before it is considered to have died and
  119. handleConnectionLost() will be called. Passing <= 0 for this timeout makes
  120. it use a default value.
  121. If this all works, the method returns true, and you can begin sending and
  122. receiving messages with the slave process.
  123. */
  124. bool launchSlaveProcess (const File& executableToLaunch,
  125. const String& commandLineUniqueID,
  126. int timeoutMs = 0);
  127. /** This will be called to deliver a message from the slave process.
  128. The call will probably be made on a background thread, so be careful with your thread-safety!
  129. */
  130. virtual void handleMessageFromSlave (const MemoryBlock&) = 0;
  131. /** This will be called when the slave process dies or is somehow disconnected.
  132. The call will probably be made on a background thread, so be careful with your thread-safety!
  133. */
  134. virtual void handleConnectionLost();
  135. /** Attempts to send a message to the slave process.
  136. This returns true if the message was dispatched, but doesn't check that it actually
  137. gets delivered at the other end. If successful, the data will emerge in a call to
  138. your ChildProcessSlave::handleMessageFromMaster().
  139. */
  140. bool sendMessageToSlave (const MemoryBlock&);
  141. private:
  142. ChildProcess childProcess;
  143. struct Connection;
  144. friend struct Connection;
  145. friend struct ContainerDeletePolicy<Connection>;
  146. ScopedPointer<Connection> connection;
  147. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcessMaster)
  148. };
  149. #endif // JUCE_CONNECTEDCHILDPROCESS_H_INCLUDED