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.

208 lines
8.9KB

  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. 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. @param mustNotExist if set to true, the method will fail if the pipe already exists
  88. @returns true if the pipe was created, or false if it fails (e.g. if another process is
  89. already using using the pipe)
  90. */
  91. bool createPipe (const String& pipeName, int pipeReceiveMessageTimeoutMs, bool mustNotExist = false);
  92. /** Disconnects and closes any currently-open sockets or pipes. */
  93. void disconnect();
  94. /** True if a socket or pipe is currently active. */
  95. bool isConnected() const;
  96. /** Returns the socket that this connection is using (or nullptr if it uses a pipe). */
  97. StreamingSocket* getSocket() const noexcept { return socket; }
  98. /** Returns the pipe that this connection is using (or nullptr if it uses a socket). */
  99. NamedPipe* getPipe() const noexcept { return pipe; }
  100. /** Returns the name of the machine at the other end of this connection.
  101. This may return an empty string if the name is unknown.
  102. */
  103. String getConnectedHostName() const;
  104. //==============================================================================
  105. /** Tries to send a message to the other end of this connection.
  106. This will fail if it's not connected, or if there's some kind of write error. If
  107. it succeeds, the connection object at the other end will receive the message by
  108. a callback to its messageReceived() method.
  109. @see messageReceived
  110. */
  111. bool sendMessage (const MemoryBlock& message);
  112. //==============================================================================
  113. /** Called when the connection is first connected.
  114. If the connection was created with the callbacksOnMessageThread flag set, then
  115. this will be called on the message thread; otherwise it will be called on a server
  116. thread.
  117. */
  118. virtual void connectionMade() = 0;
  119. /** Called when the connection is broken.
  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 connectionLost() = 0;
  125. /** Called when a message arrives.
  126. When the object at the other end of this connection sends us a message with sendMessage(),
  127. this callback is used to deliver it to us.
  128. If the connection was created with the callbacksOnMessageThread flag set, then
  129. this will be called on the message thread; otherwise it will be called on a server
  130. thread.
  131. @see sendMessage
  132. */
  133. virtual void messageReceived (const MemoryBlock& message) = 0;
  134. private:
  135. //==============================================================================
  136. CriticalSection pipeAndSocketLock;
  137. ScopedPointer<StreamingSocket> socket;
  138. ScopedPointer<NamedPipe> pipe;
  139. bool callbackConnectionState = false;
  140. const bool useMessageThread;
  141. const uint32 magicMessageHeader;
  142. int pipeReceiveMessageTimeout = -1;
  143. friend class InterprocessConnectionServer;
  144. void initialiseWithSocket (StreamingSocket*);
  145. void initialiseWithPipe (NamedPipe*);
  146. void deletePipeAndSocket();
  147. void connectionMadeInt();
  148. void connectionLostInt();
  149. void deliverDataInt (const MemoryBlock&);
  150. bool readNextMessageInt();
  151. struct ConnectionThread;
  152. friend struct ConnectionThread;
  153. friend struct ContainerDeletePolicy<ConnectionThread>;
  154. ScopedPointer<ConnectionThread> thread;
  155. void runThread();
  156. int writeData (void*, int);
  157. JUCE_DECLARE_WEAK_REFERENCEABLE (InterprocessConnection)
  158. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InterprocessConnection)
  159. };
  160. } // namespace juce