-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
212 lines (166 loc) · 4.43 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package osc
import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
"strings"
)
// Message represents a single OSC message. An OSC message consists of an OSC
// address pattern and zero or more arguments.
type Message struct {
Address string
Arguments []interface{}
}
// Verify that Messages implements the Packet interface.
// var _ Packet = (*Message)(nil)
// Append appends the given arguments to the arguments list.
func (msg *Message) Append(args ...interface{}) {
msg.Arguments = append(msg.Arguments, args...)
}
// Equals returns true if the given OSC Message `m` is equal to the current OSC
// Message. It checks if the OSC address and the arguments are equal. Returns
// true if the current object and `m` are equal.
func (msg *Message) Equals(m *Message) bool {
return reflect.DeepEqual(msg, m)
}
// Clear clears the OSC address and all arguments.
func (msg *Message) Clear() {
msg.Address = ""
msg.ClearData()
}
// ClearData removes all arguments from the OSC Message.
func (msg *Message) ClearData() {
msg.Arguments = msg.Arguments[len(msg.Arguments):]
}
// Match returns true, if the OSC address pattern of the OSC Message matches the given
// address. The match is case sensitive!
func (msg *Message) Match(addr string) bool {
return getRegEx(msg.Address).MatchString(addr)
}
// typeTags returns the type tag string.
func (msg *Message) typeTags() string {
if len(msg.Arguments) == 0 {
return ","
}
var tags strings.Builder
_ = tags.WriteByte(',')
for _, m := range msg.Arguments {
tags.WriteByte(getTypeTag(m))
}
return tags.String()
}
// String implements the fmt.Stringer interface.
func (msg *Message) String() string {
if msg == nil {
return ""
}
var s strings.Builder
tags := msg.typeTags()
s.WriteString(fmt.Sprintf("%s %s", msg.Address, tags))
for _, arg := range msg.Arguments {
switch argType := arg.(type) {
case bool, int32, int64, float32, float64, string:
s.WriteString(fmt.Sprintf(" %v", argType))
case nil:
s.WriteString(" Nil")
case []byte:
s.WriteString(fmt.Sprintf(" %s", argType))
case Timetag:
s.WriteString(fmt.Sprintf(" %d", Timetag(argType)))
}
}
return s.String()
}
// MarshalBinary serializes the OSC message to a byte buffer. The byte buffer
// has the following format:
// 1. OSC Address Pattern
// 2. OSC Type Tag String
// 3. OSC Arguments.
func (msg *Message) MarshalBinary() ([]byte, error) {
// We can start with the OSC address and add it to the buffer
data := new(bytes.Buffer)
_, err := writePaddedString(msg.Address, data)
if err != nil {
return nil, err
}
// Type tag string starts with ","
lenArgs := len(msg.Arguments)
typetags := make([]byte, lenArgs+1)
typetags[0] = ','
// Process the type tags and collect all arguments
payload := new(bytes.Buffer)
for i, arg := range msg.Arguments {
switch t := arg.(type) {
case bool:
if t {
typetags[i+1] = 'T'
continue
}
typetags[i+1] = 'F'
case nil:
typetags[i+1] = 'N'
case int32:
typetags[i+1] = 'i'
err = binary.Write(payload, binary.BigEndian, t)
if err != nil {
return nil, err
}
case float32:
typetags[i+1] = 'f'
err := binary.Write(payload, binary.BigEndian, t)
if err != nil {
return nil, err
}
case string:
typetags[i+1] = 's'
_, err = writePaddedString(t, payload)
if err != nil {
return nil, err
}
case []byte:
typetags[i+1] = 'b'
_, err = writeBlob(t, payload)
if err != nil {
return nil, err
}
case int64:
typetags[i+1] = 'h'
err = binary.Write(payload, binary.BigEndian, t)
if err != nil {
return nil, err
}
case float64:
typetags[i+1] = 'd'
err = binary.Write(payload, binary.BigEndian, t)
if err != nil {
return nil, err
}
case Timetag:
typetags[i+1] = 't'
b, err := t.MarshalBinary()
if err != nil {
return nil, err
}
_, err = payload.Write(b)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported type: %T", t)
}
}
// Write the type tag string to the data buffer
if _, err := writePaddedString(string(typetags), data); err != nil {
return nil, err
}
// Write the payload (OSC arguments) to the data buffer
if _, err := data.Write(payload.Bytes()); err != nil {
return nil, err
}
return data.Bytes(), nil
}
// NewMessage returns a new Message. The address parameter is the OSC address.
func NewMessage(addr string, args ...interface{}) *Message {
return &Message{Address: addr, Arguments: args}
}