-
Notifications
You must be signed in to change notification settings - Fork 16
/
protocol.go
58 lines (48 loc) · 1.3 KB
/
protocol.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
package tcp
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
type Protocol interface {
ReadPacket(reader io.Reader) (Packet, error)
WritePacket(writer io.Writer, msg Packet) error
}
const headLength = 4
type DefaultProtocol struct {
maxPacketSize uint32
}
func (p *DefaultProtocol) ReadPacket(reader io.Reader) (Packet, error) {
return p.ReadPacketLimit(reader, p.maxPacketSize)
}
func (*DefaultProtocol) ReadPacketLimit(reader io.Reader, size uint32) (Packet, error) {
head := make([]byte, headLength)
_, err := io.ReadFull(reader, head)
if err != nil {
return nil, err
}
packetLength := binary.BigEndian.Uint32(head)
if size != 0 && packetLength > size {
return nil, fmt.Errorf("packet too large(%v > %v)", packetLength, size)
}
buf := make([]byte, packetLength)
_, err = io.ReadFull(reader, buf)
if err != nil {
return nil, err
}
return NewDefaultPacket(PacketType(buf[0]), buf[1:]), nil
}
func (*DefaultProtocol) WritePacket(writer io.Writer, p Packet) error {
var buf bytes.Buffer
head := make([]byte, 4)
data := p.Bytes()
binary.BigEndian.PutUint32(head, uint32(len(data)))
binary.Write(&buf, binary.BigEndian, head)
binary.Write(&buf, binary.BigEndian, data)
_, err := writer.Write(buf.Bytes())
return err
}
func (p *DefaultProtocol) SetMaxPacketSize(n uint32) {
p.maxPacketSize = n
}