mirror of
https://github.com/jwetzell/osc-go.git
synced 2026-08-19 21:43:55 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a117f668fa |
@@ -30,11 +30,7 @@ func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
|
|||||||
return nil, bytes, errors.New("bundle must start with a #")
|
return nil, bytes, errors.New("bundle must start with a #")
|
||||||
}
|
}
|
||||||
|
|
||||||
bundleHeader, bytesAfterBundleHeader, err := readOSCString(bytes)
|
bundleHeader, bytesAfterBundleHeader := readOSCString(bytes)
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, bytes, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if bundleHeader != "#bundle" {
|
if bundleHeader != "#bundle" {
|
||||||
return nil, bytesAfterBundleHeader, errors.New("bundle must start with #bundle string")
|
return nil, bytesAfterBundleHeader, errors.New("bundle must start with #bundle string")
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ func main() {
|
|||||||
func listenTCP(netAddress string, useSLIP bool, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ func listenTCP(netAddress string, useSLIP bool, 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 handleTCPConnection(conn, useSLIP, format)
|
go handleTCPConnection(conn, useSLIP, format)
|
||||||
@@ -185,9 +185,8 @@ func handlePacket(message osc.OSCPacket, format string) {
|
|||||||
handleBundle(bundle, format)
|
handleBundle(bundle, format)
|
||||||
} else if msg, ok := message.(*osc.OSCMessage); ok {
|
} else if msg, ok := message.(*osc.OSCMessage); ok {
|
||||||
handleMessage(msg, format)
|
handleMessage(msg, format)
|
||||||
} else {
|
|
||||||
fmt.Println("Received unknown OSC Packet type")
|
|
||||||
}
|
}
|
||||||
|
// TODO(jwetzell): handle other packet types?
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleMessage(message *osc.OSCMessage, format string) {
|
func handleMessage(message *osc.OSCMessage, format string) {
|
||||||
@@ -209,13 +208,13 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-11
@@ -29,14 +29,11 @@ func MessageFromBytes(bytes []byte) (*OSCMessage, error) {
|
|||||||
if len(bytes) == 0 {
|
if len(bytes) == 0 {
|
||||||
return nil, errors.New("cannot create OSC Message from empty byte array")
|
return nil, errors.New("cannot create OSC Message from empty byte array")
|
||||||
}
|
}
|
||||||
if bytes[0] != 47 {
|
|
||||||
return nil, errors.New("OSC Message must start with /")
|
|
||||||
}
|
|
||||||
|
|
||||||
address, typeAndArgBytes, err := readOSCString(bytes)
|
address, typeAndArgBytes := readOSCString(bytes)
|
||||||
|
|
||||||
if err != nil {
|
if address[0] != 47 {
|
||||||
return nil, err
|
return nil, errors.New("OSC Message address must start with /")
|
||||||
}
|
}
|
||||||
|
|
||||||
oscMessage := OSCMessage{
|
oscMessage := OSCMessage{
|
||||||
@@ -44,11 +41,7 @@ func MessageFromBytes(bytes []byte) (*OSCMessage, error) {
|
|||||||
Args: []OSCArg{},
|
Args: []OSCArg{},
|
||||||
}
|
}
|
||||||
|
|
||||||
typeString, argBytes, err := readOSCString(typeAndArgBytes)
|
typeString, argBytes := readOSCString(typeAndArgBytes)
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for index, oscType := range typeString {
|
for index, oscType := range typeString {
|
||||||
if index == 0 {
|
if index == 0 {
|
||||||
|
|||||||
@@ -171,7 +171,7 @@ func argsToBuffer(args []OSCArg) []byte {
|
|||||||
return argBuffers
|
return argBuffers
|
||||||
}
|
}
|
||||||
|
|
||||||
func readOSCString(bytes []byte) (string, []byte, error) {
|
func readOSCString(bytes []byte) (string, []byte) {
|
||||||
//TODO(jwetzell): add error handling
|
//TODO(jwetzell): add error handling
|
||||||
oscString := ""
|
oscString := ""
|
||||||
stringEndIndex := 0
|
stringEndIndex := 0
|
||||||
@@ -190,13 +190,9 @@ func readOSCString(bytes []byte) (string, []byte, error) {
|
|||||||
stringEndIndex = stringEndIndex + stringPadding
|
stringEndIndex = stringEndIndex + stringPadding
|
||||||
}
|
}
|
||||||
|
|
||||||
if stringEndIndex > len(bytes) {
|
|
||||||
return "", bytes, errors.New("string data is not properly padded")
|
|
||||||
}
|
|
||||||
|
|
||||||
remainingBytes := bytes[stringEndIndex:]
|
remainingBytes := bytes[stringEndIndex:]
|
||||||
|
|
||||||
return oscString, remainingBytes, nil
|
return oscString, remainingBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
func readOSCInt32(bytes []byte) (int32, []byte, error) {
|
func readOSCInt32(bytes []byte) (int32, []byte, error) {
|
||||||
@@ -291,30 +287,27 @@ func readOSCArg(bytes []byte, oscType string) (OSCArg, []byte, error) {
|
|||||||
//TODO(jwetzell): add error handling
|
//TODO(jwetzell): add error handling
|
||||||
switch oscType {
|
switch oscType {
|
||||||
case "s":
|
case "s":
|
||||||
argString, bytesLeft, err := readOSCString(bytes)
|
argString, bytesLeft := readOSCString(bytes)
|
||||||
if err != nil {
|
|
||||||
return OSCArg{}, bytes, err
|
|
||||||
}
|
|
||||||
oscArg.Value = argString
|
oscArg.Value = argString
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
case "i":
|
case "i":
|
||||||
argInt, bytesLeft, err := readOSCInt32(bytes)
|
argInt, bytesLeft, error := readOSCInt32(bytes)
|
||||||
if err != nil {
|
if error != nil {
|
||||||
readArgError = err
|
readArgError = error
|
||||||
}
|
}
|
||||||
oscArg.Value = argInt
|
oscArg.Value = argInt
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
case "f":
|
case "f":
|
||||||
argFloat, bytesLeft, err := readOSCFloat32(bytes)
|
argFloat, bytesLeft, error := readOSCFloat32(bytes)
|
||||||
if err != nil {
|
if error != nil {
|
||||||
readArgError = err
|
readArgError = error
|
||||||
}
|
}
|
||||||
oscArg.Value = argFloat
|
oscArg.Value = argFloat
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
case "b":
|
case "b":
|
||||||
argBytes, bytesLeft, err := readOSCBlob(bytes)
|
argBytes, bytesLeft, error := readOSCBlob(bytes)
|
||||||
if err != nil {
|
if error != nil {
|
||||||
readArgError = err
|
readArgError = error
|
||||||
}
|
}
|
||||||
oscArg.Value = argBytes
|
oscArg.Value = argBytes
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
@@ -331,23 +324,23 @@ func readOSCArg(bytes []byte, oscType string) (OSCArg, []byte, error) {
|
|||||||
oscArg.Value = math.MaxInt32
|
oscArg.Value = math.MaxInt32
|
||||||
remainingBytes = bytes
|
remainingBytes = bytes
|
||||||
case "r":
|
case "r":
|
||||||
argColor, bytesLeft, err := readOSCColor(bytes)
|
argColor, bytesLeft, error := readOSCColor(bytes)
|
||||||
if err != nil {
|
if error != nil {
|
||||||
readArgError = err
|
readArgError = error
|
||||||
}
|
}
|
||||||
oscArg.Value = argColor
|
oscArg.Value = argColor
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
case "h":
|
case "h":
|
||||||
argInt, bytesLeft, err := readOSCInt64(bytes)
|
argInt, bytesLeft, error := readOSCInt64(bytes)
|
||||||
if err != nil {
|
if error != nil {
|
||||||
readArgError = err
|
readArgError = error
|
||||||
}
|
}
|
||||||
oscArg.Value = argInt
|
oscArg.Value = argInt
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
case "d":
|
case "d":
|
||||||
argFloat, bytesLeft, err := readOSCFloat64(bytes)
|
argFloat, bytesLeft, error := readOSCFloat64(bytes)
|
||||||
if err != nil {
|
if error != nil {
|
||||||
readArgError = err
|
readArgError = error
|
||||||
}
|
}
|
||||||
oscArg.Value = argFloat
|
oscArg.Value = argFloat
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
|
|||||||
Reference in New Issue
Block a user