forked from funny/link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
182 lines (163 loc) · 4.18 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
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
package link
import (
"encoding/binary"
"io"
)
var (
BigEndian = ByteOrder(binary.BigEndian)
LittleEndian = ByteOrder(binary.LittleEndian)
packet1BE = newSimpleProtocol(1, BigEndian)
packet1LE = newSimpleProtocol(1, LittleEndian)
packet2BE = newSimpleProtocol(2, BigEndian)
packet2LE = newSimpleProtocol(2, LittleEndian)
packet4BE = newSimpleProtocol(4, BigEndian)
packet4LE = newSimpleProtocol(4, LittleEndian)
packet8BE = newSimpleProtocol(8, BigEndian)
packet8LE = newSimpleProtocol(8, LittleEndian)
)
type ByteOrder binary.ByteOrder
type ProtocolSide int
const (
SERVER_SIDE ProtocolSide = 1
CLIENT_SIDE ProtocolSide = 2
)
// Packet protocol.
type Protocol interface {
// Create protocol state.
// New(net.Conn) for session protocol state.
// New(*Server) for server protocol state.
// New(*Channel) for channel protocol state.
// If the protocol need handshake for connection initialization.
// Do it in Protocol.New() and returns nil and a error when handshake failed.
New(interface{}, ProtocolSide) (ProtocolState, error)
}
// Protocol state.
type ProtocolState interface {
// Packet a message.
PrepareOutBuffer(buffer *OutBuffer, size int)
// Write a packet.
Write(writer io.Writer, buffer *OutBuffer) error
// Read a packet.
Read(reader io.Reader, buffer *InBuffer) error
}
// Create a {packet, N} protocol.
// The n means how many bytes of the packet header.
// n must is 1、2、4 or 8.
func PacketN(n int, byteOrder ByteOrder) Protocol {
switch n {
case 1:
switch byteOrder {
case BigEndian:
return packet1BE
case LittleEndian:
return packet1LE
}
case 2:
switch byteOrder {
case BigEndian:
return packet2BE
case LittleEndian:
return packet2LE
}
case 4:
switch byteOrder {
case BigEndian:
return packet4BE
case LittleEndian:
return packet4LE
}
case 8:
switch byteOrder {
case BigEndian:
return packet8BE
case LittleEndian:
return packet8LE
}
}
panic("unsupported packet head size")
}
// The packet spliting protocol like Erlang's {packet, N}.
// Each packet has a fix length packet header to present packet length.
type simpleProtocol struct {
n int
bo binary.ByteOrder
encodeHead func([]byte)
decodeHead func([]byte) int
MaxPacketSize int
}
func newSimpleProtocol(n int, byteOrder binary.ByteOrder) *simpleProtocol {
protocol := &simpleProtocol{
n: n,
bo: byteOrder,
}
switch n {
case 1:
protocol.encodeHead = func(buffer []byte) {
buffer[0] = byte(len(buffer) - n)
}
protocol.decodeHead = func(buffer []byte) int {
return int(buffer[0])
}
case 2:
protocol.encodeHead = func(buffer []byte) {
byteOrder.PutUint16(buffer, uint16(len(buffer)-n))
}
protocol.decodeHead = func(buffer []byte) int {
return int(byteOrder.Uint16(buffer))
}
case 4:
protocol.encodeHead = func(buffer []byte) {
byteOrder.PutUint32(buffer, uint32(len(buffer)-n))
}
protocol.decodeHead = func(buffer []byte) int {
return int(byteOrder.Uint32(buffer))
}
case 8:
protocol.encodeHead = func(buffer []byte) {
byteOrder.PutUint64(buffer, uint64(len(buffer)-n))
}
protocol.decodeHead = func(buffer []byte) int {
return int(byteOrder.Uint64(buffer))
}
default:
panic("unsupported packet head size")
}
return protocol
}
func (p *simpleProtocol) New(v interface{}, _ ProtocolSide) (ProtocolState, error) {
return p, nil
}
func (p *simpleProtocol) PrepareOutBuffer(buffer *OutBuffer, size int) {
buffer.Prepare(size)
buffer.Data = buffer.Data[:p.n]
}
func (p *simpleProtocol) Write(writer io.Writer, packet *OutBuffer) error {
if p.MaxPacketSize > 0 && len(packet.Data) > p.MaxPacketSize {
return PacketTooLargeError
}
p.encodeHead(packet.Data)
if _, err := writer.Write(packet.Data); err != nil {
return err
}
return nil
}
func (p *simpleProtocol) Read(reader io.Reader, buffer *InBuffer) error {
// head
buffer.Prepare(p.n)
if _, err := io.ReadFull(reader, buffer.Data); err != nil {
return err
}
size := p.decodeHead(buffer.Data)
if p.MaxPacketSize > 0 && size > p.MaxPacketSize {
return PacketTooLargeError
}
// body
buffer.Prepare(size)
if size == 0 {
return nil
}
if _, err := io.ReadFull(reader, buffer.Data); err != nil {
return err
}
return nil
}