mirror of
https://github.com/jwetzell/osc-go.git
synced 2026-09-08 15:09:00 +00:00
add support for SLIP and bundles
This commit is contained in:
@@ -16,26 +16,27 @@ func main() {
|
|||||||
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: &Host,
|
||||||
},
|
},
|
||||||
&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", Host, 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,7 +85,7 @@ 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)
|
fmt.Println(err)
|
||||||
@@ -89,13 +100,13 @@ func listenTCP(netAddress string, format string) {
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
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,26 @@ 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)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Received unknown OSC Packet type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,6 +199,12 @@ 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)
|
||||||
@@ -199,11 +229,13 @@ func listenUDP(netAddress string, format string) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
oscMessage, err := osc.MessageFromBytes(buffer[0:bytesRead])
|
fmt.Println("Received UDP packet")
|
||||||
|
fmt.Println(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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user