mirror of
https://github.com/jwetzell/osc-go.git
synced 2026-08-19 21:43:55 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9bc51c787f | |||
| 67a1cc083b |
@@ -30,7 +30,11 @@ 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 := readOSCString(bytes)
|
bundleHeader, bytesAfterBundleHeader, err := 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")
|
||||||
|
|||||||
@@ -229,8 +229,6 @@ func listenUDP(netAddress string, format string) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Received UDP packet")
|
|
||||||
fmt.Println(buffer[0:bytesRead])
|
|
||||||
oscPacket, _, err := osc.PacketFromBytes(buffer[0:bytesRead])
|
oscPacket, _, err := osc.PacketFromBytes(buffer[0:bytesRead])
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+11
-4
@@ -29,11 +29,14 @@ 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 := readOSCString(bytes)
|
address, typeAndArgBytes, err := readOSCString(bytes)
|
||||||
|
|
||||||
if address[0] != 47 {
|
if err != nil {
|
||||||
return nil, errors.New("OSC Message address must start with /")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
oscMessage := OSCMessage{
|
oscMessage := OSCMessage{
|
||||||
@@ -41,7 +44,11 @@ func MessageFromBytes(bytes []byte) (*OSCMessage, error) {
|
|||||||
Args: []OSCArg{},
|
Args: []OSCArg{},
|
||||||
}
|
}
|
||||||
|
|
||||||
typeString, argBytes := readOSCString(typeAndArgBytes)
|
typeString, argBytes, err := 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) {
|
func readOSCString(bytes []byte) (string, []byte, error) {
|
||||||
//TODO(jwetzell): add error handling
|
//TODO(jwetzell): add error handling
|
||||||
oscString := ""
|
oscString := ""
|
||||||
stringEndIndex := 0
|
stringEndIndex := 0
|
||||||
@@ -190,9 +190,13 @@ func readOSCString(bytes []byte) (string, []byte) {
|
|||||||
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
|
return oscString, remainingBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readOSCInt32(bytes []byte) (int32, []byte, error) {
|
func readOSCInt32(bytes []byte) (int32, []byte, error) {
|
||||||
@@ -287,27 +291,30 @@ 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 := readOSCString(bytes)
|
argString, bytesLeft, err := 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, error := readOSCInt32(bytes)
|
argInt, bytesLeft, err := readOSCInt32(bytes)
|
||||||
if error != nil {
|
if err != nil {
|
||||||
readArgError = error
|
readArgError = err
|
||||||
}
|
}
|
||||||
oscArg.Value = argInt
|
oscArg.Value = argInt
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
case "f":
|
case "f":
|
||||||
argFloat, bytesLeft, error := readOSCFloat32(bytes)
|
argFloat, bytesLeft, err := readOSCFloat32(bytes)
|
||||||
if error != nil {
|
if err != nil {
|
||||||
readArgError = error
|
readArgError = err
|
||||||
}
|
}
|
||||||
oscArg.Value = argFloat
|
oscArg.Value = argFloat
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
case "b":
|
case "b":
|
||||||
argBytes, bytesLeft, error := readOSCBlob(bytes)
|
argBytes, bytesLeft, err := readOSCBlob(bytes)
|
||||||
if error != nil {
|
if err != nil {
|
||||||
readArgError = error
|
readArgError = err
|
||||||
}
|
}
|
||||||
oscArg.Value = argBytes
|
oscArg.Value = argBytes
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
@@ -324,23 +331,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, error := readOSCColor(bytes)
|
argColor, bytesLeft, err := readOSCColor(bytes)
|
||||||
if error != nil {
|
if err != nil {
|
||||||
readArgError = error
|
readArgError = err
|
||||||
}
|
}
|
||||||
oscArg.Value = argColor
|
oscArg.Value = argColor
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
case "h":
|
case "h":
|
||||||
argInt, bytesLeft, error := readOSCInt64(bytes)
|
argInt, bytesLeft, err := readOSCInt64(bytes)
|
||||||
if error != nil {
|
if err != nil {
|
||||||
readArgError = error
|
readArgError = err
|
||||||
}
|
}
|
||||||
oscArg.Value = argInt
|
oscArg.Value = argInt
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
case "d":
|
case "d":
|
||||||
argFloat, bytesLeft, error := readOSCFloat64(bytes)
|
argFloat, bytesLeft, err := readOSCFloat64(bytes)
|
||||||
if error != nil {
|
if err != nil {
|
||||||
readArgError = error
|
readArgError = err
|
||||||
}
|
}
|
||||||
oscArg.Value = argFloat
|
oscArg.Value = argFloat
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
|
|||||||
Reference in New Issue
Block a user