NanoOcp
Minimal AES70 / OCP.1 TCP client/server library for d&b Soundscape devices
Loading...
Searching...
No Matches
NanoOcp1::NanoOcp1Client Class Reference

OCP.1 TCP client with automatic reconnection. More...

#include <NanoOcp1.h>

+ Inheritance diagram for NanoOcp1::NanoOcp1Client:
+ Collaboration diagram for NanoOcp1::NanoOcp1Client:

Public Member Functions

 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().
 
 NanoOcp1Client (const juce::String &address, const int port, const bool callbacksOnMessageThread, const juce::Thread::Priority threadPriority=juce::Thread::Priority::normal)
 Constructs a client with address and port pre-configured.
 
 ~NanoOcp1Client () override
 
bool start () override
 Starts the reconnect timer and begins attempting to connect.
 
bool stop () override
 Stops the reconnect timer and closes the TCP socket.
 
bool isRunning ()
 Returns true if start() has been called and stop() has not.
 
bool sendData (const ByteVector &data) override
 Sends serialized OCP.1 bytes over the active TCP connection. The bytes must be a complete, framed OCP.1 message as produced by Ocp1Message::GetSerializedData().
 
void connectionMade () override
 Called by Ocp1Connection when TCP connect succeeds — invokes onConnectionEstablished.
 
void connectionLost () override
 Called by Ocp1Connection when TCP connection is lost — invokes onConnectionLost.
 
void messageReceived (const ByteVector &message) override
 Called by Ocp1Connection for each received OCP.1 frame — invokes onDataReceived.
 
- Public Member Functions inherited from NanoOcp1::NanoOcp1Base
 NanoOcp1Base (const juce::String &address, const int port)
 
virtual ~NanoOcp1Base ()
 
void setAddress (const juce::String &address)
 Sets the IP address or hostname of the remote OCA device.
 
const juce::String & getAddress ()
 Returns the current target address.
 
void setPort (const int port)
 Sets the TCP port number of the remote OCA device. DS100 default: 50014.
 
const int getPort ()
 Returns the current target port number.
 
- Public Member Functions inherited from NanoOcp1::Ocp1Connection
 Ocp1Connection (bool callbacksOnMessageThread=true, const juce::Thread::Priority threadPriority=juce::Thread::Priority::normal)
 Constructs the connection object.
 
virtual ~Ocp1Connection ()
 
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.
 
juce::StreamingSocket * getSocket () const noexcept
 Returns the underlying JUCE socket (for diagnostics).
 
juce::String getConnectedHostName () const
 Returns the hostname of the currently connected remote peer, or an empty string.
 
bool sendMessage (const ByteVector &message)
 Sends a complete OCP.1 frame over the TCP socket. Acquires the write lock, then writes all bytes in one blocking call.
 

Protected Member Functions

void timerCallback () override
 Timer callback — attempts connectToSocket() when not yet connected.
 
- Protected Member Functions inherited from NanoOcp1::NanoOcp1Base
bool processReceivedData (const ByteVector &data)
 Called by derived classes when bytes arrive from the socket. Invokes onDataReceived if set; the frame has already been delimited by Ocp1Connection::readNextMessage().
 

Additional Inherited Members

- Public Types inherited from NanoOcp1::Ocp1Connection
enum class  Notify {
  no ,
  yes
}
 Controls whether connectionLost() is called when disconnect() is invoked. Use Notify::no when shutting down deliberately so the application does not treat an intentional disconnect as an error. More...
 
- Public Attributes inherited from NanoOcp1::NanoOcp1Base
std::function< bool(const ByteVector &)> onDataReceived
 Fired when a complete OCP.1 frame is received.
 
std::function< void()> onConnectionEstablished
 Fired once after a successful TCP connection is established.
 
std::function< void()> onConnectionLost
 Fired when the TCP connection is dropped or a connect attempt fails.
 

Detailed Description

OCP.1 TCP client with automatic reconnection.

Inherits socket I/O from Ocp1Connection and reconnect timing from juce::Timer. When start() is called, a juce::Timer fires periodically and attempts connectToSocket() until it succeeds. Once connected, connectionMade() calls onConnectionEstablished. On disconnect (detected by the read thread), connectionLost() calls onConnectionLost and the timer resumes retrying.

Usage in DeviceController (Umsci)

DeviceController creates a NanoOcp1Client with callbacksOnMessageThread=false so that OCP.1 parsing runs on the socket thread, avoiding latency on the JUCE message thread. Parsed RemoteObject values are then posted to the message thread via juce::MessageListener.

m_ocp1Client = std::make_unique<NanoOcp1Client>("192.168.1.100", 50014, false);
m_ocp1Client->onConnectionEstablished = [this]() { handleConnected(); };
m_ocp1Client->onConnectionLost = [this]() { handleDisconnected(); };
m_ocp1Client->onDataReceived = [this](const ByteVector& d) {
return ocp1MessageReceived(d);
};
m_ocp1Client->start();
std::vector< std::uint8_t > ByteVector
Binary buffer type used throughout NanoOcp for all serialized OCP.1 data.

