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 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 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. int streamFlags = ChildProcess::wantStdOut | ChildProcess::wantStdErr);
  128. /** This will be called to deliver a message from the slave process.
  129. The call will probably be made on a background thread, so be careful with your thread-safety!
  130. */
  131. virtual void handleMessageFromSlave (const MemoryBlock&) = 0;
  132. /** This will be called when the slave process dies or is somehow disconnected.
  133. The call will probably be made on a background thread, so be careful with your thread-safety!
  134. */
  135. virtual void handleConnectionLost();
  136. /** Attempts to send a message to the slave process.
  137. This returns true if the message was dispatched, but doesn't check that it actually
  138. gets delivered at the other end. If successful, the data will emerge in a call to
  139. your ChildProcessSlave::handleMessageFromMaster().
  140. */
  141. bool sendMessageToSlave (const MemoryBlock&);
  142. private:
  143. ChildProcess childProcess;
  144. struct Connection;
  145. friend struct Connection;
  146. friend struct ContainerDeletePolicy<Connection>;
  147. ScopedPointer<Connection> connection;
  148. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcessMaster)
  149. };
  150. } // namespace juce