radarsat1 / liblo

liblo is an implementation of the Open Sound Control protocol for POSIX systems
GNU Lesser General Public License v2.1
192 stars 60 forks source link

Sending OSC data through TCP data prepend #117

Closed sphaero closed 1 year ago

sphaero commented 3 years ago

I'm trying to trace why sending OSC through TCP (oscsend utility) gets data prepended. See this for example:

On one terminal run netcat:

nc -ln 127.0.0.1 1234

Then send an osc packet through tcp on a second terminal:

oscsend osc.tcp://localhost:1234 /sample/address iTfs 1 3.14 hello

In the first (netcat) terminal you will receive this output:

(/sample/address,iTfs@H��hello

What's that '(' character doing in front there?

If you try the same with UDP it doesn't give this character (oscsend osc.udp://localhost:1234 /sample/address iTfs 1 3.14 hello and nc -lun 127.0.0.1 1234). See this output from netcat:

/sample/address,iTfs@H��hello
malloch commented 1 year ago

@sphaero the ( character is the byte count prefix for the TCP message. In this case the OSC message is 40 bytes (16 bytes for the path, 8 bytes for the typestring + padding, 4 bytes each for the int and float, and 8 bytes for the string "hello").

sphaero commented 1 year ago

But why is it there. A regular netcat doesn't show any size prefixes? Is liblo adding a prefix when TCP is the transport?

radarsat1 commented 1 year ago

Hi @sphaero, when OSC is sent over UDP, there is one message per datagram, so there is no need to packetize.

However, when sent over a TCP connection, multiple messages can be sent on the same stream, so they must be packetized some way. Liblo supports two methods of packetization. One is prefix-count, where a 32-bit integer is prepended to every message indicating how long it is. The other way, which seems to be more generally accepted, but not Liblo's default, is the SLIP protocol, which marks end-of-message with a special code.

On the server side, when Liblo accepts an incoming TCP stream, it sniffs out which method is being used based on the first few bytes.

radarsat1 commented 1 year ago

Closing due to inactivity.