NanoOcp
Minimal AES70 / OCP.1 TCP client/server library for d&b Soundscape devices
Loading...
Searching...
No Matches
NanoOcp1.cpp
Go to the documentation of this file.
1/* Copyright (c) 2022-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
19#include "NanoOcp1.h"
20
21
22namespace NanoOcp1
23{
24
25
26//==============================================================================
27NanoOcp1Base::NanoOcp1Base(const juce::String& address, const int port)
28{
29 setAddress(address);
30 setPort(port);
31}
32
36
37void NanoOcp1Base::setAddress(const juce::String& address)
38{
39 m_address = address;
40}
41
42const juce::String& NanoOcp1Base::getAddress()
43{
44 return m_address;
45}
46
47void NanoOcp1Base::setPort(const int port)
48{
49 m_port = port;
50}
51
53{
54 return m_port;
55}
56
58{
60 return onDataReceived(data);
61
62 return false;
63}
64
65//==============================================================================
66NanoOcp1Client::NanoOcp1Client(const bool callbacksOnMessageThread, const juce::Thread::Priority threadPriority) :
67 NanoOcp1Client(juce::String(), 0, callbacksOnMessageThread, threadPriority)
68{
69}
70
71NanoOcp1Client::NanoOcp1Client(const juce::String& address, const int port, const bool callbacksOnMessageThread, const juce::Thread::Priority threadPriority) :
72 NanoOcp1Base(address, port), Ocp1Connection(callbacksOnMessageThread, threadPriority)
73{
74}
75
80
82{
83 m_running = true;
84
86 return true; // connection immediatly established
87 else
88 startTimer(500); // start trying to establish connection
89
90 return false;
91}
92
94{
95 m_running = false;
96
97 stopTimer();
98
99 disconnect(1000);
100
103
104 return !isConnected();
105}
106
108{
109 return m_running;
110}
111
113{
114 if (!isConnected())
115 return false;
116
117 return Ocp1Connection::sendMessage(data);
118}
119
121{
122 stopTimer();
123
126}
127
129{
132
133 if (m_running)
134 startTimer(500); // start trying to reestablish connection
135}
136
138{
139 processReceivedData(message);
140}
141
143{
144 if (connectToSocket(getAddress(), getPort(), 50))
145 stopTimer(); // connection established, no need to retry
146}
147
148//==============================================================================
149NanoOcp1Server::NanoOcp1Server(const bool callbacksOnMessageThread, const juce::Thread::Priority threadPriority) :
150 NanoOcp1Server(juce::String(), 0, callbacksOnMessageThread, threadPriority)
151{
152}
153
154NanoOcp1Server::NanoOcp1Server(const juce::String& address, const int port, const bool callbacksOnMessageThread, const juce::Thread::Priority threadPriority) :
155 NanoOcp1Base(address, port), Ocp1ConnectionServer(threadPriority), m_callbacksOnMessageThread(callbacksOnMessageThread), m_threadPriority(threadPriority)
156{
157}
158
163
168
170{
171 if (m_activeConnection)
172 {
173 m_activeConnection->disconnect(1000);
174 return !m_activeConnection->isConnected();
175 }
176 else
177 return true;
178}
179
181{
182 if (!m_activeConnection)
183 return false;
184
185 return m_activeConnection->sendData(data);
186}
187
189{
190 m_activeConnection = std::make_unique<NanoOcp1Client>(m_callbacksOnMessageThread, m_threadPriority);
191 m_activeConnection->onDataReceived = this->onDataReceived;
192
193 return m_activeConnection.get();
194}
195
196
197}
Abstract base class shared by NanoOcp1Client and NanoOcp1Server.
Definition NanoOcp1.h:130
NanoOcp1Base(const juce::String &address, const int port)
Definition NanoOcp1.cpp:27
const int getPort()
Returns the current target port number.
Definition NanoOcp1.cpp:52
std::function< void()> onConnectionEstablished
Fired once after a successful TCP connection is established.
Definition NanoOcp1.h:183
void setPort(const int port)
Sets the TCP port number of the remote OCA device. DS100 default: 50014.
Definition NanoOcp1.cpp:47
std::function< void()> onConnectionLost
Fired when the TCP connection is dropped or a connect attempt fails.
Definition NanoOcp1.h:193
bool processReceivedData(const ByteVector &data)
Called by derived classes when bytes arrive from the socket. Invokes onDataReceived if set; the frame...
Definition NanoOcp1.cpp:57
void setAddress(const juce::String &address)
Sets the IP address or hostname of the remote OCA device.
Definition NanoOcp1.cpp:37
const juce::String & getAddress()
Returns the current target address.
Definition NanoOcp1.cpp:42
std::function< bool(const ByteVector &)> onDataReceived
Fired when a complete OCP.1 frame is received.
Definition NanoOcp1.h:173
OCP.1 TCP client with automatic reconnection.
Definition NanoOcp1.h:238
bool isRunning()
Returns true if start() has been called and stop() has not.
Definition NanoOcp1.cpp:107
void timerCallback() override
Timer callback — attempts connectToSocket() when not yet connected.
Definition NanoOcp1.cpp:142
void messageReceived(const ByteVector &message) override
Called by Ocp1Connection for each received OCP.1 frame — invokes onDataReceived.
Definition NanoOcp1.cpp:137
void connectionMade() override
Called by Ocp1Connection when TCP connect succeeds — invokes onConnectionEstablished.
Definition NanoOcp1.cpp:120
NanoOcp1Client(const bool callbacksOnMessageThread, const juce::Thread::Priority threadPriority=juce::Thread::Priority::normal)
Constructs a client without an initial address/port. Call setAddress() and setPort() before start().
Definition NanoOcp1.cpp:66
bool stop() override
Stops the reconnect timer and closes the TCP socket.
Definition NanoOcp1.cpp:93
bool start() override
Starts the reconnect timer and begins attempting to connect.
Definition NanoOcp1.cpp:81
void connectionLost() override
Called by Ocp1Connection when TCP connection is lost — invokes onConnectionLost.
Definition NanoOcp1.cpp:128
bool sendData(const ByteVector &data) override
Sends serialized OCP.1 bytes over the active TCP connection. The bytes must be a complete,...
Definition NanoOcp1.cpp:112
OCP.1 TCP server that accepts a single incoming connection at a time.
Definition NanoOcp1.h:321
NanoOcp1Server(const bool callbacksOnMessageThread, const juce::Thread::Priority threadPriority=juce::Thread::Priority::normal)
Constructs a server without an initial bind address/port.
Definition NanoOcp1.cpp:149
bool start() override
Binds the socket and starts the accept loop thread.
Definition NanoOcp1.cpp:164
Ocp1Connection * createConnectionObject() override
Factory method called by the accept loop — creates the NanoOcp1Client peer object for the newly accep...
Definition NanoOcp1.cpp:188
bool stop() override
Stops the accept loop and closes any active connection.
Definition NanoOcp1.cpp:169
bool sendData(const ByteVector &data) override
Sends serialized OCP.1 bytes to the currently connected peer. If no peer is connected,...
Definition NanoOcp1.cpp:180
TCP accept-loop server base class for OCP.1 connections.
bool beginWaitingForSocket(int portNumber, const juce::String &bindAddress=juce::String())
Binds a TCP socket to the given port and starts the accept-loop thread.
Low-level TCP socket manager for a single OCP.1 connection.
bool connectToSocket(const juce::String &hostName, int portNumber, int timeOutMillisecs)
Attempts a TCP connection to the given host and port. Spawns the read thread on success.
void disconnect(int timeoutMs=0, Notify notify=Notify::yes)
Closes the TCP socket and stops the read thread.
bool isConnected() const
Returns true if the TCP socket is currently open.
bool sendMessage(const ByteVector &message)
Sends a complete OCP.1 frame over the TCP socket. Acquires the write lock, then writes all bytes in o...
Minimal AES70 / OCP.1 TCP client/server library built on JUCE.
Definition NanoOcp1.cpp:23
std::vector< std::uint8_t > ByteVector
Binary buffer type used throughout NanoOcp for all serialized OCP.1 data.