-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathworker.go
53 lines (48 loc) · 1.11 KB
/
worker.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
package osc
import (
"github.com/pkg/errors"
)
// worker is a worker who can process OSC messages.
type worker struct {
DataChan chan Incoming
Dispatcher Dispatcher
ErrChan chan error
Ready chan<- worker
ExactMatch bool
}
// run runs the worker.
func (w worker) run() {
w.Ready <- w
DataLoop:
for incoming := range w.DataChan {
data := incoming.Data
switch data[0] {
case BundleTag[0]:
bundle, err := ParseBundle(data, incoming.Sender)
if err != nil {
w.ErrChan <- err
}
if err := w.Dispatcher.Dispatch(bundle, w.ExactMatch); err != nil {
w.ErrChan <- errors.Wrap(err, "dispatch bundle")
}
case MessageChar:
msg, err := ParseMessage(data, incoming.Sender)
if err != nil {
w.ErrChan <- err
continue DataLoop
}
if err := ValidateAddress(msg.Address); err != nil {
w.ErrChan <- err
continue DataLoop
}
if err := w.Dispatcher.Invoke(msg, w.ExactMatch); err != nil {
w.ErrChan <- errors.Wrap(err, "dispatch message")
continue DataLoop
}
default:
w.ErrChan <- ErrParse
}
// Announce the worker is ready again.
w.Ready <- w
}
}