NanoOcp
Minimal AES70 / OCP.1 TCP client/server library for d&b Soundscape devices
Loading...
Searching...
No Matches
NanoOcp1.h
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#pragma once
20
21#ifdef JUCE_GLOBAL_MODULE_SETTINGS_INCLUDED
22 #include <juce_core/juce_core.h>
23 #include <juce_events/juce_events.h>
24#else
25 #include <JuceHeader.h>
26#endif
27
28
29#include "Ocp1Connection.h"
31#include "Ocp1DataTypes.h"
32
33
116namespace NanoOcp1
117{
118
130{
131public:
132 //==============================================================================
133 NanoOcp1Base(const juce::String& address, const int port);
134 virtual ~NanoOcp1Base();
135
137 void setAddress(const juce::String& address);
139 const juce::String& getAddress();
140
142 void setPort(const int port);
144 const int getPort();
145
146 //==============================================================================
148 virtual bool start() = 0;
150 virtual bool stop() = 0;
151
152 //==============================================================================
158 virtual bool sendData(const ByteVector& data) = 0;
159
160 //==============================================================================
173 std::function<bool(const ByteVector&)> onDataReceived;
174
183 std::function<void()> onConnectionEstablished;
184
193 std::function<void()> onConnectionLost;
194
195protected:
196 //==============================================================================
202 bool processReceivedData(const ByteVector& data);
203
204private:
205 //==============================================================================
206 juce::String m_address;
207 int m_port{ 0 };
208
209};
210
237class NanoOcp1Client : public NanoOcp1Base, public Ocp1Connection, public juce::Timer
238{
239public:
240 //==============================================================================
250 NanoOcp1Client(const bool callbacksOnMessageThread, const juce::Thread::Priority threadPriority=juce::Thread::Priority::normal);
251
259 NanoOcp1Client(const juce::String& address, const int port, const bool callbacksOnMessageThread, const juce::Thread::Priority threadPriority=juce::Thread::Priority::normal);
260 ~NanoOcp1Client() override;
261
262 //==============================================================================
267 bool start() override;
268
273 bool stop() override;
274
276 bool isRunning();
277
278 //==============================================================================
284 bool sendData(const ByteVector& data) override;
285
286 //==============================================================================
288 void connectionMade() override;
290 void connectionLost() override;
292 void messageReceived(const ByteVector& message) override;
293
294protected:
295 //==============================================================================
297 void timerCallback() override;
298
299private:
300 //==============================================================================
301 bool m_running{ false };
302};
303
321{
322public:
323 //==============================================================================
329 NanoOcp1Server(const bool callbacksOnMessageThread, const juce::Thread::Priority threadPriority=juce::Thread::Priority::normal);
330
338 NanoOcp1Server(const juce::String& address, const int port, const bool callbacksOnMessageThread, const juce::Thread::Priority threadPriority=juce::Thread::Priority::normal);
339 ~NanoOcp1Server() override;
340
341 //==============================================================================
346 bool start() override;
347
352 bool stop() override;
353
354 //==============================================================================
359 bool sendData(const ByteVector& data) override;
360
361protected:
362 //==============================================================================
368
369private:
370 //==============================================================================
371 std::unique_ptr<NanoOcp1Client> m_activeConnection;
372 bool m_callbacksOnMessageThread{ true };
373 juce::Thread::Priority m_threadPriority;
374};
375
376}
Abstract base class shared by NanoOcp1Client and NanoOcp1Server.
Definition NanoOcp1.h:130
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
virtual bool stop()=0
Stops the client/server and closes any open TCP connection.
std::function< void()> onConnectionLost
Fired when the TCP connection is dropped or a connect attempt fails.
Definition NanoOcp1.h:193
virtual bool start()=0
Starts the client/server. For the client, begins periodic reconnect attempts.
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
virtual bool sendData(const ByteVector &data)=0
Sends raw bytes over the active TCP connection.
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
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
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.
Low-level TCP socket manager for a single OCP.1 connection.
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.