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.

217 lines
9.6KB

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