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.

209 lines
9.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_INTERPROCESSCONNECTION_H_INCLUDED
  18. #define JUCE_INTERPROCESSCONNECTION_H_INCLUDED
  19. class InterprocessConnectionServer;
  20. class MemoryBlock;
  21. //==============================================================================
  22. /**
  23. Manages a simple two-way messaging connection to another process, using either
  24. a socket or a named pipe as the transport medium.
  25. To connect to a waiting socket or an open pipe, use the connectToSocket() or
  26. connectToPipe() methods. If this succeeds, messages can be sent to the other end,
  27. and incoming messages will result in a callback via the messageReceived()
  28. method.
  29. To open a pipe and wait for another client to connect to it, use the createPipe()
  30. method.
  31. To act as a socket server and create connections for one or more client, see the
  32. InterprocessConnectionServer class.
  33. @see InterprocessConnectionServer, Socket, NamedPipe
  34. */
  35. class JUCE_API InterprocessConnection
  36. {
  37. public:
  38. //==============================================================================
  39. /** Creates a connection.
  40. Connections are created manually, connecting them with the connectToSocket()
  41. or connectToPipe() methods, or they are created automatically by a InterprocessConnectionServer
  42. when a client wants to connect.
  43. @param callbacksOnMessageThread if true, callbacks to the connectionMade(),
  44. connectionLost() and messageReceived() methods will
  45. always be made using the message thread; if false,
  46. these will be called immediately on the connection's
  47. own thread.
  48. @param magicMessageHeaderNumber a magic number to use in the header to check the
  49. validity of the data blocks being sent and received. This
  50. can be any number, but the sender and receiver must obviously
  51. use matching values or they won't recognise each other.
  52. */
  53. InterprocessConnection (bool callbacksOnMessageThread = true,
  54. uint32 magicMessageHeaderNumber = 0xf2b49e2c);
  55. /** Destructor. */
  56. virtual ~InterprocessConnection();
  57. //==============================================================================
  58. /** Tries to connect this object to a socket.
  59. For this to work, the machine on the other end needs to have a InterprocessConnectionServer
  60. object waiting to receive client connections on this port number.
  61. @param hostName the host computer, either a network address or name
  62. @param portNumber the socket port number to try to connect to
  63. @param timeOutMillisecs how long to keep trying before giving up
  64. @returns true if the connection is established successfully
  65. @see Socket
  66. */
  67. bool connectToSocket (const String& hostName,
  68. int portNumber,
  69. int timeOutMillisecs);
  70. /** Tries to connect the object to an existing named pipe.
  71. For this to work, another process on the same computer must already have opened
  72. an InterprocessConnection object and used createPipe() to create a pipe for this
  73. to connect to.
  74. @param pipeName the name to use for the pipe - this should be unique to your app
  75. @param pipeReceiveMessageTimeoutMs a timeout length to be used when reading or writing
  76. to the pipe, or -1 for an infinite timeout.
  77. @returns true if it connects successfully.
  78. @see createPipe, NamedPipe
  79. */
  80. bool connectToPipe (const String& pipeName, int pipeReceiveMessageTimeoutMs);
  81. /** Tries to create a new pipe for other processes to connect to.
  82. This creates a pipe with the given name, so that other processes can use
  83. connectToPipe() to connect to the other end.
  84. @param pipeName the name to use for the pipe - this should be unique to your app
  85. @param pipeReceiveMessageTimeoutMs a timeout length to be used when reading or writing
  86. to the pipe, or -1 for an infinite timeout.
  87. @returns true if the pipe was created, or false if it fails (e.g. if another process is
  88. already using using the pipe).
  89. */
  90. bool createPipe (const String& pipeName, int pipeReceiveMessageTimeoutMs);
  91. /** Disconnects and closes any currently-open sockets or pipes. */
  92. void disconnect();
  93. /** True if a socket or pipe is currently active. */
  94. bool isConnected() const;
  95. /** Returns the socket that this connection is using (or nullptr if it uses a pipe). */
  96. StreamingSocket* getSocket() const noexcept { return socket; }
  97. /** Returns the pipe that this connection is using (or nullptr if it uses a socket). */
  98. NamedPipe* getPipe() const noexcept { return pipe; }
  99. /** Returns the name of the machine at the other end of this connection.
  100. This may return an empty string if the name is unknown.
  101. */
  102. String getConnectedHostName() const;
  103. //==============================================================================
  104. /** Tries to send a message to the other end of this connection.
  105. This will fail if it's not connected, or if there's some kind of write error. If
  106. it succeeds, the connection object at the other end will receive the message by
  107. a callback to its messageReceived() method.
  108. @see messageReceived
  109. */
  110. bool sendMessage (const MemoryBlock& message);
  111. //==============================================================================
  112. /** Called when the connection is first connected.
  113. If the connection was created with the callbacksOnMessageThread flag set, then
  114. this will be called on the message thread; otherwise it will be called on a server
  115. thread.
  116. */
  117. virtual void connectionMade() = 0;
  118. /** Called when the connection is broken.
  119. If the connection was created with the callbacksOnMessageThread flag set, then
  120. this will be called on the message thread; otherwise it will be called on a server
  121. thread.
  122. */
  123. virtual void connectionLost() = 0;
  124. /** Called when a message arrives.
  125. When the object at the other end of this connection sends us a message with sendMessage(),
  126. this callback is used to deliver it to us.
  127. If the connection was created with the callbacksOnMessageThread flag set, then
  128. this will be called on the message thread; otherwise it will be called on a server
  129. thread.
  130. @see sendMessage
  131. */
  132. virtual void messageReceived (const MemoryBlock& message) = 0;
  133. private:
  134. //==============================================================================
  135. WeakReference<InterprocessConnection>::Master masterReference;
  136. friend class WeakReference<InterprocessConnection>;
  137. CriticalSection pipeAndSocketLock;
  138. ScopedPointer <StreamingSocket> socket;
  139. ScopedPointer <NamedPipe> pipe;
  140. bool callbackConnectionState;
  141. const bool useMessageThread;
  142. const uint32 magicMessageHeader;
  143. int pipeReceiveMessageTimeout;
  144. friend class InterprocessConnectionServer;
  145. void initialiseWithSocket (StreamingSocket*);
  146. void initialiseWithPipe (NamedPipe*);
  147. void deletePipeAndSocket();
  148. void connectionMadeInt();
  149. void connectionLostInt();
  150. void deliverDataInt (const MemoryBlock&);
  151. bool readNextMessageInt();
  152. struct ConnectionThread;
  153. friend struct ConnectionThread;
  154. friend struct ContainerDeletePolicy<ConnectionThread>;
  155. ScopedPointer<ConnectionThread> thread;
  156. void runThread();
  157. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InterprocessConnection)
  158. };
  159. #endif // JUCE_INTERPROCESSCONNECTION_H_INCLUDED