mirror of
https://github.com/jwetzell/osc-go.git
synced 2026-07-26 10:28:42 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a117f668fa | |||
| 67a1cc083b | |||
| 25c159013b | |||
| 5ac60b0685 | |||
| 6251acb852 | |||
| 5600ef75f5 | |||
| 77c92c5456 | |||
| 594c51d2a4 | |||
| d7c6da5c5b | |||
| 6f3a65322b | |||
| e34a589c5f | |||
| 41112d583b | |||
| 24a0501a4d | |||
| e06a0a2776 | |||
| d1696baa31 | |||
| c0304bf7a6 |
@@ -36,7 +36,7 @@ jobs:
|
||||
- goarch: "386"
|
||||
goos: darwin
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/checkout@v6
|
||||
- uses: wangyoucao577/go-release-action@v1
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
@@ -36,7 +36,7 @@ jobs:
|
||||
- goarch: "386"
|
||||
goos: darwin
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/checkout@v6
|
||||
- uses: wangyoucao577/go-release-action@v1
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
@@ -36,7 +36,7 @@ jobs:
|
||||
- goarch: "386"
|
||||
goos: darwin
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
- uses: actions/checkout@v6
|
||||
- uses: wangyoucao577/go-release-action@v1
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
@@ -21,25 +21,25 @@ func (b *OSCBundle) ToBytes() []byte {
|
||||
return bytes
|
||||
}
|
||||
|
||||
func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
|
||||
func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
|
||||
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 {
|
||||
return OSCBundle{}, bytes, errors.New("bundle must start with a #")
|
||||
return nil, bytes, errors.New("bundle must start with a #")
|
||||
}
|
||||
|
||||
bundleHeader, bytesAfterBundleHeader := readOSCString(bytes)
|
||||
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
return OSCBundle{}, bytesAfterBundleHeader, err
|
||||
return nil, bytesAfterBundleHeader, err
|
||||
}
|
||||
|
||||
bundleContents := []OSCPacket{}
|
||||
@@ -52,13 +52,13 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
|
||||
contentSize, bytesAfterContentSize, err := readOSCInt32(remainingBytes)
|
||||
|
||||
if err != nil {
|
||||
return OSCBundle{}, remainingBytes, err
|
||||
return nil, remainingBytes, err
|
||||
}
|
||||
|
||||
remainingBytes = bytesAfterContentSize
|
||||
|
||||
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]
|
||||
@@ -66,17 +66,17 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
|
||||
if bundleContentBytes[0] == 35 {
|
||||
content, _, err := BundleFromBytes(bundleContentBytes)
|
||||
if err != nil {
|
||||
return OSCBundle{}, remainingBytes, err
|
||||
return nil, remainingBytes, err
|
||||
}
|
||||
bundleContents = append(bundleContents, &content)
|
||||
bundleContents = append(bundleContents, content)
|
||||
} else if bundleContentBytes[0] == 47 {
|
||||
content, err := MessageFromBytes(bundleContentBytes)
|
||||
if err != nil {
|
||||
return OSCBundle{}, remainingBytes, err
|
||||
return nil, remainingBytes, err
|
||||
}
|
||||
bundleContents = append(bundleContents, &content)
|
||||
bundleContents = append(bundleContents, content)
|
||||
} 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:]
|
||||
if len(remainingBytes) == 0 {
|
||||
@@ -85,7 +85,7 @@ func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
|
||||
|
||||
}
|
||||
|
||||
return OSCBundle{
|
||||
return &OSCBundle{
|
||||
TimeTag: timeTag,
|
||||
Contents: bundleContents,
|
||||
},
|
||||
|
||||
+4
-4
@@ -10,12 +10,12 @@ func TestOSCBundleEncoding(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
description string
|
||||
bundle OSCBundle
|
||||
bundle *OSCBundle
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
"simple contents single message",
|
||||
OSCBundle{
|
||||
&OSCBundle{
|
||||
TimeTag: OSCTimeTag{
|
||||
seconds: 32,
|
||||
fractionalSeconds: 0,
|
||||
@@ -46,12 +46,12 @@ func TestOSCBundleEncoding(t *testing.T) {
|
||||
func TestOSCBundleDecoding(t *testing.T) {
|
||||
testCases := []struct {
|
||||
description string
|
||||
expected OSCBundle
|
||||
expected *OSCBundle
|
||||
bytes []byte
|
||||
}{
|
||||
{
|
||||
"simple contents single message",
|
||||
OSCBundle{
|
||||
&OSCBundle{
|
||||
TimeTag: OSCTimeTag{
|
||||
seconds: 32,
|
||||
fractionalSeconds: 0,
|
||||
|
||||
@@ -147,9 +147,9 @@ func slipEncode(bytes []byte) []byte {
|
||||
|
||||
for _, byteToEncode := range bytes {
|
||||
if byteToEncode == END {
|
||||
encodedBytes = append(encodedBytes, ESC_END)
|
||||
encodedBytes = append(encodedBytes, ESC, ESC_END)
|
||||
} else if byteToEncode == ESC {
|
||||
encodedBytes = append(encodedBytes, ESC_ESC)
|
||||
encodedBytes = append(encodedBytes, ESC, ESC_ESC)
|
||||
} else {
|
||||
encodedBytes = append(encodedBytes, byteToEncode)
|
||||
}
|
||||
|
||||
@@ -12,30 +12,31 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
var Host string
|
||||
var IP string
|
||||
var Port int32
|
||||
var Protocol string
|
||||
var Format string
|
||||
var Slip bool
|
||||
|
||||
cmd := &cli.Command{
|
||||
Name: "receiveosc",
|
||||
Usage: "receive OSC messages via UDP or TCP",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "host",
|
||||
Usage: "host to send OSC message to",
|
||||
Value: "127.0.0.1",
|
||||
Destination: &Host,
|
||||
Name: "ip",
|
||||
Usage: "ip to receive OSC messages on",
|
||||
Value: "0.0.0.0",
|
||||
Destination: &IP,
|
||||
},
|
||||
&cli.Int32Flag{
|
||||
Name: "port",
|
||||
Usage: "port to send OSC message to",
|
||||
Usage: "port to receive OSC messages on",
|
||||
Destination: &Port,
|
||||
Value: 8888,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "protocol",
|
||||
Usage: "protocol to use to send (tcp or udp)",
|
||||
Usage: "protocol to use to receive (tcp or udp)",
|
||||
Value: "udp",
|
||||
Destination: &Protocol,
|
||||
Validator: func(flag string) error {
|
||||
@@ -57,13 +58,23 @@ func main() {
|
||||
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 {
|
||||
netAddress := fmt.Sprintf("%s:%d", Host, Port)
|
||||
if Protocol == "udp" {
|
||||
netAddress := fmt.Sprintf("%s:%d", IP, Port)
|
||||
switch Protocol {
|
||||
case "udp":
|
||||
listenUDP(netAddress, Format)
|
||||
} else if Protocol == "tcp" {
|
||||
listenTCP(netAddress, Format)
|
||||
case "tcp":
|
||||
if !Slip {
|
||||
return fmt.Errorf("OSC 1.0 over TCP is not supported yet")
|
||||
}
|
||||
listenTCP(netAddress, Slip, Format)
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
// TODO(jwetzell): output error properly
|
||||
return
|
||||
}
|
||||
|
||||
@@ -86,16 +97,16 @@ func listenTCP(netAddress string, format string) {
|
||||
for {
|
||||
conn, err := socket.Accept()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
// TODO(jwetzell): output error properly
|
||||
continue
|
||||
}
|
||||
go handleConnection(conn, format)
|
||||
go handleTCPConnection(conn, useSLIP, format)
|
||||
}
|
||||
}
|
||||
|
||||
type SLIP struct {
|
||||
pendingBytes []byte
|
||||
Messages chan osc.OSCMessage
|
||||
Packets chan osc.OSCPacket
|
||||
}
|
||||
|
||||
func (s *SLIP) decode(bytes []byte) {
|
||||
@@ -121,14 +132,14 @@ func (s *SLIP) decode(bytes []byte) {
|
||||
escapeNext = false
|
||||
} else if packetByte == END {
|
||||
if len(s.pendingBytes) == 0 {
|
||||
// opening END byte, can discard
|
||||
// probably opening END byte, can discard
|
||||
continue
|
||||
} else {
|
||||
message, err := osc.MessageFromBytes(s.pendingBytes)
|
||||
oscPacket, _, err := osc.PacketFromBytes(s.pendingBytes)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
panic(err)
|
||||
} else {
|
||||
s.Messages <- message
|
||||
s.Packets <- oscPacket
|
||||
}
|
||||
}
|
||||
s.pendingBytes = []byte{}
|
||||
@@ -136,19 +147,18 @@ func (s *SLIP) decode(bytes []byte) {
|
||||
s.pendingBytes = append(s.pendingBytes, packetByte)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func handleSLIP(slip SLIP, format string) {
|
||||
for message := range slip.Messages {
|
||||
handleMessage(message, format)
|
||||
for message := range slip.Packets {
|
||||
handlePacket(message, format)
|
||||
}
|
||||
}
|
||||
|
||||
func handleConnection(conn net.Conn, format string) {
|
||||
func handleTCPConnection(conn net.Conn, useSLIP bool, format string) {
|
||||
slip := SLIP{
|
||||
pendingBytes: []byte{},
|
||||
Messages: make(chan osc.OSCMessage),
|
||||
Packets: make(chan osc.OSCPacket),
|
||||
}
|
||||
go handleSLIP(slip, format)
|
||||
|
||||
@@ -161,12 +171,25 @@ func handleConnection(conn net.Conn, format string) {
|
||||
if err != nil {
|
||||
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" {
|
||||
jsonData, _ := json.Marshal(message)
|
||||
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) {
|
||||
|
||||
s, err := net.ResolveUDPAddr("udp4", netAddress)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
// TODO(jwetzell): output error properly
|
||||
return
|
||||
}
|
||||
|
||||
connection, err := net.ListenUDP("udp4", s)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
// TODO(jwetzell): output error properly
|
||||
return
|
||||
}
|
||||
|
||||
@@ -199,11 +228,11 @@ func listenUDP(netAddress string, format string) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
oscMessage, err := osc.MessageFromBytes(buffer[0:bytesRead])
|
||||
oscPacket, _, err := osc.PacketFromBytes(buffer[0:bytesRead])
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
handleMessage(oscMessage, format)
|
||||
handlePacket(oscPacket, format)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -177,9 +178,9 @@ func slipEncode(bytes []byte) []byte {
|
||||
|
||||
for _, byteToEncode := range bytes {
|
||||
if byteToEncode == END {
|
||||
encodedBytes = append(encodedBytes, ESC_END)
|
||||
encodedBytes = append(encodedBytes, ESC, ESC_END)
|
||||
} else if byteToEncode == ESC {
|
||||
encodedBytes = append(encodedBytes, ESC_ESC)
|
||||
encodedBytes = append(encodedBytes, ESC, ESC_ESC)
|
||||
} else {
|
||||
encodedBytes = append(encodedBytes, byteToEncode)
|
||||
}
|
||||
@@ -211,6 +212,12 @@ func send(host string, port int32, address string, args []string, types []string
|
||||
|
||||
if slip {
|
||||
oscMessageBuffer = slipEncode(oscMessageBuffer)
|
||||
} else if protocol == "tcp" {
|
||||
// OSC 1.0 prepends a 4 byte size header for non-SLIP TCP messages
|
||||
size := uint32(len(oscMessageBuffer))
|
||||
sizeBytes := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(sizeBytes, size)
|
||||
oscMessageBuffer = append(sizeBytes, oscMessageBuffer...)
|
||||
}
|
||||
|
||||
netAddress := fmt.Sprintf("%s:%d", host, port)
|
||||
|
||||
@@ -2,4 +2,4 @@ module github.com/jwetzell/osc-go
|
||||
|
||||
go 1.25.1
|
||||
|
||||
require github.com/urfave/cli/v3 v3.6.1
|
||||
require github.com/urfave/cli/v3 v3.6.2
|
||||
|
||||
@@ -4,7 +4,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/urfave/cli/v3 v3.6.1 h1:j8Qq8NyUawj/7rTYdBGrxcH7A/j7/G8Q5LhWEW4G3Mo=
|
||||
github.com/urfave/cli/v3 v3.6.1/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
|
||||
github.com/urfave/cli/v3 v3.6.2 h1:lQuqiPrZ1cIz8hz+HcrG0TNZFxU70dPZ3Yl+pSrH9A8=
|
||||
github.com/urfave/cli/v3 v3.6.2/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
+9
-5
@@ -25,11 +25,15 @@ func (m *OSCMessage) ToBytes() []byte {
|
||||
return oscBuffer
|
||||
}
|
||||
|
||||
func MessageFromBytes(bytes []byte) (OSCMessage, error) {
|
||||
func MessageFromBytes(bytes []byte) (*OSCMessage, error) {
|
||||
if len(bytes) == 0 {
|
||||
return nil, errors.New("cannot create OSC Message from empty byte array")
|
||||
}
|
||||
|
||||
address, typeAndArgBytes := readOSCString(bytes)
|
||||
|
||||
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{
|
||||
@@ -42,17 +46,17 @@ func MessageFromBytes(bytes []byte) (OSCMessage, error) {
|
||||
for index, oscType := range typeString {
|
||||
if index == 0 {
|
||||
if oscType != ',' {
|
||||
return OSCMessage{}, errors.New("type string is malformed")
|
||||
return nil, errors.New("type string is malformed")
|
||||
}
|
||||
} else {
|
||||
oscArg, remainingBytes, error := readOSCArg(argBytes, string(oscType))
|
||||
if error != nil {
|
||||
return oscMessage, error
|
||||
return nil, error
|
||||
}
|
||||
argBytes = remainingBytes
|
||||
oscMessage.Args = append(oscMessage.Args, oscArg)
|
||||
}
|
||||
}
|
||||
|
||||
return oscMessage, nil
|
||||
return &oscMessage, nil
|
||||
}
|
||||
|
||||
+14
-14
@@ -11,12 +11,12 @@ func TestOSCMessageEncoding(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
description string
|
||||
message OSCMessage
|
||||
message *OSCMessage
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
"simple hello",
|
||||
OSCMessage{
|
||||
&OSCMessage{
|
||||
Address: "/hello",
|
||||
Args: []OSCArg{},
|
||||
},
|
||||
@@ -24,7 +24,7 @@ func TestOSCMessageEncoding(t *testing.T) {
|
||||
},
|
||||
{
|
||||
"simple address string arg",
|
||||
OSCMessage{
|
||||
&OSCMessage{
|
||||
Address: "/hello",
|
||||
Args: []OSCArg{
|
||||
{
|
||||
@@ -37,47 +37,47 @@ func TestOSCMessageEncoding(t *testing.T) {
|
||||
},
|
||||
{
|
||||
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},
|
||||
},
|
||||
{
|
||||
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},
|
||||
},
|
||||
{
|
||||
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},
|
||||
},
|
||||
{
|
||||
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},
|
||||
},
|
||||
{
|
||||
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},
|
||||
},
|
||||
{
|
||||
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},
|
||||
},
|
||||
{
|
||||
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},
|
||||
},
|
||||
{
|
||||
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},
|
||||
},
|
||||
{
|
||||
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{
|
||||
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",
|
||||
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{
|
||||
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,
|
||||
@@ -109,7 +109,7 @@ func TestOSCMessageEncoding(t *testing.T) {
|
||||
},
|
||||
{
|
||||
description: "osc 1.0 spec example 2",
|
||||
message: OSCMessage{
|
||||
message: &OSCMessage{
|
||||
Address: "/foo",
|
||||
Args: []OSCArg{
|
||||
{Type: "i", Value: 1000},
|
||||
|
||||
@@ -174,17 +174,13 @@ func argsToBuffer(args []OSCArg) []byte {
|
||||
func readOSCString(bytes []byte) (string, []byte) {
|
||||
//TODO(jwetzell): add error handling
|
||||
oscString := ""
|
||||
stringFinished := false
|
||||
stringEndIndex := 0
|
||||
remainingBytes := []byte{}
|
||||
|
||||
for index, byteIn := range bytes {
|
||||
if !stringFinished {
|
||||
if byteIn == 0 {
|
||||
oscString = string(bytes[0:index])
|
||||
stringEndIndex = index + 1
|
||||
break
|
||||
}
|
||||
if byteIn == 0 {
|
||||
oscString = string(bytes[0:index])
|
||||
stringEndIndex = index + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,7 +190,7 @@ func readOSCString(bytes []byte) (string, []byte) {
|
||||
stringEndIndex = stringEndIndex + stringPadding
|
||||
}
|
||||
|
||||
remainingBytes = bytes[stringEndIndex:]
|
||||
remainingBytes := bytes[stringEndIndex:]
|
||||
|
||||
return oscString, remainingBytes
|
||||
}
|
||||
@@ -354,3 +350,26 @@ func readOSCArg(bytes []byte, oscType string) (OSCArg, []byte, error) {
|
||||
}
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user