The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

187 lines
8.3KB

  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. #pragma once
  18. //==============================================================================
  19. /**
  20. Acts as the slave end of a master/slave pair of connected processes.
  21. The ChildProcessSlave and ChildProcessMaster classes make it easy for an app
  22. to spawn a child process, and to manage a 2-way messaging connection to control it.
  23. To use the system, you need to create subclasses of both ChildProcessSlave and
  24. ChildProcessMaster. To instantiate the ChildProcessSlave object, you must
  25. add some code to your main() or JUCEApplication::initialise() function that
  26. calls the initialiseFromCommandLine() method to check the app's command-line
  27. parameters to see whether it's being launched as a child process. If this returns
  28. true then the slave process can be allowed to run, and its handleMessageFromMaster()
  29. method will be called whenever a message arrives.
  30. The juce demo app has a good example of this class in action.
  31. @see ChildProcessMaster, InterprocessConnection, ChildProcess
  32. */
  33. class JUCE_API ChildProcessSlave
  34. {
  35. public:
  36. /** Creates a non-connected slave process.
  37. Use initialiseFromCommandLine to connect to a master process.
  38. */
  39. ChildProcessSlave();
  40. /** Destructor. */
  41. virtual ~ChildProcessSlave();
  42. /** This checks some command-line parameters to see whether they were generated by
  43. ChildProcessMaster::launchSlaveProcess(), and if so, connects to that master process.
  44. In an exe that can be used as a child process, you should add some code to your
  45. main() or JUCEApplication::initialise() that calls this method.
  46. The commandLineUniqueID should be a short alphanumeric identifier (no spaces!)
  47. that matches the string passed to ChildProcessMaster::launchSlaveProcess().
  48. The timeoutMs parameter lets you specify how long the child process is allowed
  49. to run without receiving a ping from the master before the master is considered to
  50. have died, and handleConnectionLost() will be called. Passing <= 0 for this timeout
  51. makes it use a default value.
  52. Returns true if the command-line matches and the connection is made successfully.
  53. */
  54. bool initialiseFromCommandLine (const String& commandLine,
  55. const String& commandLineUniqueID,
  56. int timeoutMs = 0);
  57. //==============================================================================
  58. /** This will be called to deliver messages from the master process.
  59. The call will probably be made on a background thread, so be careful with your
  60. thread-safety! You may want to respond by sending back a message with
  61. sendMessageToMaster()
  62. */
  63. virtual void handleMessageFromMaster (const MemoryBlock&) = 0;
  64. /** This will be called when the master process finishes connecting to this slave.
  65. The call will probably be made on a background thread, so be careful with your thread-safety!
  66. */
  67. virtual void handleConnectionMade();
  68. /** This will be called when the connection to the master process is lost.
  69. The call may be made from any thread (including the message thread).
  70. Typically, if your process only exists to act as a slave, you should probably exit
  71. when this happens.
  72. */
  73. virtual void handleConnectionLost();
  74. /** Tries to send a message to the master process.
  75. This returns true if the message was sent, but doesn't check that it actually gets
  76. delivered at the other end. If successful, the data will emerge in a call to your
  77. ChildProcessMaster::handleMessageFromSlave().
  78. */
  79. bool sendMessageToMaster (const MemoryBlock&);
  80. private:
  81. struct Connection;
  82. friend struct Connection;
  83. friend struct ContainerDeletePolicy<Connection>;
  84. ScopedPointer<Connection> connection;
  85. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcessSlave)
  86. };
  87. //==============================================================================
  88. /**
  89. Acts as the master in a master/slave pair of connected processes.
  90. The ChildProcessSlave and ChildProcessMaster classes make it easy for an app
  91. to spawn a child process, and to manage a 2-way messaging connection to control it.
  92. To use the system, you need to create subclasses of both ChildProcessSlave and
  93. ChildProcessMaster. When you want your master process to launch the slave, you
  94. just call launchSlaveProcess(), and it'll attempt to launch the executable that
  95. you specify (which may be the same exe), and assuming it has been set-up to
  96. correctly parse the command-line parameters (see ChildProcessSlave) then a
  97. two-way connection will be created.
  98. The juce demo app has a good example of this class in action.
  99. @see ChildProcessSlave, InterprocessConnection, ChildProcess
  100. */
  101. class JUCE_API ChildProcessMaster
  102. {
  103. public:
  104. /** Creates an uninitialised master process object.
  105. Use launchSlaveProcess to launch and connect to a child process.
  106. */
  107. ChildProcessMaster();
  108. /** Destructor. */
  109. virtual ~ChildProcessMaster();
  110. /** Attempts to launch and connect to a slave process.
  111. This will start the given executable, passing it a special command-line
  112. parameter based around the commandLineUniqueID string, which must be a
  113. short alphanumeric string (no spaces!) that identifies your app. The exe
  114. that gets launched must respond by calling ChildProcessSlave::initialiseFromCommandLine()
  115. in its startup code, and must use a matching ID to commandLineUniqueID.
  116. The timeoutMs parameter lets you specify how long the child process is allowed
  117. to go without sending a ping before it is considered to have died and
  118. handleConnectionLost() will be called. Passing <= 0 for this timeout makes
  119. it use a default value.
  120. If this all works, the method returns true, and you can begin sending and
  121. receiving messages with the slave process.
  122. */
  123. bool launchSlaveProcess (const File& executableToLaunch,
  124. const String& commandLineUniqueID,
  125. int timeoutMs = 0,
  126. int streamFlags = ChildProcess::wantStdOut | ChildProcess::wantStdErr);
  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. };