juce_ConnectedChildProcess.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. @tags{Events}
  34. */
  35. class JUCE_API ChildProcessSlave
  36. {
  37. public:
  38. /** Creates a non-connected slave process.
  39. Use initialiseFromCommandLine to connect to a master process.
  40. */
  41. ChildProcessSlave();
  42. /** Destructor. */
  43. virtual ~ChildProcessSlave();
  44. /** This checks some command-line parameters to see whether they were generated by
  45. ChildProcessMaster::launchSlaveProcess(), and if so, connects to that master 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 ChildProcessMaster::launchSlaveProcess().
  50. The timeoutMs parameter lets you specify how long the child process is allowed
  51. to run without receiving a ping from the master before the master 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 master 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. sendMessageToMaster()
  64. */
  65. virtual void handleMessageFromMaster (const MemoryBlock&) = 0;
  66. /** This will be called when the master process finishes connecting to this slave.
  67. The call will probably be made on a background thread, so be careful with your thread-safety!
  68. */
  69. virtual void handleConnectionMade();
  70. /** This will be called when the connection to the master process is lost.
  71. The call may be made from any thread (including the message thread).
  72. Typically, if your process only exists to act as a slave, you should probably exit
  73. when this happens.
  74. */
  75. virtual void handleConnectionLost();
  76. /** Tries to send a message to the master process.
  77. This returns true if the message was sent, but doesn't check that it actually gets
  78. delivered at the other end. If successful, the data will emerge in a call to your
  79. ChildProcessMaster::handleMessageFromSlave().
  80. */
  81. bool sendMessageToMaster (const MemoryBlock&);
  82. private:
  83. struct Connection;
  84. std::unique_ptr<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. @tags{Events}
  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. Note that the destructor calls killSlaveProcess(), but doesn't wait for
  111. the child process to finish terminating.
  112. */
  113. virtual ~ChildProcessMaster();
  114. /** Attempts to launch and connect to a slave process.
  115. This will start the given executable, passing it a special command-line
  116. parameter based around the commandLineUniqueID string, which must be a
  117. short alphanumeric string (no spaces!) that identifies your app. The exe
  118. that gets launched must respond by calling ChildProcessSlave::initialiseFromCommandLine()
  119. in its startup code, and must use a matching ID to commandLineUniqueID.
  120. The timeoutMs parameter lets you specify how long the child process is allowed
  121. to go without sending a ping before it is considered to have died and
  122. handleConnectionLost() will be called. Passing <= 0 for this timeout makes
  123. it use a default value.
  124. If this all works, the method returns true, and you can begin sending and
  125. receiving messages with the slave process.
  126. If a child process is already running, this will call killSlaveProcess() and
  127. start a new one.
  128. */
  129. bool launchSlaveProcess (const File& executableToLaunch,
  130. const String& commandLineUniqueID,
  131. int timeoutMs = 0,
  132. int streamFlags = ChildProcess::wantStdOut | ChildProcess::wantStdErr);
  133. /** Sends a kill message to the slave, and disconnects from it.
  134. Note that this won't wait for it to terminate.
  135. */
  136. void killSlaveProcess();
  137. /** This will be called to deliver a message from the slave process.
  138. The call will probably be made on a background thread, so be careful with your thread-safety!
  139. */
  140. virtual void handleMessageFromSlave (const MemoryBlock&) = 0;
  141. /** This will be called when the slave process dies or is somehow disconnected.
  142. The call will probably be made on a background thread, so be careful with your thread-safety!
  143. */
  144. virtual void handleConnectionLost();
  145. /** Attempts to send a message to the slave process.
  146. This returns true if the message was dispatched, but doesn't check that it actually
  147. gets delivered at the other end. If successful, the data will emerge in a call to
  148. your ChildProcessSlave::handleMessageFromMaster().
  149. */
  150. bool sendMessageToSlave (const MemoryBlock&);
  151. private:
  152. std::unique_ptr<ChildProcess> childProcess;
  153. struct Connection;
  154. std::unique_ptr<Connection> connection;
  155. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcessMaster)
  156. };
  157. } // namespace juce