-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemoProtocol.go
77 lines (61 loc) · 1.51 KB
/
demoProtocol.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
package ymtcp
import (
"encoding/binary"
"fmt"
"io"
"log"
"net"
"runtime"
)
const (
HeadLength = 4
MaxBodyLength = 1024
)
type EchoProtocolImpl struct {
}
func MakePacket(bytes []byte) ([]byte, error) {
bodyLength := len(bytes)
if bodyLength > MaxBodyLength {
err := fmt.Errorf("packet's size %d is too large than %d", bodyLength, MaxBodyLength)
return nil, err
}
buff := make([]byte, HeadLength+len(bytes))
binary.BigEndian.PutUint32(buff[0:HeadLength], uint32(len(bytes)))
copy(buff[HeadLength:], bytes)
return buff, nil
}
func (ep *EchoProtocolImpl) ReadBytes(conn *net.TCPConn) ([]byte, error) {
defer func() {
if x := recover(); x != nil {
var st = func(all bool) string {
// Reserve 1K buffer at first
buf := make([]byte, 512)
for {
size := runtime.Stack(buf, all)
// The size of the buffer may be not enough to hold the stacktrace,
// so double the buffer size
if size == len(buf) {
buf = make([]byte, len(buf)<<1)
continue
}
break
}
return string(buf)
}
log.Println(st(false))
}
}()
lengthBytes := make([]byte, HeadLength)
if _, err := io.ReadFull(conn, lengthBytes); err != nil {
return nil, err
}
var length int
if length = int(binary.BigEndian.Uint32(lengthBytes)); length > MaxBodyLength {
return nil, fmt.Errorf("the size of packet %d is larger than the limit %d", length, MaxBodyLength)
}
buff := make([]byte, length)
if _, err := io.ReadFull(conn, buff); err != nil {
return nil, err
}
return buff, nil
}