3#include <ossia/detail/thread.hpp>
5#include <oscpack/ip/UdpSocket.h>
6#include <oscpack/osc/OscDebug.h>
7#include <oscpack/osc/OscPacketListener.h>
20template <
typename Impl_T>
21struct ClearListener :
public oscpack::TimerListener
23 ClearListener(UdpSocket<Impl_T>& s)
27 UdpSocket<Impl_T>& socket;
29 void TimerExpired()
override { socket.AsynchronousBreak(); }
32template <
typename Impl_T>
33class ReceiveSocket :
public UdpSocket<Impl_T>
35 SocketReceiveMultiplexer<Impl_T> mux_;
36 PacketListener* listener_;
39 ReceiveSocket(
const IpEndpointName& localEndpoint, PacketListener* listener)
42 this->Bind(localEndpoint);
43 mux_.AttachSocketListener(&this->impl_, listener_);
46 ~ReceiveSocket() { mux_.DetachSocketListener(&this->impl_, listener_); }
49 void Run() { mux_.Run(); }
52 ClearListener<Impl_T> l{*
this};
53 mux_.AttachPeriodicTimerListener(0, &l);
56 void AsynchronousBreak()
58 ClearListener<Impl_T> l{*
this};
59 mux_.AttachPeriodicTimerListener(0, &l);
60 mux_.AsynchronousBreak();
64using ReceiveSocket = detail::UdpListeningReceiveSocket<detail::Implementation>;
69template <
typename MessageHandler>
75class listener final :
public oscpack::OscPacketListener
79 : m_messageHandler{msg}
84 const oscpack::ReceivedMessage& m,
const oscpack::IpEndpointName& ip)
override
88 m_messageHandler(m, ip);
90 catch(std::exception& e)
96 "osc::listener::ProcessMessage error: '{}': {}", s.str(), e.what());
101 oscpack::debug(s, m);
102 ossia::logger().error(
"osc::listener::ProcessMessage error: '{}'", s.str());
107 const char* data,
int size,
const oscpack::IpEndpointName& remoteEndpoint)
override
111 oscpack::ReceivedPacket p(data, size);
113 this->ProcessBundle(oscpack::ReceivedBundle(p), remoteEndpoint);
115 this->ProcessMessage(oscpack::ReceivedMessage(p), remoteEndpoint);
117 catch(std::exception& e)
119 ossia::logger().error(
"osc::listener::ProcessPacket error: {}", e.what());
128 MessageHandler m_messageHandler;
140 template <
typename Handler>
141 receiver(
unsigned int port, Handler msg)
142 : m_impl{std::make_unique<listener<Handler>>(msg)}
151 m_impl = std::move(other.m_impl);
152 m_socket = std::move(other.m_socket);
153 setPort(other.m_port);
160 m_impl = std::move(other.m_impl);
161 m_socket = std::move(other.m_socket);
163 setPort(other.m_port);
172 if(m_runThread.joinable())
175 m_runThread = std::thread([
this] {
176 ossia::set_thread_name(
"ossia osc");
180 std::this_thread::sleep_for(std::chrono::microseconds(1));
202 if(m_runThread.joinable())
206 oscpack::UdpTransmitSocket send_socket(
207 oscpack::IpEndpointName(
"127.0.0.1", port()));
208 send_socket.Send(
"__stop_", 8);
209 m_socket->AsynchronousBreak();
210 std::this_thread::sleep_for(std::chrono::milliseconds(50));
214 catch(std::exception& e)
216 if(m_runThread.joinable())
217 m_runThread.detach();
225 if(m_runThread.joinable())
228 m_runThread.detach();
233 unsigned int port()
const {
return m_port; }
235 unsigned int setPort(
unsigned int port)
244 m_socket = std::make_unique<oscpack::ReceiveSocket>(
245 oscpack::IpEndpointName(oscpack::IpEndpointName::ANY_ADDRESS, m_port),
249 catch(std::runtime_error&)
259 unsigned int m_port = 0;
260 std::unique_ptr<oscpack::OscPacketListener> m_impl;
261 std::unique_ptr<oscpack::ReceiveSocket> m_socket;
263 std::thread m_runThread;
264 std::atomic_bool m_running =
false;
The listener class.
Definition receiver.hpp:76
The receiver class.
Definition receiver.hpp:138
spdlog::logger & logger() noexcept
Where the errors will be logged. Default is stderr.
Definition context.cpp:118