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

Low-level TCP socket manager for a single OCP.1 connection. More...

#include <Ocp1Connection.h>

+ Inheritance diagram for NanoOcp1::Ocp1Connection:

Classes

struct  ConnectionThread
 
class  SafeAction
 

Public Types

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 Member Functions

 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.
 
virtual void connectionMade ()=0
 Called when the TCP connection is successfully established. Override to react.
 
virtual void connectionLost ()=0
 Called when the TCP connection is dropped or closed. Override to react.
 
virtual void messageReceived (const ByteVector &message)=0
 Called with each complete OCP.1 frame received from the remote device. Pass message to Ocp1Message::UnmarshalOcp1Message() to get a typed message object.
 

Friends

class Ocp1ConnectionServer
 

Detailed Description

Low-level TCP socket manager for a single OCP.1 connection.

Derived from and inspired by juce::InterprocessConnection, but stripped of the JUCE IPC handshake header and all named-pipe support so it works as a plain TCP byte-stream suitable for OCP.1.

Role in the library

Ocp1Connection is abstract — it manages the socket, the dedicated read thread, and message framing, but delegates the three events to pure-virtual overrides:

NanoOcp1Client provides the concrete implementation: it bridges these calls into the NanoOcp1Base callback functions (onConnectionEstablished etc.) and optionally marshals them to the JUCE message thread.

Message framing

readNextMessage() reads from the socket, checks the OCP.1 sync byte (0x3b), reads the 10-byte header to determine total message size, then reads the remaining bytes. The complete frame is passed as a ByteVector to messageReceived(). Ocp1Message::UnmarshalOcp1Message() then parses it into a typed message object.

Thread safety

The read thread owns the socket exclusively. Writes go through sendMessage() which acquires socketLock (a ReadWriteLock). Callbacks are either delivered on the read thread (callbacksOnMessageThread=false) or posted asynchronously to the JUCE message thread.

Definition at line 70 of file Ocp1Connection.h.

Member Enumeration Documentation

◆ Notify

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.

Enumerator
no 
yes 

Definition at line 78 of file Ocp1Connection.h.

Constructor & Destructor Documentation

◆ Ocp1Connection()

NanoOcp1::Ocp1Connection::Ocp1Connection ( bool  callbacksOnMessageThread = true,
const juce::Thread::Priority  threadPriority = juce::Thread::Priority::normal 
)

Constructs the connection object.

Parameters
callbacksOnMessageThreadIf true, connectionMade(), connectionLost(), and messageReceived() are posted to the JUCE message thread. If false, they run directly on the socket read thread (lower latency).
threadPriorityOS priority of the socket read thread.

Definition at line 82 of file Ocp1Connection.cpp.

◆ ~Ocp1Connection()

NanoOcp1::Ocp1Connection::~Ocp1Connection ( )
virtual

Definition at line 89 of file Ocp1Connection.cpp.

References disconnect(), and no.

Member Function Documentation

◆ connectionLost()

virtual void NanoOcp1::Ocp1Connection::connectionLost ( )
pure virtual

Called when the TCP connection is dropped or closed. Override to react.

Implemented in NanoOcp1::NanoOcp1Client.

Referenced by NanoOcp1::ConnectionStateMessage::messageCallback().

◆ connectionMade()

virtual void NanoOcp1::Ocp1Connection::connectionMade ( )
pure virtual

Called when the TCP connection is successfully established. Override to react.

Implemented in NanoOcp1::NanoOcp1Client.

Referenced by NanoOcp1::ConnectionStateMessage::messageCallback().

◆ connectToSocket()

bool NanoOcp1::Ocp1Connection::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.

Parameters
hostNameIP address or hostname of the remote device.
portNumberTCP port number (DS100 default: 50014).
timeOutMillisecsMaximum time to wait for the TCP handshake.
Returns
True if the connection was established.

Definition at line 104 of file Ocp1Connection.cpp.

References disconnect().

Referenced by NanoOcp1::NanoOcp1Client::start(), and NanoOcp1::NanoOcp1Client::timerCallback().

◆ disconnect()

void NanoOcp1::Ocp1Connection::disconnect ( int  timeoutMs = 0,
Notify  notify = Notify::yes 
)

Closes the TCP socket and stops the read thread.

Parameters
timeoutMsMaximum ms to wait for the read thread to exit.
notifyWhether to invoke connectionLost() after closing. Pass Notify::no for a clean intentional shutdown.

Definition at line 121 of file Ocp1Connection.cpp.

References yes.

Referenced by connectToSocket(), NanoOcp1::NanoOcp1Client::stop(), and ~Ocp1Connection().

◆ getConnectedHostName()

juce::String NanoOcp1::Ocp1Connection::getConnectedHostName ( ) const

Returns the hostname of the currently connected remote peer, or an empty string.

Definition at line 155 of file Ocp1Connection.cpp.

◆ getSocket()

juce::StreamingSocket * NanoOcp1::Ocp1Connection::getSocket ( ) const
inlinenoexcept

Returns the underlying JUCE socket (for diagnostics).

Definition at line 114 of file Ocp1Connection.h.

◆ isConnected()

bool NanoOcp1::Ocp1Connection::isConnected ( ) const

Returns true if the TCP socket is currently open.

Definition at line 147 of file Ocp1Connection.cpp.

Referenced by NanoOcp1::NanoOcp1Client::sendData(), and NanoOcp1::NanoOcp1Client::stop().

◆ messageReceived()

virtual void NanoOcp1::Ocp1Connection::messageReceived ( const ByteVector message)
pure virtual

Called with each complete OCP.1 frame received from the remote device. Pass message to Ocp1Message::UnmarshalOcp1Message() to get a typed message object.

Implemented in NanoOcp1::NanoOcp1Client.

Referenced by NanoOcp1::DataDeliveryMessage::messageCallback().

◆ sendMessage()

bool NanoOcp1::Ocp1Connection::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.

Parameters
messageComplete serialized OCP.1 message bytes.
Returns
True if all bytes were written successfully.

Definition at line 171 of file Ocp1Connection.cpp.

Referenced by NanoOcp1::NanoOcp1Client::sendData().

Friends And Related Symbol Documentation

◆ Ocp1ConnectionServer

friend class Ocp1ConnectionServer
friend

Definition at line 145 of file Ocp1Connection.h.


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