-
Notifications
You must be signed in to change notification settings - Fork 61
/
framewriter.go
212 lines (170 loc) · 5.13 KB
/
framewriter.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
package qrpc
import (
"encoding/binary"
"errors"
"sync"
)
// frameBytesWriter for writing frame bytes
type frameBytesWriter interface {
getCodec() CompressorCodec
// writeFrameBytes write the frame bytes atomically or error
writeFrameBytes(dfw *defaultFrameWriter) error
}
// defaultFrameWriter is responsible for write frames
// should create one instance per goroutine
type defaultFrameWriter struct {
fbw frameBytesWriter
wbuf []byte
resp *response
checkExist bool
}
// DefaultWBufSize for default wbuf size
var DefaultWBufSize = 1024
const (
headerSize = 16
)
var fwPool = sync.Pool{New: func() interface{} {
return &defaultFrameWriter{wbuf: make([]byte, headerSize, DefaultWBufSize)}
}}
// newFrameWriter creates a FrameWriter instance to write frames
func newFrameWriter(fbw frameBytesWriter) *defaultFrameWriter {
fw := fwPool.Get().(*defaultFrameWriter)
fw.fbw = fbw
fw.checkExist = false
return fw
}
func (dfw *defaultFrameWriter) Finalize() {
dfw.fbw = nil
dfw.resp = nil
fwPool.Put(dfw)
}
// StartWrite Write the FrameHeader.
func (dfw *defaultFrameWriter) StartWrite(requestID uint64, cmd Cmd, flags FrameFlag) {
binary.BigEndian.PutUint64(dfw.wbuf[4:], requestID)
cmdAndFlags := uint32(flags)<<24 + uint32(cmd)&0xffffff
binary.BigEndian.PutUint32(dfw.wbuf[12:], cmdAndFlags)
}
func (dfw *defaultFrameWriter) Cmd() Cmd {
_ = dfw.wbuf[15]
return Cmd(uint32(dfw.wbuf[13])<<16 | uint32(dfw.wbuf[14])<<8 | uint32(dfw.wbuf[15]))
}
func (dfw *defaultFrameWriter) SetCmd(cmd Cmd) {
_ = append(dfw.wbuf[0:13], byte(cmd>>16), byte(cmd>>8), byte(cmd))
}
func (dfw *defaultFrameWriter) RequestID() uint64 {
requestID := binary.BigEndian.Uint64(dfw.wbuf[4:])
return requestID
}
func (dfw *defaultFrameWriter) SetRequestID(requestID uint64) {
binary.BigEndian.PutUint64(dfw.wbuf[4:], requestID)
}
func (dfw *defaultFrameWriter) Flags() FrameFlag {
return FrameFlag(dfw.wbuf[12])
}
func (dfw *defaultFrameWriter) SetFlags(flags FrameFlag) {
_ = append(dfw.wbuf[:12], byte(flags))
}
func (dfw *defaultFrameWriter) GetWbuf() []byte {
return dfw.wbuf
}
func (dfw *defaultFrameWriter) Payload() []byte {
return dfw.wbuf[16:]
}
var (
// ErrNoCodec when no codec available
ErrNoCodec = errors.New("no codec available")
)
// EndWrite finishes write frame
func (dfw *defaultFrameWriter) EndWrite() (err error) {
payloadLength := len(dfw.Payload())
if payloadLength == 0 {
dfw.SetFlags(dfw.Flags().ToNonCodec())
} else if dfw.Flags().IsCodec() {
codec := dfw.fbw.getCodec()
if codec == nil {
err = ErrNoCodec
return
}
var encodedPayload []byte
encodedPayload, err = codec.Encode(dfw.Payload())
if err != nil {
return
}
if len(encodedPayload) > payloadLength {
dfw.SetFlags(dfw.Flags().ToNonCodec())
} else {
dfw.wbuf = dfw.wbuf[:16]
dfw.WriteBytes(encodedPayload)
}
}
err = dfw.endWrite()
return
}
func (dfw *defaultFrameWriter) EndWriteCompressed() (err error) {
dfw.SetFlags(dfw.Flags().ToCodec())
return dfw.endWrite()
}
func (dfw *defaultFrameWriter) endWrite() (err error) {
length := len(dfw.wbuf) - 4
_ = append(dfw.wbuf[:0],
byte(length>>24),
byte(length>>16),
byte(length>>8),
byte(length))
err = dfw.fbw.writeFrameBytes(dfw)
dfw.wbuf = dfw.wbuf[:16]
return
}
func (dfw *defaultFrameWriter) Length() int {
return int(binary.BigEndian.Uint32(dfw.wbuf))
}
func (dfw *defaultFrameWriter) StreamEndWrite(end bool) error {
if end {
dfw.SetFlags(dfw.Flags().ToEndStream())
}
return dfw.EndWrite()
}
func (dfw *defaultFrameWriter) ResetFrame(requestID uint64, reason Cmd) error {
dfw.StartWrite(requestID, reason, StreamRstFlag)
return dfw.EndWrite()
}
// WriteUint64 write uint64 to wbuf
func (dfw *defaultFrameWriter) WriteUint64(v uint64) {
dfw.wbuf = append(dfw.wbuf, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
// WriteUint32 write uint32 to wbuf
func (dfw *defaultFrameWriter) WriteUint32(v uint32) {
dfw.wbuf = append(dfw.wbuf, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
// WriteUint16 write uint16 to wbuf
func (dfw *defaultFrameWriter) WriteUint16(v uint16) {
dfw.wbuf = append(dfw.wbuf, byte(v>>8), byte(v))
}
// WriteUint8 write uint8 to wbuf
func (dfw *defaultFrameWriter) WriteUint8(v uint8) {
dfw.wbuf = append(dfw.wbuf, byte(v))
}
// WriteBytes write multiple bytes
func (dfw *defaultFrameWriter) WriteBytes(v []byte) { dfw.wbuf = append(dfw.wbuf, v...) }
type defaultStreamWriter defaultFrameWriter
func (dsw *defaultStreamWriter) StartWrite(cmd Cmd) {
dfw := (*defaultFrameWriter)(dsw)
dfw.SetCmd(cmd)
}
func (dsw *defaultStreamWriter) RequestID() uint64 {
return (*defaultFrameWriter)(dsw).RequestID()
}
func (dsw *defaultStreamWriter) WriteBytes(v []byte) {
(*defaultFrameWriter)(dsw).WriteBytes(v)
}
func (dsw *defaultStreamWriter) EndWrite(end bool) error {
return (*defaultFrameWriter)(dsw).StreamEndWrite(end)
}
func (dsw *defaultStreamWriter) EndWriteCompressed() (err error) {
return (*defaultFrameWriter)(dsw).EndWriteCompressed()
}
func (dsw *defaultStreamWriter) ResetFrame(reason Cmd) (err error) {
dfw := (*defaultFrameWriter)(dsw)
err = dfw.ResetFrame(dfw.RequestID(), reason)
return
}