Compare commits

..

8 Commits

Author SHA1 Message Date
Joel Wetzell a117f668fa remove all fmt.println from receiveosc 2026-02-04 11:41:25 -06:00
Joel Wetzell 67a1cc083b remove fmt.println 2026-02-04 11:36:16 -06:00
Joel Wetzell 25c159013b fix variable name 2026-01-31 15:52:42 -06:00
Joel Wetzell 5ac60b0685 add support for SLIP and bundles 2026-01-31 15:52:08 -06:00
Joel Wetzell 6251acb852 use switch 2026-01-31 15:49:42 -06:00
Joel Wetzell 5600ef75f5 add method to parse bytes as packet 2026-01-31 15:49:07 -06:00
Joel Wetzell 77c92c5456 Merge pull request #14 from jwetzell/pointers
return pointers from byte parsing
2026-01-31 15:39:51 -06:00
Joel Wetzell 594c51d2a4 return pointers from byte parsing 2026-01-31 15:34:16 -06:00
6 changed files with 120 additions and 68 deletions
+13 -13
View File
@@ -21,25 +21,25 @@ func (b *OSCBundle) ToBytes() []byte {
return bytes return bytes
} }
func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) { func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
if len(bytes) < 20 { if len(bytes) < 20 {
return OSCBundle{}, bytes, errors.New("bundle has to be at least 20 bytes") return nil, bytes, errors.New("bundle has to be at least 20 bytes")
} }
if bytes[0] != 35 { if bytes[0] != 35 {
return OSCBundle{}, bytes, errors.New("bundle must start with a #") return nil, bytes, errors.New("bundle must start with a #")
} }
bundleHeader, bytesAfterBundleHeader := readOSCString(bytes) bundleHeader, bytesAfterBundleHeader := readOSCString(bytes)
if bundleHeader != "#bundle" { if bundleHeader != "#bundle" {
return OSCBundle{}, bytesAfterBundleHeader, errors.New("bundle must start with #bundle string") return nil, bytesAfterBundleHeader, errors.New("bundle must start with #bundle string")
} }
timeTag, bytesAfterTimeTag, err := readOSCTimeTag(bytesAfterBundleHeader) timeTag, bytesAfterTimeTag, err := readOSCTimeTag(bytesAfterBundleHeader)
if err != nil { if err != nil {
return OSCBundle{}, bytesAfterBundleHeader, err return nil, bytesAfterBundleHeader, err
} }
bundleContents := []OSCPacket{} bundleContents := []OSCPacket{}
@@ -52,13 +52,13 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
contentSize, bytesAfterContentSize, err := readOSCInt32(remainingBytes) contentSize, bytesAfterContentSize, err := readOSCInt32(remainingBytes)
if err != nil { if err != nil {
return OSCBundle{}, remainingBytes, err return nil, remainingBytes, err
} }
remainingBytes = bytesAfterContentSize remainingBytes = bytesAfterContentSize
if len(remainingBytes) < int(contentSize) { if len(remainingBytes) < int(contentSize) {
return OSCBundle{}, remainingBytes, errors.New("bundle doesn't have enough bytes for the content size it specifies") return nil, remainingBytes, errors.New("bundle doesn't have enough bytes for the content size it specifies")
} }
bundleContentBytes := remainingBytes[0:contentSize] bundleContentBytes := remainingBytes[0:contentSize]
@@ -66,17 +66,17 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
if bundleContentBytes[0] == 35 { if bundleContentBytes[0] == 35 {
content, _, err := BundleFromBytes(bundleContentBytes) content, _, err := BundleFromBytes(bundleContentBytes)
if err != nil { if err != nil {
return OSCBundle{}, remainingBytes, err return nil, remainingBytes, err
} }
bundleContents = append(bundleContents, &content) bundleContents = append(bundleContents, content)
} else if bundleContentBytes[0] == 47 { } else if bundleContentBytes[0] == 47 {
content, err := MessageFromBytes(bundleContentBytes) content, err := MessageFromBytes(bundleContentBytes)
if err != nil { if err != nil {
return OSCBundle{}, remainingBytes, err return nil, remainingBytes, err
} }
bundleContents = append(bundleContents, &content) bundleContents = append(bundleContents, content)
} else { } else {
return OSCBundle{}, remainingBytes, errors.New("bundle contents does not look a bundle or message") return nil, remainingBytes, errors.New("bundle contents does not look a bundle or message")
} }
remainingBytes = bytesAfterContentSize[contentSize:] remainingBytes = bytesAfterContentSize[contentSize:]
if len(remainingBytes) == 0 { if len(remainingBytes) == 0 {
@@ -85,7 +85,7 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
} }
return OSCBundle{ return &OSCBundle{
TimeTag: timeTag, TimeTag: timeTag,
Contents: bundleContents, Contents: bundleContents,
}, },
+4 -4
View File
@@ -10,12 +10,12 @@ func TestOSCBundleEncoding(t *testing.T) {
testCases := []struct { testCases := []struct {
description string description string
bundle OSCBundle bundle *OSCBundle
expected []byte expected []byte
}{ }{
{ {
"simple contents single message", "simple contents single message",
OSCBundle{ &OSCBundle{
TimeTag: OSCTimeTag{ TimeTag: OSCTimeTag{
seconds: 32, seconds: 32,
fractionalSeconds: 0, fractionalSeconds: 0,
@@ -46,12 +46,12 @@ func TestOSCBundleEncoding(t *testing.T) {
func TestOSCBundleDecoding(t *testing.T) { func TestOSCBundleDecoding(t *testing.T) {
testCases := []struct { testCases := []struct {
description string description string
expected OSCBundle expected *OSCBundle
bytes []byte bytes []byte
}{ }{
{ {
"simple contents single message", "simple contents single message",
OSCBundle{ &OSCBundle{
TimeTag: OSCTimeTag{ TimeTag: OSCTimeTag{
seconds: 32, seconds: 32,
fractionalSeconds: 0, fractionalSeconds: 0,
+60 -31
View File
@@ -12,30 +12,31 @@ import (
) )
func main() { func main() {
var Host string var IP string
var Port int32 var Port int32
var Protocol string var Protocol string
var Format string var Format string
var Slip bool
cmd := &cli.Command{ cmd := &cli.Command{
Name: "receiveosc", Name: "receiveosc",
Usage: "receive OSC messages via UDP or TCP", Usage: "receive OSC messages via UDP or TCP",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "host", Name: "ip",
Usage: "host to send OSC message to", Usage: "ip to receive OSC messages on",
Value: "127.0.0.1", Value: "0.0.0.0",
Destination: &Host, Destination: &IP,
}, },
&cli.Int32Flag{ &cli.Int32Flag{
Name: "port", Name: "port",
Usage: "port to send OSC message to", Usage: "port to receive OSC messages on",
Destination: &Port, Destination: &Port,
Value: 8888, Value: 8888,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "protocol", Name: "protocol",
Usage: "protocol to use to send (tcp or udp)", Usage: "protocol to use to receive (tcp or udp)",
Value: "udp", Value: "udp",
Destination: &Protocol, Destination: &Protocol,
Validator: func(flag string) error { Validator: func(flag string) error {
@@ -57,13 +58,23 @@ func main() {
return nil return nil
}, },
}, },
&cli.BoolFlag{
Name: "slip",
Value: false,
Usage: "whether to slip encode the OSC Message bytes",
Destination: &Slip,
},
}, },
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
netAddress := fmt.Sprintf("%s:%d", Host, Port) netAddress := fmt.Sprintf("%s:%d", IP, Port)
if Protocol == "udp" { switch Protocol {
case "udp":
listenUDP(netAddress, Format) listenUDP(netAddress, Format)
} else if Protocol == "tcp" { case "tcp":
listenTCP(netAddress, Format) if !Slip {
return fmt.Errorf("OSC 1.0 over TCP is not supported yet")
}
listenTCP(netAddress, Slip, Format)
} }
return nil return nil
}, },
@@ -74,10 +85,10 @@ func main() {
} }
} }
func listenTCP(netAddress string, format string) { func listenTCP(netAddress string, useSLIP bool, format string) {
socket, err := net.Listen("tcp4", netAddress) socket, err := net.Listen("tcp4", netAddress)
if err != nil { if err != nil {
fmt.Println(err) // TODO(jwetzell): output error properly
return return
} }
@@ -86,16 +97,16 @@ func listenTCP(netAddress string, format string) {
for { for {
conn, err := socket.Accept() conn, err := socket.Accept()
if err != nil { if err != nil {
fmt.Println(err) // TODO(jwetzell): output error properly
continue continue
} }
go handleConnection(conn, format) go handleTCPConnection(conn, useSLIP, format)
} }
} }
type SLIP struct { type SLIP struct {
pendingBytes []byte pendingBytes []byte
Messages chan osc.OSCMessage Packets chan osc.OSCPacket
} }
func (s *SLIP) decode(bytes []byte) { func (s *SLIP) decode(bytes []byte) {
@@ -121,14 +132,14 @@ func (s *SLIP) decode(bytes []byte) {
escapeNext = false escapeNext = false
} else if packetByte == END { } else if packetByte == END {
if len(s.pendingBytes) == 0 { if len(s.pendingBytes) == 0 {
// opening END byte, can discard // probably opening END byte, can discard
continue continue
} else { } else {
message, err := osc.MessageFromBytes(s.pendingBytes) oscPacket, _, err := osc.PacketFromBytes(s.pendingBytes)
if err != nil { if err != nil {
fmt.Println(err) panic(err)
} else { } else {
s.Messages <- message s.Packets <- oscPacket
} }
} }
s.pendingBytes = []byte{} s.pendingBytes = []byte{}
@@ -136,19 +147,18 @@ func (s *SLIP) decode(bytes []byte) {
s.pendingBytes = append(s.pendingBytes, packetByte) s.pendingBytes = append(s.pendingBytes, packetByte)
} }
} }
} }
func handleSLIP(slip SLIP, format string) { func handleSLIP(slip SLIP, format string) {
for message := range slip.Messages { for message := range slip.Packets {
handleMessage(message, format) handlePacket(message, format)
} }
} }
func handleConnection(conn net.Conn, format string) { func handleTCPConnection(conn net.Conn, useSLIP bool, format string) {
slip := SLIP{ slip := SLIP{
pendingBytes: []byte{}, pendingBytes: []byte{},
Messages: make(chan osc.OSCMessage), Packets: make(chan osc.OSCPacket),
} }
go handleSLIP(slip, format) go handleSLIP(slip, format)
@@ -161,12 +171,25 @@ func handleConnection(conn net.Conn, format string) {
if err != nil { if err != nil {
return return
} }
if useSLIP {
slip.decode(buffer[0:bytesRead])
} else {
// TODO(jwetzell): handle non-SLIP TCP messages properly
}
slip.decode(buffer[0:bytesRead])
} }
} }
func handleMessage(message osc.OSCMessage, format string) { func handlePacket(message osc.OSCPacket, format string) {
if bundle, ok := message.(*osc.OSCBundle); ok {
handleBundle(bundle, format)
} else if msg, ok := message.(*osc.OSCMessage); ok {
handleMessage(msg, format)
}
// TODO(jwetzell): handle other packet types?
}
func handleMessage(message *osc.OSCMessage, format string) {
if format == "json" { if format == "json" {
jsonData, _ := json.Marshal(message) jsonData, _ := json.Marshal(message)
fmt.Println(string(jsonData)) fmt.Println(string(jsonData))
@@ -175,17 +198,23 @@ func handleMessage(message osc.OSCMessage, format string) {
} }
} }
func handleBundle(bundle *osc.OSCBundle, format string) {
for _, packet := range bundle.Contents {
handlePacket(packet, format)
}
}
func listenUDP(netAddress string, format string) { func listenUDP(netAddress string, format string) {
s, err := net.ResolveUDPAddr("udp4", netAddress) s, err := net.ResolveUDPAddr("udp4", netAddress)
if err != nil { if err != nil {
fmt.Println(err) // TODO(jwetzell): output error properly
return return
} }
connection, err := net.ListenUDP("udp4", s) connection, err := net.ListenUDP("udp4", s)
if err != nil { if err != nil {
fmt.Println(err) // TODO(jwetzell): output error properly
return return
} }
@@ -199,11 +228,11 @@ func listenUDP(netAddress string, format string) {
panic(err) panic(err)
} }
oscMessage, err := osc.MessageFromBytes(buffer[0:bytesRead]) oscPacket, _, err := osc.PacketFromBytes(buffer[0:bytesRead])
if err != nil { if err != nil {
panic(err) panic(err)
} }
handleMessage(oscMessage, format) handlePacket(oscPacket, format)
} }
} }
+6 -6
View File
@@ -25,15 +25,15 @@ func (m *OSCMessage) ToBytes() []byte {
return oscBuffer return oscBuffer
} }
func MessageFromBytes(bytes []byte) (OSCMessage, error) { func MessageFromBytes(bytes []byte) (*OSCMessage, error) {
if len(bytes) == 0 { if len(bytes) == 0 {
return OSCMessage{}, errors.New("cannot create OSC Message from empty byte array") return nil, errors.New("cannot create OSC Message from empty byte array")
} }
address, typeAndArgBytes := readOSCString(bytes) address, typeAndArgBytes := readOSCString(bytes)
if address[0] != 47 { if address[0] != 47 {
return OSCMessage{}, errors.New("OSC Message address must start with /") return nil, errors.New("OSC Message address must start with /")
} }
oscMessage := OSCMessage{ oscMessage := OSCMessage{
@@ -46,17 +46,17 @@ func MessageFromBytes(bytes []byte) (OSCMessage, error) {
for index, oscType := range typeString { for index, oscType := range typeString {
if index == 0 { if index == 0 {
if oscType != ',' { if oscType != ',' {
return OSCMessage{}, errors.New("type string is malformed") return nil, errors.New("type string is malformed")
} }
} else { } else {
oscArg, remainingBytes, error := readOSCArg(argBytes, string(oscType)) oscArg, remainingBytes, error := readOSCArg(argBytes, string(oscType))
if error != nil { if error != nil {
return oscMessage, error return nil, error
} }
argBytes = remainingBytes argBytes = remainingBytes
oscMessage.Args = append(oscMessage.Args, oscArg) oscMessage.Args = append(oscMessage.Args, oscArg)
} }
} }
return oscMessage, nil return &oscMessage, nil
} }
+14 -14
View File
@@ -11,12 +11,12 @@ func TestOSCMessageEncoding(t *testing.T) {
testCases := []struct { testCases := []struct {
description string description string
message OSCMessage message *OSCMessage
expected []byte expected []byte
}{ }{
{ {
"simple hello", "simple hello",
OSCMessage{ &OSCMessage{
Address: "/hello", Address: "/hello",
Args: []OSCArg{}, Args: []OSCArg{},
}, },
@@ -24,7 +24,7 @@ func TestOSCMessageEncoding(t *testing.T) {
}, },
{ {
"simple address string arg", "simple address string arg",
OSCMessage{ &OSCMessage{
Address: "/hello", Address: "/hello",
Args: []OSCArg{ Args: []OSCArg{
{ {
@@ -37,47 +37,47 @@ func TestOSCMessageEncoding(t *testing.T) {
}, },
{ {
description: "simple address integer arg", description: "simple address integer arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: 35}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: 35}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35},
}, },
{ {
description: "simple address float arg", description: "simple address float arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: 34.5}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: 34.5}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0},
}, },
{ {
description: "simple address blob arg", description: "simple address blob arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98},
}, },
{ {
description: "simple address True arg", description: "simple address True arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{OSCArg{Type: "T", Value: true}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "T", Value: true}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0},
}, },
{ {
description: "simple address False arg", description: "simple address False arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0},
}, },
{ {
description: "simple address color arg", description: "simple address color arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10},
}, },
{ {
description: "simple address nil arg", description: "simple address nil arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0},
}, },
{ {
description: "simple address int64 arg", description: "simple address int64 arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: 281474976710655}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: 281474976710655}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255},
}, },
{ {
description: "simple address float64 arg", description: "simple address float64 arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: 12.7654763}}}, message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: 12.7654763}}},
expected: []byte{ expected: []byte{
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6, 47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
}, },
@@ -101,7 +101,7 @@ func TestOSCMessageEncoding(t *testing.T) {
// }, // },
{ {
description: "osc 1.0 spec example 1", description: "osc 1.0 spec example 1",
message: OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: 440}}}, message: &OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: 440}}},
expected: []byte{ expected: []byte{
47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44, 47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44,
102, 0, 0, 67, 220, 0, 0, 102, 0, 0, 67, 220, 0, 0,
@@ -109,7 +109,7 @@ func TestOSCMessageEncoding(t *testing.T) {
}, },
{ {
description: "osc 1.0 spec example 2", description: "osc 1.0 spec example 2",
message: OSCMessage{ message: &OSCMessage{
Address: "/foo", Address: "/foo",
Args: []OSCArg{ Args: []OSCArg{
{Type: "i", Value: 1000}, {Type: "i", Value: 1000},
+23
View File
@@ -350,3 +350,26 @@ func readOSCArg(bytes []byte, oscType string) (OSCArg, []byte, error) {
} }
return oscArg, remainingBytes, readArgError return oscArg, remainingBytes, readArgError
} }
func PacketFromBytes(bytes []byte) (OSCPacket, []byte, error) {
if len(bytes) == 0 {
return nil, bytes, errors.New("cannot create OSC Packet from empty byte array")
}
switch bytes[0] {
case '#':
bundle, remainingBytes, err := BundleFromBytes(bytes)
if err != nil {
return nil, bytes, err
}
return bundle, remainingBytes, nil
case '/':
message, err := MessageFromBytes(bytes)
if err != nil {
return nil, bytes, err
}
return message, []byte{}, nil
default:
return nil, bytes, errors.New("OSC Packet must start with # for bundle or / for message")
}
}