-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe.go
168 lines (151 loc) · 3.45 KB
/
frame.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
package eventbus
import (
"errors"
"io"
"log"
"net"
)
//--------------------------------------------------
// Variables and Constants
//--------------------------------------------------
var _op_type = []byte{0xF0, 0x00} // 1111 0000 0000 0000
var _op_size = []byte{0x0F, 0xFF} // 0000 1111 1111 1111 (4kb = 4095)
const (
RegReqFrameType = 0x1000 //registration request - 0001 0000 0000 0000
RegRespFrameType = 0x9000 //registration response - 1001 0000 0000 0000
EvtReqFrameType = 0x3000 //event request - 0011 0000 0000 0000
EvtRespFrameType = 0xb000 //event request - 1011 0000 0000 0000
PngReqFrameType = 0x7000 //ping request - 0111 0000 0000 0000
PngRespFrameType = 0xF000 //ping response - 1111 0000 0000 0000
ErrRespFrameType = 0x8000 //error response - 1000 0000 0000 0000
)
//--------------------------------------------------
// Type
//--------------------------------------------------
type Frame struct {
Type int
Size int
Data []byte
}
//--------------------------------------------------
// Private Operations
//--------------------------------------------------
func rtype(b0, b1 byte) int {
p0 := b0 & _op_type[0]
p1 := b1 & _op_type[1]
return int(p0)*256 + int(p1)
}
func rsize(b0, b1 byte) int {
p0 := b0 & _op_size[0]
p1 := b1 & _op_size[1]
return int(p0)*256 + int(p1)
}
func wtypesize(t int, s int) []byte {
b := (s & 0x0FFF) | t
//int to byte array
p0 := (b / 256)
p1 := b - (p0 * 256)
//
pt := make([]byte, 2)
pt[0] = byte(p0)
pt[1] = byte(p1)
//
return pt
}
func _read(r io.Reader, s int) ([]byte, error) {
//slice
result := make([]byte, 0, s)
//count received
count := 0
remaining := s
//
for {
//log
log.Printf("--start the reading of %d bytes", remaining)
//
input := make([]byte, remaining)
c, err := r.Read(input)
if err != nil {
return nil, err
}
//log
log.Printf("--read %d bytes", c)
//
if c == 0 {
return nil, errors.New("Closed connection, EOF stream or badly formatted data")
}
//
result = append(result, input[:c]...)
count += c
remaining = (s - count)
//log
log.Printf("--remaining: %d bytes / count: %d bytes", remaining, count)
//
if remaining == 0 {
//log
log.Printf("--end read")
break
}
}
//result
return result, nil
}
func _readFrame(r io.Reader) (*Frame, error) {
//log
log.Printf("Start reading...")
//read 2 bytes
bs, err := _read(r, 2)
if err != nil {
return nil, err
}
//translate
t := rtype(bs[0], bs[1])
s := rsize(bs[0], bs[1])
//log
log.Printf("Head: t=0x%x; s=%d(bytes)", t, s)
//data
dt, err := _read(r, s)
if err != nil {
return nil, err
}
//log
log.Printf("Body: d=%s", dt)
//create frame
fr := new(Frame)
fr.Type = t
fr.Size = s
fr.Data = dt
//log
log.Printf("Stop reading...")
//result
return fr, nil
}
func _writeFrame(w io.Writer, f *Frame) error {
//log
log.Printf("Start writing...")
//write 2 bytes
bs := wtypesize(f.Type, f.Size)
//log
log.Printf("Head: t=0x%x; s=%d(bytes)", f.Type, f.Size)
//
w.Write(bs)
//log
log.Printf("Body: d=%s", f.Data)
//
w.Write(f.Data)
//log
log.Printf("Stop writing...")
return nil
}
//--------------------------------------------------
// Public Operations
//--------------------------------------------------
func NewFrame(t int, b []byte) *Frame {
return &Frame{Type: t, Size: len(b), Data: b}
}
func ReadFrame(conn net.Conn) (*Frame, error) {
return _readFrame(conn)
}
func WriteFrame(conn net.Conn, frame *Frame) error {
return _writeFrame(conn, frame)
}