-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathprotocol.go
248 lines (227 loc) · 5.17 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package gop2p
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"log"
"net"
"net/http"
"sync/atomic"
)
/*
Protocol message ID
*/
const (
// Client to server
MSGID_REG = uint16(1)
MSGID_LIST = uint16(101)
MSGID_CONNECT = uint16(102)
MSGID_KEEPALIVE = uint16(103)
MSGID_UNREG = uint16(104)
// Server to client
MSGID_TO_CONNECT = uint16(201)
// Client to client
MSGID_TRY_CONNECT = uint16(301)
MSGID_DATA = uint16(302)
)
/*
Error codes
*/
const (
// 2XX
ERROR_CODE_SUCCESS = http.StatusOK
// 4XX
ERROR_CODE_BAD_REQUEST = http.StatusBadRequest
ERROR_CODE_NOT_FOUND = http.StatusNotFound
ERROR_CODE_RETRY = 449
// 5XX
ERROR_CODE_FATAL_ERROR = http.StatusInternalServerError
)
/*
Register request
*/
type RegRequest struct {
ID string `json:"id"`
}
/*
Connect request
*/
type ConnectRequest struct {
PeerID string `json:"peerid"`
}
/*
ToConnect request
*/
type ToConnectRequest struct {
ID string `json:"id"`
Address string `json:"address"`
}
/*
Unregister request
*/
type UnregRequest struct {
ID string `json:"id"`
}
/*
TryConnect request
*/
type TryConnectRequest struct {
ID string `json:"id"`
}
///////////////////////////////////////////////////////////////
// Response
/*
Common response
*/
type CommonResponse struct {
Ecode int `json:"ecode"`
}
/*
List response
*/
type ListResponse struct {
Ecode int `json:"ecode"`
Clients map[string]string `json:"clients"`
}
/*
Connect response
*/
type ConnectResponse struct {
Ecode int `json:"ecode"`
PeerID string `json:"peerid"`
Addr string `json:"addr"`
}
var (
COMMON_RESP_SUCCESS = []byte(`{"ecode":200}`)
COMMON_RESP_FAILED = []byte(`{"ecode":400}`)
COMMON_RESP_NOT_FOUND = []byte(`{"ecode":404}`)
COMMON_RESP_RETRY = []byte(`{"ecode":449}`)
)
///////////////////////////////////////////////////////////////
// Package
const (
PACKAGE_HEAD_TAIL uint16 = 0XA9E3
PACKAGE_HEAD_LEN uint16 = 8
PACKAGE_MAX_LENGTH int = 65535
)
var seqId uint32
/*
Package to send
*/
type Package struct {
SeqID uint32
MsgID uint16
DataSize uint16
Data []byte
IsResponse bool
Addr net.Addr
}
/*
Parse data into Package object
*/
func ParsePackage(data []byte, addr net.Addr) (pack *Package, err error) {
pack = &Package{
Addr: addr,
}
buf := bytes.NewBuffer(data)
err = binary.Read(buf, binary.LittleEndian, &(pack.SeqID))
if err != nil {
return nil, err
}
err = binary.Read(buf, binary.LittleEndian, &(pack.MsgID))
if err != nil {
return nil, err
}
if pack.MsgID&uint16(0X8000) != 0 {
pack.MsgID = pack.MsgID & uint16(0X7FFF)
pack.IsResponse = true
}
err = binary.Read(buf, binary.LittleEndian, &(pack.DataSize))
if err != nil {
return nil, err
}
if len(data) < (int)(pack.DataSize+PACKAGE_HEAD_LEN) {
return nil, errors.New(fmt.Sprintf("Package size unmatch: len(data): %d, DataSize: %d\n", len(data), pack.DataSize))
}
pack.Data = make([]byte, pack.DataSize)
copy(pack.Data, data[PACKAGE_HEAD_LEN:])
if pack.DataSize > 0 {
if pack.IsResponse {
log.Printf("<<< Response (%s) {MsgID:%d, SeqID:%d, Data:%s}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID, string(pack.Data))
} else {
log.Printf("<<< Request (%s) {MsgID:%d, SeqID:%d, Data:%s}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID, string(pack.Data))
}
} else {
if pack.IsResponse {
log.Printf("<<< Response (%s) {MsgID:%d, SeqID:%d, Data:nil}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID)
} else {
log.Printf("<<< Request (%s) {MsgID:%d, SeqID:%d, Data:nil}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID)
}
}
return
}
/*
Get next sequence number
*/
func NextSeqID() (id uint32) {
for id == 0 {
id = atomic.AddUint32(&seqId, 1)
}
return id
}
/*
Send package
*/
func (pack *Package) Send(conn *net.UDPConn) (n int, err error) {
pack.DataSize = uint16(len(pack.Data))
buf := new(bytes.Buffer)
err = binary.Write(buf, binary.LittleEndian, pack.SeqID)
if err != nil {
return
}
if pack.IsResponse {
t := uint16(uint16(0X8000) | pack.MsgID)
err = binary.Write(buf, binary.LittleEndian, t)
if err != nil {
return
}
} else {
err = binary.Write(buf, binary.LittleEndian, pack.MsgID)
if err != nil {
return
}
}
err = binary.Write(buf, binary.LittleEndian, pack.DataSize)
if err != nil {
return
}
if pack.DataSize > 0 {
if pack.IsResponse {
log.Printf(">>> Response (%s): {MsgID:%d, SeqID:%d, Data:%s}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID, string(pack.Data))
} else {
log.Printf(">>> Request (%s): {MsgID:%d, SeqID:%d, Data:%s}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID, string(pack.Data))
}
n, err = buf.Write(pack.Data)
if err != nil {
return
}
if n != int(pack.DataSize) {
return 0, errors.New("Write buffer error")
}
} else {
if pack.IsResponse {
log.Printf(">>> Response (%s): {MsgID:%d, SeqID:%d, Data:nil}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID)
} else {
log.Printf(">>> Request (%s): {MsgID:%d, SeqID:%d, Data:nil}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID)
}
}
n, err = conn.WriteTo(buf.Bytes(), pack.Addr)
if err != nil {
return
}
if n != int(pack.DataSize+PACKAGE_HEAD_LEN) {
return n, errors.New("Send failed!")
}
return
}