Originally posted by alexford April 6, 2023
Hello! This is a great library. I have a question about how I would achieve something I'm trying to do:
For my use case, I need to send and receive OSC messages from the same port. I am integrating with a family of mixers (Behringer XR series, as an example), that "respond" to some OSC messages by sending a message back to the same port on the client that the original message was sent from.
So, I am trying to achieve this bidirectional communication by setting up both an OSCClient
for sending and an OSCServer
for receiving.
The issue I'm having is that when I set up an OSCClient
from this library and send an OSC message, the outgoing port is randomly assigned by the OS. The mixer then responds back to that random port. I understand this is part of the behavior of GCDAsyncUdpSocket
(and sockets in general), and a side effect of the two "sides" of this library each using their own socket.
I need an OSCServer
to be listening on the same port that I send from, but I won't know that port until I send the first message. I have experimented a bit with trying to send an initial message, then interrogate the udpClient
in the OSCClient
to find the port, then start the OSCServer
listening on that port. This required some minor modifications to OSCKit
's source to attempt. However, OSCClient
's use of GCDAsyncUdpSocket
doesn't lend itself to the enableReusePort
functionality (something about needing a delegate which the client does not use), so I couldn't get it to work cleanly and would have ended up making too many changes, so I came here for advice first.
Essentially, I'm looking for a hybrid of an OSCServer
and an OSCClient
that sends and receives OSC messages over the same socket. This is implemented in another library, also called OSCKit as something they call a "peer". However, I prefer the API, approach, and license of this library, so I'm hoping it can be done here as well.
Looking at the code, it seems like this could be made to work by basically copying the send
function from OSCClient
into OSCServer
, so that it sends over the same socket the server has already established.
Would it be strange to have OSCServer
basically implement OSCClient
, though? I'm sure there's a better way to accomplish this without muddying the server/client dichotomy you've established here. Maybe a subclass of OSCServer
that adds the send functionality? Looking for your feedback. I am happy to start a PR.
I'm very new to Swift and UDP, so maybe I've missed something obvious, too!