OpenShot Library | OpenShotAudio  0.2.2
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
int getBoundPort() const noexcept
Returns the local port number to which this server is currently bound.
virtual InterprocessConnection * createConnectionObject()=0
Creates a suitable connection object for a client process that wants to connect to this one.
bool beginWaitingForSocket(int portNumber, const String &bindAddress=String())
Starts an internal thread which listens on the given port number.
InterprocessConnectionServer()
Creates an uninitialised server object.
void stop()
Terminates the listener thread, if it's active.
A wrapper for a streaming (TCP) socket.
Definition: juce_Socket.h:42
The JUCE String class!
Definition: juce_String.h:43
Encapsulates a thread.
Definition: juce_Thread.h:47
void startThread()
Starts the thread running.
bool threadShouldExit() const
Checks whether the thread has been told to stop running.
bool stopThread(int timeOutMilliseconds)
Attempts to stop the thread running.
void signalThreadShouldExit()
Sets a flag to tell the thread it should stop.