NanoOcp
Minimal AES70 / OCP.1 TCP client/server library for d&b Soundscape devices
Loading...
Searching...
No Matches
Ocp1Connection.h
Go to the documentation of this file.
1/* Copyright (c) 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#else
24 #include <JuceHeader.h>
25#endif
26
27#include "Ocp1DataTypes.h"
28
29
30namespace NanoOcp1
31{
32
33
34class Ocp1ConnectionServer;
35
36
71{
72public:
78 enum class Notify { no, yes };
79
80public:
89 Ocp1Connection(bool callbacksOnMessageThread = true, const juce::Thread::Priority threadPriority = juce::Thread::Priority::normal);
90 virtual ~Ocp1Connection();
91
100 bool connectToSocket(const juce::String& hostName, int portNumber, int timeOutMillisecs);
101
108 void disconnect(int timeoutMs = 0, Notify notify = Notify::yes);
109
111 bool isConnected() const;
112
114 juce::StreamingSocket* getSocket() const noexcept { return socket.get(); }
115
117 juce::String getConnectedHostName() const;
118
125 bool sendMessage(const ByteVector& message);
126
127 //==============================================================================
129 virtual void connectionMade() = 0;
131 virtual void connectionLost() = 0;
136 virtual void messageReceived(const ByteVector& message) = 0;
137
138private:
139 //==============================================================================
140 juce::ReadWriteLock socketLock;
141 std::unique_ptr<juce::StreamingSocket> socket;
142 bool callbackConnectionState = false;
143 const bool useMessageThread;
144
146 void initialise();
147 void initialiseWithSocket(std::unique_ptr<juce::StreamingSocket>);
148 void deleteSocket();
149 void connectionMadeInt();
150 void connectionLostInt();
151 void deliverDataInt(const ByteVector&);
152 bool readNextMessage();
153 int readData(void*, int);
154
155 struct ConnectionThread;
156 std::unique_ptr<ConnectionThread> thread;
157 std::atomic<bool> threadIsRunning{ false };
158
159 class SafeAction;
160 std::shared_ptr<SafeAction> safeAction;
161
162 void runThread();
163 int writeData(void*, int);
164
165 juce::Thread::Priority m_threadPriority;
166
167 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Ocp1Connection)
168};
169
170}
TCP accept-loop server base class for OCP.1 connections.
Low-level TCP socket manager for a single OCP.1 connection.
virtual void connectionLost()=0
Called when the TCP connection is dropped or closed. Override to react.
Notify
Controls whether connectionLost() is called when disconnect() is invoked. Use Notify::no when shuttin...
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.
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.
virtual void messageReceived(const ByteVector &message)=0
Called with each complete OCP.1 frame received from the remote device. Pass message to Ocp1Message::U...
bool isConnected() const
Returns true if the TCP socket is currently open.
virtual void connectionMade()=0
Called when the TCP connection is successfully established. Override to react.
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.