mirror of
https://github.com/jwetzell/osc-go.git
synced 2026-07-26 10:28:42 +00:00
Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d7c6da5c5b | |||
| 6f3a65322b | |||
| e34a589c5f | |||
| 41112d583b | |||
| 24a0501a4d | |||
| e06a0a2776 | |||
| d1696baa31 | |||
| c0304bf7a6 | |||
| d62d65c386 | |||
| f2888cc038 | |||
| 24077a77d6 | |||
| 9e515cc28e | |||
| 1ad942e489 | |||
| e748adfa1c | |||
| 74a62f6287 | |||
| a1a550b4bb | |||
| c039245298 | |||
| 085bd082b4 | |||
| 048eb57d5a |
@@ -1,4 +1,4 @@
|
||||
name: "release go binaries for multiple os/arch"
|
||||
name: "release go binaries for makeosc"
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -36,7 +36,7 @@ jobs:
|
||||
- goarch: "386"
|
||||
goos: darwin
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
- uses: wangyoucao577/go-release-action@v1
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
name: "release go binaries for multiple os/arch"
|
||||
name: "release go binaries for receiveosc"
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -36,7 +36,7 @@ jobs:
|
||||
- goarch: "386"
|
||||
goos: darwin
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
- uses: wangyoucao577/go-release-action@v1
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
name: "release go binaries for multiple os/arch"
|
||||
name: "release go binaries for sendosc"
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -36,7 +36,7 @@ jobs:
|
||||
- goarch: "386"
|
||||
goos: darwin
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
- uses: wangyoucao577/go-release-action@v1
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
@@ -14,10 +15,11 @@ func main() {
|
||||
var Host string
|
||||
var Port int32
|
||||
var Protocol string
|
||||
var Format string
|
||||
|
||||
cmd := &cli.Command{
|
||||
Name: "makeosc",
|
||||
Usage: "make osc bytes",
|
||||
Name: "receiveosc",
|
||||
Usage: "receive OSC messages via UDP or TCP",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "host",
|
||||
@@ -43,13 +45,25 @@ func main() {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "format",
|
||||
Usage: "format for messages to be output in ('json')",
|
||||
Value: "json",
|
||||
Destination: &Format,
|
||||
Validator: func(flag string) error {
|
||||
if flag != "json" {
|
||||
return fmt.Errorf("format must be 'json'")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
netAddress := fmt.Sprintf("%s:%d", Host, Port)
|
||||
if Protocol == "udp" {
|
||||
listenUDP(netAddress)
|
||||
listenUDP(netAddress, Format)
|
||||
} else if Protocol == "tcp" {
|
||||
listenTCP(netAddress)
|
||||
listenTCP(netAddress, Format)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@@ -60,7 +74,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func listenTCP(netAddress string) {
|
||||
func listenTCP(netAddress string, format string) {
|
||||
socket, err := net.Listen("tcp4", netAddress)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@@ -69,15 +83,13 @@ func listenTCP(netAddress string) {
|
||||
|
||||
defer socket.Close()
|
||||
|
||||
fmt.Printf("listening on %s (tcp w/ SLIP)\n", netAddress)
|
||||
|
||||
for {
|
||||
conn, err := socket.Accept()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
go handleConnection(conn)
|
||||
go handleConnection(conn, format)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,18 +139,18 @@ func (s *SLIP) decode(bytes []byte) {
|
||||
|
||||
}
|
||||
|
||||
func handleMessages(slip SLIP) {
|
||||
func handleSLIP(slip SLIP, format string) {
|
||||
for message := range slip.Messages {
|
||||
fmt.Printf("%v\n", message)
|
||||
handleMessage(message, format)
|
||||
}
|
||||
}
|
||||
|
||||
func handleConnection(conn net.Conn) {
|
||||
func handleConnection(conn net.Conn, format string) {
|
||||
slip := SLIP{
|
||||
pendingBytes: []byte{},
|
||||
Messages: make(chan osc.OSCMessage),
|
||||
}
|
||||
go handleMessages(slip)
|
||||
go handleSLIP(slip, format)
|
||||
|
||||
defer conn.Close()
|
||||
buffer := make([]byte, 1024)
|
||||
@@ -154,7 +166,16 @@ func handleConnection(conn net.Conn) {
|
||||
}
|
||||
}
|
||||
|
||||
func listenUDP(netAddress string) {
|
||||
func handleMessage(message osc.OSCMessage, format string) {
|
||||
if format == "json" {
|
||||
jsonData, _ := json.Marshal(message)
|
||||
fmt.Println(string(jsonData))
|
||||
} else {
|
||||
fmt.Printf("%v\n", message)
|
||||
}
|
||||
}
|
||||
|
||||
func listenUDP(netAddress string, format string) {
|
||||
|
||||
s, err := net.ResolveUDPAddr("udp4", netAddress)
|
||||
if err != nil {
|
||||
@@ -168,8 +189,6 @@ func listenUDP(netAddress string) {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("listening on %s (udp)\n", netAddress)
|
||||
|
||||
defer connection.Close()
|
||||
buffer := make([]byte, 1024)
|
||||
|
||||
@@ -185,6 +204,6 @@ func listenUDP(netAddress string) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(oscMessage)
|
||||
handleMessage(oscMessage, format)
|
||||
}
|
||||
}
|
||||
|
||||
+11
-4
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -23,8 +24,8 @@ func main() {
|
||||
var Slip bool
|
||||
|
||||
cmd := &cli.Command{
|
||||
Name: "makeosc",
|
||||
Usage: "make osc bytes",
|
||||
Name: "sendosc",
|
||||
Usage: "send OSC messages via UDP or TCP",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "host",
|
||||
@@ -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.4.1
|
||||
require github.com/urfave/cli/v3 v3.6.2
|
||||
|
||||
@@ -2,9 +2,9 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/urfave/cli/v3 v3.4.1 h1:1M9UOCy5bLmGnuu1yn3t3CB4rG79Rtoxuv1sPhnm6qM=
|
||||
github.com/urfave/cli/v3 v3.4.1/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
|
||||
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.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=
|
||||
|
||||
@@ -26,6 +26,10 @@ func (m *OSCMessage) ToBytes() []byte {
|
||||
}
|
||||
|
||||
func MessageFromBytes(bytes []byte) (OSCMessage, error) {
|
||||
if len(bytes) == 0 {
|
||||
return OSCMessage{}, errors.New("cannot create OSC Message from empty byte array")
|
||||
}
|
||||
|
||||
address, typeAndArgBytes := readOSCString(bytes)
|
||||
|
||||
if address[0] != 47 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -5,18 +5,18 @@ type OSCPacket interface {
|
||||
}
|
||||
|
||||
type OSCBundle struct {
|
||||
Contents []OSCPacket
|
||||
TimeTag OSCTimeTag
|
||||
Contents []OSCPacket `json:"contents"`
|
||||
TimeTag OSCTimeTag `json:"timeTag"`
|
||||
}
|
||||
|
||||
type OSCArg struct {
|
||||
Value any
|
||||
Type string
|
||||
Value any `json:"value"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type OSCMessage struct {
|
||||
Address string
|
||||
Args []OSCArg
|
||||
Address string `json:"address"`
|
||||
Args []OSCArg `json:"args"`
|
||||
}
|
||||
|
||||
type OSCColor struct {
|
||||
|
||||
Reference in New Issue
Block a user