-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathprotocol.go
137 lines (113 loc) · 2.41 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
package main
import (
"bytes"
"io"
"log"
"net"
)
const (
kProtoControlHardResetClientV1 = 1
kProtoControlHardResetServerV1 = 2
kProtoControlSoftResetV1 = 3
kProtoControlV1 = 4
kProtoAckV1 = 5
kProtoDataV1 = 6
kProtoControlHardResetClientV2 = 7
kProtoControlHardResetServerV2 = 8
)
type sessionId [8]byte
type ackArray []uint32
type packet struct {
opCode byte
keyId byte
localSid sessionId
acks ackArray
remoteSid sessionId
id uint32
content []byte
}
func decodeCommonHeader(buf []byte) *packet {
if len(buf) < 2 {
return nil
}
packet := &packet{
opCode: buf[0] >> 3,
keyId: buf[0] & 0x07,
}
packet.content = make([]byte, len(buf)-1)
copy(packet.content, buf[1:])
return packet
}
func sendDataPacket(conn *net.UDPConn, packet *packet) {
buf := &bytes.Buffer{}
// op code and key id
buf.WriteByte((packet.opCode << 3) | (packet.keyId & 0x07))
// content
buf.Write(packet.content)
// sending
_, err := conn.Write(buf.Bytes())
if err != nil {
log.Fatalf("can't send packet to peer: %v", err)
}
}
func encodeCtrlPacket(packet *packet) []byte {
buf := &bytes.Buffer{}
// op code and key id
buf.WriteByte((packet.opCode << 3) | (packet.keyId & 0x07))
// local session id
buf.Write(packet.localSid[:])
// acks
buf.WriteByte(byte(len(packet.acks)))
for i := 0; i < len(packet.acks); i++ {
bufWriteUint32(buf, packet.acks[i])
}
// remote session id
if len(packet.acks) > 0 {
buf.Write(packet.remoteSid[:])
}
// packet id
if packet.opCode != kProtoAckV1 {
bufWriteUint32(buf, packet.id)
}
// content
buf.Write(packet.content)
return buf.Bytes()
}
func decodeCtrlPacket(packet *packet) *packet {
buf := bytes.NewBuffer(packet.content)
// remote session id
_, err := io.ReadFull(buf, packet.localSid[:])
if err != nil {
return nil
}
// ack array
code, err := buf.ReadByte()
if err != nil {
return nil
}
nAcks := int(code)
packet.acks = make([]uint32, nAcks)
for i := 0; i < nAcks; i++ {
packet.acks[i], err = bufReadUint32(buf)
if err != nil {
return nil
}
}
// local session id
if nAcks > 0 {
_, err = io.ReadFull(buf, packet.remoteSid[:])
if err != nil {
return nil
}
}
// packet id
if packet.opCode != kProtoAckV1 {
packet.id, err = bufReadUint32(buf)
if err != nil {
return nil
}
}
// content
packet.content = buf.Bytes()
return packet
}