-
Notifications
You must be signed in to change notification settings - Fork 6
/
send.go
69 lines (62 loc) · 925 Bytes
/
send.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
package lifxlan
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"net"
)
func (d *device) Send(
ctx context.Context,
conn net.Conn,
flags AckResFlag,
message MessageType,
payload interface{},
) (seq uint8, err error) {
if ctx.Err() != nil {
err = ctx.Err()
return
}
var msg []byte
seq = d.NextSequence()
buf := new(bytes.Buffer)
if payload != nil {
if err = binary.Write(buf, binary.LittleEndian, payload); err != nil {
return
}
}
msg, err = GenerateMessage(
NotTagged,
d.Source(),
d.Target(),
flags,
seq,
message,
buf.Bytes(),
)
if err != nil {
return
}
if ctx.Err() != nil {
err = ctx.Err()
return
}
var n int
n, err = conn.Write(msg)
if err != nil {
return
}
if n < len(msg) {
err = fmt.Errorf(
"lifxlan.Device.Send: only wrote %d out of %d bytes",
n,
len(msg),
)
return
}
if ctx.Err() != nil {
err = ctx.Err()
return
}
return
}