The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

205 lines
8.7KB

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