OpenShot Audio Library | OpenShotAudio  0.3.1
juce_InterprocessConnectionServer.cpp
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 
27 {
28 }
29 
31 {
32  stop();
33 }
34 
35 //==============================================================================
36 bool InterprocessConnectionServer::beginWaitingForSocket (const int portNumber, const String& bindAddress)
37 {
38  stop();
39 
40  socket.reset (new StreamingSocket());
41 
42  if (socket->createListener (portNumber, bindAddress))
43  {
44  startThread();
45  return true;
46  }
47 
48  socket.reset();
49  return false;
50 }
51 
53 {
55 
56  if (socket != nullptr)
57  socket->close();
58 
59  stopThread (4000);
60  socket.reset();
61 }
62 
64 {
65  return (socket == nullptr) ? -1 : socket->getBoundPort();
66 }
67 
68 void InterprocessConnectionServer::run()
69 {
70  while ((! threadShouldExit()) && socket != nullptr)
71  {
72  std::unique_ptr<StreamingSocket> clientSocket (socket->waitForNextConnection());
73 
74  if (clientSocket != nullptr)
75  if (auto* newConnection = createConnectionObject())
76  newConnection->initialiseWithSocket (clientSocket.release());
77  }
78 }
79 
80 } // namespace juce
virtual InterprocessConnection * createConnectionObject()=0
bool beginWaitingForSocket(int portNumber, const String &bindAddress=String())
void startThread()
bool threadShouldExit() const
bool stopThread(int timeOutMilliseconds)
void signalThreadShouldExit()