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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 { magicCoordWorkerConnectionHeader = 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 bool isMessageType (const MemoryBlock& mb, const char* messageType) noexcept
  25. {
  26. return mb.matches (messageType, (size_t) specialMessageSize);
  27. }
  28. static String getCommandLinePrefix (const String& commandLineUniqueID)
  29. {
  30. return "--" + commandLineUniqueID + ":";
  31. }
  32. //==============================================================================
  33. // This thread sends and receives ping messages every second, so that it
  34. // can find out if the other process has stopped running.
  35. struct ChildProcessPingThread : public Thread,
  36. private AsyncUpdater
  37. {
  38. ChildProcessPingThread (int timeout) : Thread ("IPC ping"), timeoutMs (timeout)
  39. {
  40. pingReceived();
  41. }
  42. void startPinging() { startThread (4); }
  43. void pingReceived() noexcept { countdown = timeoutMs / 1000 + 1; }
  44. void triggerConnectionLostMessage() { triggerAsyncUpdate(); }
  45. virtual bool sendPingMessage (const MemoryBlock&) = 0;
  46. virtual void pingFailed() = 0;
  47. int timeoutMs;
  48. private:
  49. Atomic<int> countdown;
  50. void handleAsyncUpdate() override { pingFailed(); }
  51. void run() override
  52. {
  53. while (! threadShouldExit())
  54. {
  55. if (--countdown <= 0 || ! sendPingMessage ({ pingMessage, specialMessageSize }))
  56. {
  57. triggerConnectionLostMessage();
  58. break;
  59. }
  60. wait (1000);
  61. }
  62. }
  63. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcessPingThread)
  64. };
  65. //==============================================================================
  66. struct ChildProcessCoordinator::Connection : public InterprocessConnection,
  67. private ChildProcessPingThread
  68. {
  69. Connection (ChildProcessCoordinator& m, const String& pipeName, int timeout)
  70. : InterprocessConnection (false, magicCoordWorkerConnectionHeader),
  71. ChildProcessPingThread (timeout),
  72. owner (m)
  73. {
  74. createPipe (pipeName, timeoutMs);
  75. }
  76. ~Connection() override
  77. {
  78. stopThread (10000);
  79. }
  80. using ChildProcessPingThread::startPinging;
  81. private:
  82. void connectionMade() override {}
  83. void connectionLost() override { owner.handleConnectionLost(); }
  84. bool sendPingMessage (const MemoryBlock& m) override { return owner.sendMessageToWorker (m); }
  85. void pingFailed() override { connectionLost(); }
  86. void messageReceived (const MemoryBlock& m) override
  87. {
  88. pingReceived();
  89. if (m.getSize() != specialMessageSize || ! isMessageType (m, pingMessage))
  90. owner.handleMessageFromWorker (m);
  91. }
  92. ChildProcessCoordinator& owner;
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Connection)
  94. };
  95. //==============================================================================
  96. ChildProcessCoordinator::ChildProcessCoordinator() = default;
  97. ChildProcessCoordinator::~ChildProcessCoordinator()
  98. {
  99. killWorkerProcess();
  100. }
  101. void ChildProcessCoordinator::handleConnectionLost() {}
  102. void ChildProcessCoordinator::handleMessageFromWorker (const MemoryBlock& mb)
  103. {
  104. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
  105. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
  106. handleMessageFromSlave (mb);
  107. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  108. JUCE_END_IGNORE_WARNINGS_MSVC
  109. }
  110. bool ChildProcessCoordinator::sendMessageToWorker (const MemoryBlock& mb)
  111. {
  112. if (connection != nullptr)
  113. return connection->sendMessage (mb);
  114. jassertfalse; // this can only be used when the connection is active!
  115. return false;
  116. }
  117. bool ChildProcessCoordinator::launchWorkerProcess (const File& executable, const String& commandLineUniqueID,
  118. int timeoutMs, int streamFlags)
  119. {
  120. killWorkerProcess();
  121. auto pipeName = "p" + String::toHexString (Random().nextInt64());
  122. StringArray args;
  123. args.add (executable.getFullPathName());
  124. args.add (getCommandLinePrefix (commandLineUniqueID) + pipeName);
  125. childProcess.reset (new ChildProcess());
  126. if (childProcess->start (args, streamFlags))
  127. {
  128. connection.reset (new Connection (*this, pipeName, timeoutMs <= 0 ? defaultTimeoutMs : timeoutMs));
  129. if (connection->isConnected())
  130. {
  131. connection->startPinging();
  132. sendMessageToWorker ({ startMessage, specialMessageSize });
  133. return true;
  134. }
  135. connection.reset();
  136. }
  137. return false;
  138. }
  139. void ChildProcessCoordinator::killWorkerProcess()
  140. {
  141. if (connection != nullptr)
  142. {
  143. sendMessageToWorker ({ killMessage, specialMessageSize });
  144. connection->disconnect();
  145. connection.reset();
  146. }
  147. childProcess.reset();
  148. }
  149. //==============================================================================
  150. struct ChildProcessWorker::Connection : public InterprocessConnection,
  151. private ChildProcessPingThread
  152. {
  153. Connection (ChildProcessWorker& p, const String& pipeName, int timeout)
  154. : InterprocessConnection (false, magicCoordWorkerConnectionHeader),
  155. ChildProcessPingThread (timeout),
  156. owner (p)
  157. {
  158. connectToPipe (pipeName, timeoutMs);
  159. }
  160. ~Connection() override
  161. {
  162. stopThread (10000);
  163. disconnect();
  164. }
  165. using ChildProcessPingThread::startPinging;
  166. private:
  167. ChildProcessWorker& owner;
  168. void connectionMade() override {}
  169. void connectionLost() override { owner.handleConnectionLost(); }
  170. bool sendPingMessage (const MemoryBlock& m) override { return owner.sendMessageToCoordinator (m); }
  171. void pingFailed() override { connectionLost(); }
  172. void messageReceived (const MemoryBlock& m) override
  173. {
  174. pingReceived();
  175. if (isMessageType (m, pingMessage))
  176. return;
  177. if (isMessageType (m, killMessage))
  178. return triggerConnectionLostMessage();
  179. if (isMessageType (m, startMessage))
  180. return owner.handleConnectionMade();
  181. owner.handleMessageFromCoordinator (m);
  182. }
  183. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Connection)
  184. };
  185. //==============================================================================
  186. ChildProcessWorker::ChildProcessWorker() = default;
  187. ChildProcessWorker::~ChildProcessWorker() = default;
  188. void ChildProcessWorker::handleConnectionMade() {}
  189. void ChildProcessWorker::handleConnectionLost() {}
  190. void ChildProcessWorker::handleMessageFromCoordinator (const MemoryBlock& mb)
  191. {
  192. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
  193. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
  194. handleMessageFromMaster (mb);
  195. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  196. JUCE_END_IGNORE_WARNINGS_MSVC
  197. }
  198. bool ChildProcessWorker::sendMessageToCoordinator (const MemoryBlock& mb)
  199. {
  200. if (connection != nullptr)
  201. return connection->sendMessage (mb);
  202. jassertfalse; // this can only be used when the connection is active!
  203. return false;
  204. }
  205. bool ChildProcessWorker::initialiseFromCommandLine (const String& commandLine,
  206. const String& commandLineUniqueID,
  207. int timeoutMs)
  208. {
  209. auto prefix = getCommandLinePrefix (commandLineUniqueID);
  210. if (commandLine.trim().startsWith (prefix))
  211. {
  212. auto pipeName = commandLine.fromFirstOccurrenceOf (prefix, false, false)
  213. .upToFirstOccurrenceOf (" ", false, false).trim();
  214. if (pipeName.isNotEmpty())
  215. {
  216. connection.reset (new Connection (*this, pipeName, timeoutMs <= 0 ? defaultTimeoutMs : timeoutMs));
  217. if (connection->isConnected())
  218. connection->startPinging();
  219. else
  220. connection.reset();
  221. }
  222. }
  223. return connection != nullptr;
  224. }
  225. } // namespace juce