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

7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. enum { magicMastSlaveConnectionHeader = 0x712baf04 };
  20. static const char* startMessage = "__ipc_st";
  21. static const char* killMessage = "__ipc_k_";
  22. static const char* pingMessage = "__ipc_p_";
  23. enum { specialMessageSize = 8, defaultTimeoutMs = 8000 };
  24. static String getCommandLinePrefix (const String& commandLineUniqueID)
  25. {
  26. return "--" + commandLineUniqueID + ":";
  27. }
  28. //==============================================================================
  29. // This thread sends and receives ping messages every second, so that it
  30. // can find out if the other process has stopped running.
  31. struct ChildProcessPingThread : public Thread,
  32. private AsyncUpdater
  33. {
  34. ChildProcessPingThread (int timeout) : Thread ("IPC ping"), timeoutMs (timeout)
  35. {
  36. pingReceived();
  37. }
  38. static bool isPingMessage (const MemoryBlock& m) noexcept
  39. {
  40. return memcmp (m.getData(), pingMessage, specialMessageSize) == 0;
  41. }
  42. void pingReceived() noexcept { countdown = timeoutMs / 1000 + 1; }
  43. void triggerConnectionLostMessage() { triggerAsyncUpdate(); }
  44. virtual bool sendPingMessage (const MemoryBlock&) = 0;
  45. virtual void pingFailed() = 0;
  46. int timeoutMs;
  47. private:
  48. Atomic<int> countdown;
  49. void handleAsyncUpdate() override { pingFailed(); }
  50. void run() override
  51. {
  52. while (! threadShouldExit())
  53. {
  54. if (--countdown <= 0 || ! sendPingMessage (MemoryBlock (pingMessage, specialMessageSize)))
  55. {
  56. triggerConnectionLostMessage();
  57. break;
  58. }
  59. wait (1000);
  60. }
  61. }
  62. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcessPingThread)
  63. };
  64. //==============================================================================
  65. struct ChildProcessMaster::Connection : public InterprocessConnection,
  66. private ChildProcessPingThread
  67. {
  68. Connection (ChildProcessMaster& m, const String& pipeName, int timeout)
  69. : InterprocessConnection (false, magicMastSlaveConnectionHeader),
  70. ChildProcessPingThread (timeout),
  71. owner (m)
  72. {
  73. if (createPipe (pipeName, timeoutMs))
  74. startThread (4);
  75. }
  76. ~Connection()
  77. {
  78. stopThread (10000);
  79. }
  80. private:
  81. void connectionMade() override {}
  82. void connectionLost() override { owner.handleConnectionLost(); }
  83. bool sendPingMessage (const MemoryBlock& m) override { return owner.sendMessageToSlave (m); }
  84. void pingFailed() override { connectionLost(); }
  85. void messageReceived (const MemoryBlock& m) override
  86. {
  87. pingReceived();
  88. if (m.getSize() != specialMessageSize || ! isPingMessage (m))
  89. owner.handleMessageFromSlave (m);
  90. }
  91. ChildProcessMaster& owner;
  92. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Connection)
  93. };
  94. //==============================================================================
  95. ChildProcessMaster::ChildProcessMaster() {}
  96. ChildProcessMaster::~ChildProcessMaster()
  97. {
  98. if (connection != nullptr)
  99. {
  100. sendMessageToSlave (MemoryBlock (killMessage, specialMessageSize));
  101. connection->disconnect();
  102. connection = nullptr;
  103. }
  104. }
  105. void ChildProcessMaster::handleConnectionLost() {}
  106. bool ChildProcessMaster::sendMessageToSlave (const MemoryBlock& mb)
  107. {
  108. if (connection != nullptr)
  109. return connection->sendMessage (mb);
  110. jassertfalse; // this can only be used when the connection is active!
  111. return false;
  112. }
  113. bool ChildProcessMaster::launchSlaveProcess (const File& executable, const String& commandLineUniqueID, int timeoutMs, int streamFlags)
  114. {
  115. connection = nullptr;
  116. jassert (childProcess.kill());
  117. const String pipeName ("p" + String::toHexString (Random().nextInt64()));
  118. StringArray args;
  119. args.add (executable.getFullPathName());
  120. args.add (getCommandLinePrefix (commandLineUniqueID) + pipeName);
  121. if (childProcess.start (args, streamFlags))
  122. {
  123. connection = new Connection (*this, pipeName, timeoutMs <= 0 ? defaultTimeoutMs : timeoutMs);
  124. if (connection->isConnected())
  125. {
  126. sendMessageToSlave (MemoryBlock (startMessage, specialMessageSize));
  127. return true;
  128. }
  129. connection = nullptr;
  130. }
  131. return false;
  132. }
  133. //==============================================================================
  134. struct ChildProcessSlave::Connection : public InterprocessConnection,
  135. private ChildProcessPingThread
  136. {
  137. Connection (ChildProcessSlave& p, const String& pipeName, int timeout)
  138. : InterprocessConnection (false, magicMastSlaveConnectionHeader),
  139. ChildProcessPingThread (timeout),
  140. owner (p)
  141. {
  142. connectToPipe (pipeName, timeoutMs);
  143. startThread (4);
  144. }
  145. ~Connection()
  146. {
  147. stopThread (10000);
  148. }
  149. private:
  150. ChildProcessSlave& owner;
  151. void connectionMade() override {}
  152. void connectionLost() override { owner.handleConnectionLost(); }
  153. bool sendPingMessage (const MemoryBlock& m) override { return owner.sendMessageToMaster (m); }
  154. void pingFailed() override { connectionLost(); }
  155. void messageReceived (const MemoryBlock& m) override
  156. {
  157. pingReceived();
  158. if (m.getSize() == specialMessageSize)
  159. {
  160. if (isPingMessage (m))
  161. return;
  162. if (memcmp (m.getData(), killMessage, specialMessageSize) == 0)
  163. {
  164. triggerConnectionLostMessage();
  165. return;
  166. }
  167. if (memcmp (m.getData(), startMessage, specialMessageSize) == 0)
  168. {
  169. owner.handleConnectionMade();
  170. return;
  171. }
  172. }
  173. owner.handleMessageFromMaster (m);
  174. }
  175. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Connection)
  176. };
  177. //==============================================================================
  178. ChildProcessSlave::ChildProcessSlave() {}
  179. ChildProcessSlave::~ChildProcessSlave() {}
  180. void ChildProcessSlave::handleConnectionMade() {}
  181. void ChildProcessSlave::handleConnectionLost() {}
  182. bool ChildProcessSlave::sendMessageToMaster (const MemoryBlock& mb)
  183. {
  184. if (connection != nullptr)
  185. return connection->sendMessage (mb);
  186. jassertfalse; // this can only be used when the connection is active!
  187. return false;
  188. }
  189. bool ChildProcessSlave::initialiseFromCommandLine (const String& commandLine,
  190. const String& commandLineUniqueID,
  191. int timeoutMs)
  192. {
  193. String prefix (getCommandLinePrefix (commandLineUniqueID));
  194. if (commandLine.trim().startsWith (prefix))
  195. {
  196. String pipeName (commandLine.fromFirstOccurrenceOf (prefix, false, false)
  197. .upToFirstOccurrenceOf (" ", false, false).trim());
  198. if (pipeName.isNotEmpty())
  199. {
  200. connection = new Connection (*this, pipeName, timeoutMs <= 0 ? defaultTimeoutMs : timeoutMs);
  201. if (! connection->isConnected())
  202. connection = nullptr;
  203. }
  204. }
  205. return connection != nullptr;
  206. }
  207. } // namespace juce