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