Definition at line 237 of file NanoOcp1.h.

Constructor & Destructor Documentation

◆ NanoOcp1Client() [1/2]

NanoOcp1::NanoOcp1Client::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().

Parameters
callbacksOnMessageThreadIf true, all three callbacks are marshaled to the JUCE message thread. If false, they fire on the socket thread (lower latency, but you must be thread-safe in the callbacks).
threadPriorityOS thread priority for the socket I/O thread.

Definition at line 66 of file NanoOcp1.cpp.

◆ NanoOcp1Client() [2/2]

NanoOcp1::NanoOcp1Client::NanoOcp1Client ( const juce::String &  address,
const int  port,
const bool  callbacksOnMessageThread,
const juce::Thread::Priority  threadPriority = juce::Thread::Priority::normal 
)

Constructs a client with address and port pre-configured.

Parameters
addressIP address or hostname of the OCA device.
portTCP port number (DS100 default: 50014).
callbacksOnMessageThreadSee other constructor.
threadPrioritySee other constructor.

Definition at line 71 of file NanoOcp1.cpp.

◆ ~NanoOcp1Client()

NanoOcp1::NanoOcp1Client::~NanoOcp1Client ( )
override

Definition at line 76 of file NanoOcp1.cpp.

References stop().

Member Function Documentation

◆ connectionLost()

void NanoOcp1::NanoOcp1Client::connectionLost ( )
overridevirtual

Called by Ocp1Connection when TCP connection is lost — invokes onConnectionLost.

Implements NanoOcp1::Ocp1Connection.

Definition at line 128 of file NanoOcp1.cpp.

References NanoOcp1::NanoOcp1Base::onConnectionLost.

◆ connectionMade()

void NanoOcp1::NanoOcp1Client::connectionMade ( )
overridevirtual

Called by Ocp1Connection when TCP connect succeeds — invokes onConnectionEstablished.

Implements NanoOcp1::Ocp1Connection.

Definition at line 120 of file NanoOcp1.cpp.

References NanoOcp1::NanoOcp1Base::onConnectionEstablished.

◆ isRunning()

bool NanoOcp1::NanoOcp1Client::isRunning ( )

Returns true if start() has been called and stop() has not.

Definition at line 107 of file NanoOcp1.cpp.

◆ messageReceived()

void NanoOcp1::NanoOcp1Client::messageReceived ( const ByteVector message)
overridevirtual

Called by Ocp1Connection for each received OCP.1 frame — invokes onDataReceived.

Implements NanoOcp1::Ocp1Connection.

Definition at line 137 of file NanoOcp1.cpp.

References NanoOcp1::NanoOcp1Base::processReceivedData().

◆ sendData()

bool NanoOcp1::NanoOcp1Client::sendData ( const ByteVector data)
overridevirtual

Sends serialized OCP.1 bytes over the active TCP connection. The bytes must be a complete, framed OCP.1 message as produced by Ocp1Message::GetSerializedData().

Implements NanoOcp1::NanoOcp1Base.

Definition at line 112 of file NanoOcp1.cpp.

References NanoOcp1::Ocp1Connection::isConnected(), and NanoOcp1::Ocp1Connection::sendMessage().

◆ start()

bool NanoOcp1::NanoOcp1Client::start ( )
overridevirtual

Starts the reconnect timer and begins attempting to connect.

Returns
True always; actual connection success is signalled via onConnectionEstablished.

Implements NanoOcp1::NanoOcp1Base.

Definition at line 81 of file NanoOcp1.cpp.

References NanoOcp1::Ocp1Connection::connectToSocket(), NanoOcp1::NanoOcp1Base::getAddress(), and NanoOcp1::NanoOcp1Base::getPort().

◆ stop()

bool NanoOcp1::NanoOcp1Client::stop ( )
overridevirtual

Stops the reconnect timer and closes the TCP socket.

Returns
True always.

Implements NanoOcp1::NanoOcp1Base.

Definition at line 93 of file NanoOcp1.cpp.

References NanoOcp1::Ocp1Connection::disconnect(), NanoOcp1::Ocp1Connection::isConnected(), and NanoOcp1::NanoOcp1Base::onConnectionLost.

Referenced by ~NanoOcp1Client().

◆ timerCallback()

void NanoOcp1::NanoOcp1Client::timerCallback ( )
overrideprotected

Timer callback — attempts connectToSocket() when not yet connected.

Definition at line 142 of file NanoOcp1.cpp.

References NanoOcp1::Ocp1Connection::connectToSocket(), NanoOcp1::NanoOcp1Base::getAddress(), and NanoOcp1::NanoOcp1Base::getPort().


The documentation for this class was generated from the following files: