Skip to content

Commit

Permalink
[oscquery] Add a mode to force use of webscockets and add oscquery_tc…
Browse files Browse the repository at this point in the history
…p to ossia max
  • Loading branch information
jcelerier committed May 5, 2024
1 parent 5805ca3 commit a450610
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/libremidi
61 changes: 61 additions & 0 deletions src/ossia-max/src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
#include <ossia/detail/config.hpp>

#include <ossia/network/base/parameter_data.hpp>
#include <ossia/network/context.hpp>
#include <ossia/network/local/local.hpp>
#include <ossia/network/minuit/minuit.hpp>
#include <ossia/network/osc/osc.hpp>
#include <ossia/network/oscquery/oscquery_client.hpp>
#include <ossia/network/oscquery/oscquery_server.hpp>
#include <ossia/protocols/oscquery/oscquery_mirror_asio.hpp>
#include <ossia/protocols/oscquery/oscquery_server_asio.hpp>

#include <boost/algorithm/string/case_conv.hpp>

Expand Down Expand Up @@ -101,6 +104,13 @@ void device::class_setup(t_class* c)
c, "savebi", 0, "onoff", "Save bi parameters when snapshotting presets");
}

void device::asio_timer(device* x)
{
x->network_context->context.poll();
x->network_context->context.reset();
clock_delay(x->network_poll_clock, 10);
}

void* device::create(t_symbol*, long argc, t_atom* argv)
{
auto x = make_ossia<device>();
Expand Down Expand Up @@ -142,6 +152,10 @@ void* device::create(t_symbol*, long argc, t_atom* argv)
// process attr args, if any
attr_args_process(x, argc - attrstart, argv + attrstart);

x->network_context = std::make_shared<ossia::net::network_context>();
x->network_poll_clock = clock_new(x, (method)asio_timer);
clock_delay(x->network_poll_clock, 10);

auto local_proto_ptr = std::make_unique<ossia::net::local_protocol>();

x->m_device = std::make_shared<ossia::net::generic_device>(
Expand Down Expand Up @@ -193,6 +207,10 @@ void device::destroy(device* x)
#endif
}

clock_free((t_object*)x->network_poll_clock);
x->network_poll_clock = nullptr;
x->network_context.reset();

x->disconnect_slots();
on_device_removing(x);

Expand Down Expand Up @@ -346,6 +364,49 @@ void device::expose(device* x, t_symbol*, long argc, t_atom* argv)
A_SETLONG(a, connected ? 1 : 0);
outlet_anything(x->m_dumpout, gensym("expose"), 4, a);
}
else if(protocol == "oscquery_tcp")
{
protocol_settings::oscquery settings{};

argc--;
argv++;

if(argc == 2 && argv[0].a_type == A_LONG && argv[1].a_type == A_LONG)
{
settings.oscport = atom_getlong(argv++);
settings.wsport = atom_getlong(argv++);
}

bool connected = true;

t_atom a[4];
A_SETSYM(a + 1, gensym("oscquery_tcp"));
A_SETLONG(a + 2, settings.oscport);
A_SETLONG(a + 3, settings.wsport);

try
{
auto oscq_proto
= std::make_unique<ossia::oscquery_asio::oscquery_server_protocol>(
x->network_context, settings.oscport, settings.wsport, true);
x->m_device->set_echo(true);

A_SETSYM(a + 1, gensym("oscquery_tcp"));
A_SETLONG(a + 2, oscq_proto->get_osc_port());
A_SETLONG(a + 3, oscq_proto->get_ws_port());

multiplex.expose_to(std::move(oscq_proto));
}
catch(const std::exception& e)
{
connected = false;
object_error((t_object*)x, "can't connect, port might be already in use");
object_error((t_object*)x, "libossia error: '%s'", e.what());
}

A_SETLONG(a, connected ? 1 : 0);
outlet_anything(x->m_dumpout, gensym("expose"), 4, a);
}
else if(protocol == "osc")
{
protocol_settings::osc settings{};
Expand Down
9 changes: 9 additions & 0 deletions src/ossia-max/src/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

#include <ossia-max/src/device_base.hpp>

namespace ossia::net
{
struct network_context;
using network_context_ptr = std::shared_ptr<network_context>;
}
namespace ossia
{
namespace max_binding
Expand All @@ -12,6 +17,8 @@ namespace max_binding
class device : public device_base
{
public:
ossia::net::network_context_ptr network_context;
void* network_poll_clock{};
using is_device = std::true_type;

static void register_children(device*);
Expand All @@ -32,6 +39,8 @@ class device : public device_base
static void* create(t_symbol*, long, t_atom*);
static void destroy(ossia::max_binding::device*);
static void class_setup(t_class* c);

static void asio_timer(device* x);
};

namespace protocol_settings
Expand Down
6 changes: 4 additions & 2 deletions src/ossia/protocols/oscquery/oscquery_server_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ is_same(const oscquery_client& clt, const ossia::net::message_origin_identifier&
}

oscquery_server_protocol::oscquery_server_protocol(
ossia::net::network_context_ptr ctx, uint16_t osc_port, uint16_t ws_port)
ossia::net::network_context_ptr ctx, uint16_t osc_port, uint16_t ws_port,
bool forceWS)
: protocol_base{flags{SupportsMultiplex}}
, m_context{std::move(ctx)}
, m_oscServer{std::make_unique<osc_receiver_impl>(
Expand All @@ -67,6 +68,7 @@ oscquery_server_protocol::oscquery_server_protocol(
m_context->context)}
, m_oscPort{osc_port}
, m_wsPort{ws_port}
, m_forceWS{forceWS}
{
m_clients.reserve(2);
m_websocketServer->set_open_handler(
Expand Down Expand Up @@ -187,7 +189,7 @@ bool oscquery_server_protocol::push_impl(const T& addr, const ossia::value& v)
if(val.valid())
{
// Push to all clients
auto critical = addr.get_critical();
auto critical = m_forceWS || addr.get_critical();
if(!critical)
{
lock_t lock(m_clientsMutex);
Expand Down
5 changes: 4 additions & 1 deletion src/ossia/protocols/oscquery/oscquery_server_asio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class OSSIA_EXPORT oscquery_server_protocol final : public ossia::net::protocol_
using connection_handler = std::weak_ptr<void>;
oscquery_server_protocol(
ossia::net::network_context_ptr ctx, uint16_t osc_port = 1234,
uint16_t ws_port = 5678);
uint16_t ws_port = 5678, bool forceWS = false);
~oscquery_server_protocol() override;

bool pull(net::parameter_base&) override;
Expand Down Expand Up @@ -140,6 +140,9 @@ class OSSIA_EXPORT oscquery_server_protocol final : public ossia::net::protocol_
// The local ports
uint16_t m_oscPort{};
uint16_t m_wsPort{};

// Will only send changes through WS
bool m_forceWS{};
};
}
}

0 comments on commit a450610

Please sign in to comment.