NanoOcp
Minimal AES70 / OCP.1 TCP client/server library for d&b Soundscape devices
Loading...
Searching...
No Matches
Ocp1ConnectionServer.cpp
Go to the documentation of this file.
1/* Copyright (c) 2023, Christian Ahrens
2 *
3 * This file is part of NanoOcp <https://github.com/ChristianAhrens/NanoOcp>
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License version 3.0 as published
7 * by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
20#include "Ocp1Connection.h"
21
22
23namespace NanoOcp1
24{
25
26
27Ocp1ConnectionServer::Ocp1ConnectionServer(const juce::Thread::Priority threadPriority) : juce::Thread("NanoOcp1 connection server"), m_threadPriority(threadPriority)
28{
29}
30
35
36//==============================================================================
37bool Ocp1ConnectionServer::beginWaitingForSocket(const int portNumber, const juce::String& bindAddress)
38{
39 stop();
40
41 socket.reset(new juce::StreamingSocket());
42
43 if (socket->createListener(portNumber, bindAddress))
44 {
45 startThread(m_threadPriority);
46 return true;
47 }
48
49 socket.reset();
50 return false;
51}
52
54{
55 signalThreadShouldExit();
56
57 if (socket != nullptr)
58 socket->close();
59
60 stopThread(4000);
61 socket.reset();
62}
63
65{
66 return (socket == nullptr) ? -1 : socket->getBoundPort();
67}
68
69void Ocp1ConnectionServer::run()
70{
71 while ((!threadShouldExit()) && socket != nullptr)
72 {
73 std::unique_ptr<juce::StreamingSocket> clientSocket(socket->waitForNextConnection());
74
75 if (clientSocket != nullptr)
76 if (auto* newConnection = createConnectionObject())
77 newConnection->initialiseWithSocket(std::move(clientSocket));
78 }
79}
80
81}
Ocp1ConnectionServer(const juce::Thread::Priority threadPriority=juce::Thread::Priority::normal)
Constructs the server.
int getBoundPort() const noexcept
Returns the port number the server is bound to, or -1 if not listening.
void stop()
Stops the accept-loop thread and closes the listening socket.
bool beginWaitingForSocket(int portNumber, const juce::String &bindAddress=juce::String())
Binds a TCP socket to the given port and starts the accept-loop thread.
virtual Ocp1Connection * createConnectionObject()=0
Called by the accept loop each time a new TCP client connects.
Minimal AES70 / OCP.1 TCP client/server library built on JUCE.
Definition NanoOcp1.cpp:23