-
Notifications
You must be signed in to change notification settings - Fork 14
/
connection.go
49 lines (43 loc) · 1.05 KB
/
connection.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
package spotcontrol
import (
"encoding/binary"
"io"
)
type plainConnection struct {
writer io.Writer
reader io.Reader
}
func makePacketPrefix(prefix []byte, data []byte) []byte {
size := len(prefix) + 4 + len(data)
buf := make([]byte, 0, size)
buf = append(buf, prefix...)
sizeBuf := make([]byte, 4)
binary.BigEndian.PutUint32(sizeBuf, uint32(size))
buf = append(buf, sizeBuf...)
return append(buf, data...)
}
func makePlainConnection(reader io.Reader, writer io.Writer) plainConnection {
return plainConnection{
reader: reader,
writer: writer,
}
}
func (p *plainConnection) SendPrefixPacket(prefix []byte, data []byte) (packet []byte, err error) {
packet = makePacketPrefix(prefix, data)
_, err = p.writer.Write(packet)
return
}
func (p *plainConnection) RecvPacket() (buf []byte, err error) {
var size uint32
err = binary.Read(p.reader, binary.BigEndian, &size)
if err != nil {
return
}
buf = make([]byte, size)
binary.BigEndian.PutUint32(buf, size)
_, err = io.ReadFull(p.reader, buf[4:])
if err != nil {
return
}
return buf, nil
}