-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_v1.go
239 lines (200 loc) · 4.82 KB
/
frame_v1.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
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package frame
import (
"bytes"
"encoding/binary"
"io"
"math"
"github.com/antlabs/wsutil/bytespool"
"github.com/antlabs/wsutil/enum"
"github.com/antlabs/wsutil/errs"
"github.com/antlabs/wsutil/fixedwriter"
"github.com/antlabs/wsutil/mask"
"github.com/antlabs/wsutil/opcode"
)
type FrameHeader struct {
PayloadLen int64
MaskKey uint32
Mask bool
Head byte
Opcode opcode.Opcode
}
func (f *FrameHeader) GetFin() bool {
return f.Head&(1<<7) > 0
}
func (f *FrameHeader) GetRsv1() bool {
return f.Head&(1<<6) > 0
}
func (f *FrameHeader) GetRsv2() bool {
return f.Head&(1<<5) > 0
}
func (f *FrameHeader) GetRsv3() bool {
return f.Head&(1<<4) > 0
}
type Frame struct {
FrameHeader
Payload []byte
}
func ReadHeader(r io.Reader, headArray *[enum.MaxFrameHeaderSize]byte) (h FrameHeader, size int, err error) {
// var headArray [enum.MaxFrameHeaderSize]byte
head := (*headArray)[:2]
n, err := io.ReadFull(r, head)
if err != nil {
return
}
if n != 2 {
err = io.ErrUnexpectedEOF
return
}
size = 2
h.Head = head[0]
// h.Fin = head[0]&(1<<7) > 0
// h.Rsv1 = head[0]&(1<<6) > 0
// h.Rsv2 = head[0]&(1<<5) > 0
// h.Rsv3 = head[0]&(1<<4) > 0
h.Opcode = opcode.Opcode(head[0] & 0xF)
have := 0
h.Mask = head[1]&(1<<7) > 0
if h.Mask {
have += 4
size += 4
}
h.PayloadLen = int64(head[1] & 0x7F)
switch {
// 长度
case h.PayloadLen >= 0 && h.PayloadLen <= 125:
case h.PayloadLen == 126:
// 2字节长度
have += 2
size += 2
case h.PayloadLen == 127:
// 8字节长度
have += 8
size += 8
default:
// 预期之外的, 直接报错
return h, 0, errs.ErrFramePayloadLength
}
head = head[:have]
_, err = io.ReadFull(r, head)
if err != nil {
return
}
switch h.PayloadLen {
case 126:
h.PayloadLen = int64(binary.BigEndian.Uint16(head[:2]))
head = head[2:]
case 127:
h.PayloadLen = int64(binary.BigEndian.Uint64(head[:8]))
head = head[8:]
}
if h.Mask {
h.MaskKey = binary.LittleEndian.Uint32(head[:4])
}
return
}
// https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
// (the most significant bit MUST be 0)
func WriteHeader(head []byte, fin bool, rsv1, rsv2, rsv3 bool, code opcode.Opcode, payloadLen int, mask bool, maskValue uint32) (have int, err error) {
head[0] = 0
head[1] = 0
head[2] = 0
head[3] = 0
head[4] = 0
head[5] = 0
head[6] = 0
head[7] = 0
head[8] = 0
head[9] = 0
head[10] = 0
head[11] = 0
head[12] = 0
head[13] = 0
if fin {
head[0] |= 1 << 7
}
if rsv1 {
head[0] |= 1 << 6
}
if rsv2 {
head[0] |= 1 << 5
}
if rsv3 {
head[0] |= 1 << 5
}
head[0] |= byte(code & 0xF)
have = 2
switch {
case payloadLen <= 125:
head[1] = byte(payloadLen)
case payloadLen <= math.MaxUint16:
head[1] = 126
binary.BigEndian.PutUint16(head[2:], uint16(payloadLen))
have += 2 // 2前
default:
head[1] = 127
binary.BigEndian.PutUint64(head[2:], uint64(payloadLen))
have += 8
}
if mask {
head[1] |= 1 << 7
// have += copy(head[have:], maskValue[:])
binary.LittleEndian.PutUint32(head[have:], maskValue)
have += 4
}
return have, err
}
// fw 是个临时空间,先聚合好数据,再写入 w
func WriteFrame(fw *fixedwriter.FixedWriter, w io.Writer, payload []byte, fin bool, rsv1 bool, isMask bool, code opcode.Opcode, maskValue uint32) (err error) {
buf := bytespool.GetBytes(len(payload) + enum.MaxFrameHeaderSize)
var wIndex int
fw.Reset(*buf)
if wIndex, err = WriteHeader(*buf, fin, rsv1, false, false, code, len(payload), isMask, maskValue); err != nil {
goto free
}
fw.SetW(wIndex)
_, err = fw.Write(payload)
if err != nil {
goto free
}
if isMask {
mask.Mask(fw.Bytes()[wIndex:], maskValue)
}
_, err = w.Write(fw.Bytes())
free:
fw.Free()
bytespool.PutBytes(buf)
return
}
func WriteFrameToBytes(w *bytes.Buffer, payload []byte, fin bool, rsv1 bool, isMask bool, code opcode.Opcode, maskValue uint32) (err error) {
var head [enum.MaxFrameHeaderSize]byte
var wIndex int
if wIndex, err = WriteHeader(head[:], fin, rsv1, false, false, code, len(payload), isMask, maskValue); err != nil {
return err
}
_, err = w.Write(head[:wIndex])
if err != nil {
return err
}
wIndex = w.Len()
_, err = w.Write(payload)
if err != nil {
return err
}
if isMask {
mask.Mask(w.Bytes()[wIndex:], maskValue)
}
return
}