OpenShot Audio Library | OpenShotAudio  0.3.1
juce_InterprocessConnection.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 class InterprocessConnectionServer;
27 class MemoryBlock;
28 
29 
30 //==============================================================================
50 class JUCE_API InterprocessConnection
51 {
52 public:
53  //==============================================================================
70  InterprocessConnection (bool callbacksOnMessageThread = true,
71  uint32 magicMessageHeaderNumber = 0xf2b49e2c);
72 
74  virtual ~InterprocessConnection();
75 
76  //==============================================================================
88  bool connectToSocket (const String& hostName,
89  int portNumber,
90  int timeOutMillisecs);
91 
104  bool connectToPipe (const String& pipeName, int pipeReceiveMessageTimeoutMs);
105 
118  bool createPipe (const String& pipeName, int pipeReceiveMessageTimeoutMs, bool mustNotExist = false);
119 
121  void disconnect();
122 
124  bool isConnected() const;
125 
127  StreamingSocket* getSocket() const noexcept { return socket.get(); }
128 
130  NamedPipe* getPipe() const noexcept { return pipe.get(); }
131 
135  String getConnectedHostName() const;
136 
137  //==============================================================================
146  bool sendMessage (const MemoryBlock& message);
147 
148  //==============================================================================
155  virtual void connectionMade() = 0;
156 
163  virtual void connectionLost() = 0;
164 
176  virtual void messageReceived (const MemoryBlock& message) = 0;
177 
178 
179 private:
180  //==============================================================================
181  CriticalSection pipeAndSocketLock;
182  std::unique_ptr<StreamingSocket> socket;
183  std::unique_ptr<NamedPipe> pipe;
184  bool callbackConnectionState = false;
185  const bool useMessageThread;
186  const uint32 magicMessageHeader;
187  int pipeReceiveMessageTimeout = -1;
188 
189  friend class InterprocessConnectionServer;
190  void initialiseWithSocket (StreamingSocket*);
191  void initialiseWithPipe (NamedPipe*);
192  void deletePipeAndSocket();
193  void connectionMadeInt();
194  void connectionLostInt();
195  void deliverDataInt (const MemoryBlock&);
196  bool readNextMessage();
197  int readData (void*, int);
198 
199  struct ConnectionThread;
200  std::unique_ptr<ConnectionThread> thread;
201  std::atomic<bool> threadIsRunning { false };
202 
203  void runThread();
204  int writeData (void*, int);
205 
206  JUCE_DECLARE_WEAK_REFERENCEABLE (InterprocessConnection)
207  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InterprocessConnection)
208 };
209 
210 } // namespace juce
virtual void connectionMade()=0
virtual void messageReceived(const MemoryBlock &message)=0
StreamingSocket * getSocket() const noexcept
virtual void connectionLost()=0
NamedPipe * getPipe() const noexcept