|
NanoOcp
Minimal AES70 / OCP.1 TCP client/server library for d&b Soundscape devices
|
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 |
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.
Ocp1Connection is abstract — it manages the socket, the dedicated read thread, and message framing, but delegates the three events to pure-virtual overrides:
connectionMade() — TCP handshake succeeded.connectionLost() — TCP dropped or disconnected.messageReceived() — a complete OCP.1 frame arrived.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.
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.
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.
|
strong |
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.
| NanoOcp1::Ocp1Connection::Ocp1Connection | ( | bool | callbacksOnMessageThread = true, |
| const juce::Thread::Priority | threadPriority = juce::Thread::Priority::normal |
||
| ) |
Constructs the connection object.
| callbacksOnMessageThread | If true, connectionMade(), connectionLost(), and messageReceived() are posted to the JUCE message thread. If false, they run directly on the socket read thread (lower latency). |
| threadPriority | OS priority of the socket read thread. |
Definition at line 82 of file Ocp1Connection.cpp.
|
virtual |
Definition at line 89 of file Ocp1Connection.cpp.
References disconnect(), and no.
|
pure virtual |
Called when the TCP connection is dropped or closed. Override to react.
Implemented in NanoOcp1::NanoOcp1Client.
Referenced by NanoOcp1::ConnectionStateMessage::messageCallback().
|
pure virtual |
Called when the TCP connection is successfully established. Override to react.
Implemented in NanoOcp1::NanoOcp1Client.
Referenced by NanoOcp1::ConnectionStateMessage::messageCallback().
| 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.
| hostName | IP address or hostname of the remote device. |
| portNumber | TCP port number (DS100 default: 50014). |
| timeOutMillisecs | Maximum time to wait for the TCP handshake. |
Definition at line 104 of file Ocp1Connection.cpp.
References disconnect().
Referenced by NanoOcp1::NanoOcp1Client::start(), and NanoOcp1::NanoOcp1Client::timerCallback().
| void NanoOcp1::Ocp1Connection::disconnect | ( | int | timeoutMs = 0, |
| Notify | notify = Notify::yes |
||
| ) |
Closes the TCP socket and stops the read thread.
| timeoutMs | Maximum ms to wait for the read thread to exit. |
| notify | Whether 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().
| 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.
|
inlinenoexcept |
Returns the underlying JUCE socket (for diagnostics).
Definition at line 114 of file Ocp1Connection.h.
| 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().
|
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().
| 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.
| message | Complete serialized OCP.1 message bytes. |
Definition at line 171 of file Ocp1Connection.cpp.
Referenced by NanoOcp1::NanoOcp1Client::sendData().
|
friend |
Definition at line 145 of file Ocp1Connection.h.