-
Notifications
You must be signed in to change notification settings - Fork 1
/
codec.go
304 lines (258 loc) · 7.54 KB
/
codec.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) 2019 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
package rpc
import (
"context"
"encoding/json"
"encoding/xml"
"errors"
"github.com/hslam/buffer"
"github.com/hslam/funcs"
"github.com/hslam/socket"
"sync"
)
var codecs = sync.Map{}
func init() {
RegisterCodec("json", NewJSONCodec)
RegisterCodec("code", NewCODECodec)
RegisterCodec("pb", NewPBCodec)
}
// RegisterCodec registers a codec.
func RegisterCodec(name string, New func() Codec) {
codecs.Store(name, New)
}
// NewCodec returns a new Codec.
func NewCodec(name string) func() Codec {
if c, ok := codecs.Load(name); ok {
return c.(func() Codec)
}
return nil
}
// contextKey is a key for use with context.WithValue. It's used as
// a pointer so it fits in an interface{} without allocation.
type contextKey struct {
name string
}
// String returns a context key.
func (k *contextKey) String() string { return "github.com/hslam/rpc context key " + k.name }
// BufferContextKey is a context key.
var BufferContextKey = &contextKey{"buffer"}
const (
bufferSize = 65536
)
func checkBuffer(buf []byte, n int) []byte {
if cap(buf) >= n {
buf = buf[:n]
} else {
buf = make([]byte, n)
}
return buf
}
// GetBuffer gets a buffer from the pool.
func GetBuffer(size int) []byte {
if size > 0 {
return buffer.AssignPool(size).GetBuffer(size)
}
return nil
}
// PutBuffer puts a buffer to the pool.
func PutBuffer(buf []byte) {
size := cap(buf)
if size > 0 {
buffer.AssignPool(size).PutBuffer(buf[:size])
}
}
// BufferSize sets the buffer size.
type BufferSize interface {
SetBufferSize(int)
}
// DirectIO sets the direct IO.
type DirectIO interface {
SetDirectIO(bool)
}
// GetContextBuffer gets a buffer from the context.
func GetContextBuffer(ctx context.Context) (buffer []byte) {
value := ctx.Value(BufferContextKey)
if value != nil {
if b, ok := value.([]byte); ok {
buffer = b
}
}
return
}
// FreeContextBuffer frees the context buffer to the pool.
func FreeContextBuffer(ctx context.Context) {
PutBuffer(GetContextBuffer(ctx))
}
// Context is an RPC context for codec.
type Context struct {
Seq uint64
Upgrade []byte
ServiceMethod string
Error string
buffer []byte
data []byte
upgrade *upgrade
f *funcs.Func
args funcs.Value
reply funcs.Value
sending *sync.Mutex
codec ServerCodec
value []byte
stream *stream
ctx *Context
}
// Reset resets the Context.
func (ctx *Context) Reset() {
*ctx = Context{}
}
// ServerCodec implements reading of RPC requests and writing of
// RPC responses for the server side of an RPC session.
// The server calls ReadRequestHeader and ReadRequestBody in pairs
// to read requests from the connection, and it calls WriteResponse to
// write a response back. The server calls Close when finished with the
// connection. ReadRequestBody may be called with a nil
// argument to force the body of the request to be read and discarded.
type ServerCodec interface {
Messages() socket.Messages
ReadRequestHeader(*Context) error
ReadRequestBody([]byte, interface{}) error
WriteResponse(*Context, interface{}) error
Close() error
}
// ClientCodec implements writing of RPC requests and
// reading of RPC responses for the client side of an RPC session.
// The client calls WriteRequest to write a request to the connection
// and calls ReadResponseHeader and ReadResponseBody in pairs
// to read responses. The client calls Close when finished with the
// connection. ReadResponseBody may be called with a nil
// argument to force the body of the response to be read and then
// discarded.
type ClientCodec interface {
Messages() socket.Messages
WriteRequest(*Context, interface{}) error
ReadResponseHeader(*Context) error
ReadResponseBody([]byte, interface{}) error
Close() error
}
// Codec defines the interface for encoding/decoding.
type Codec interface {
Marshal(buf []byte, v interface{}) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
}
// JSONCodec struct
type JSONCodec struct {
}
// Marshal returns the JSON encoding of v.
func (c *JSONCodec) Marshal(buf []byte, v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
func (c *JSONCodec) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
// XMLCodec struct
type XMLCodec struct {
}
// Marshal returns the XML encoding of v.
func (c *XMLCodec) Marshal(buf []byte, v interface{}) ([]byte, error) {
return xml.Marshal(v)
}
// Unmarshal parses the XML-encoded data and stores the result in the value pointed to by v.
func (c *XMLCodec) Unmarshal(data []byte, v interface{}) error {
return xml.Unmarshal(data, v)
}
// BYTESCodec struct
type BYTESCodec struct {
}
// Marshal returns the BYTES encoding of v.
func (c *BYTESCodec) Marshal(buf []byte, v interface{}) ([]byte, error) {
return *v.(*[]byte), nil
}
// Unmarshal parses the BYTES-encoded data and stores the result in the value pointed to by v.
func (c *BYTESCodec) Unmarshal(data []byte, v interface{}) error {
*v.(*[]byte) = data
return nil
}
// GoGoProtobuf defines the interface for gogo's protobuf.
type GoGoProtobuf interface {
Size() (n int)
Marshal() (data []byte, err error)
MarshalTo(buf []byte) (int, error)
Unmarshal(data []byte) error
}
// GOGOPBCodec struct
type GOGOPBCodec struct {
}
//ErrorGOGOPB is the error that v is not GoGoProtobuf
var ErrorGOGOPB = errors.New("is not GoGoProtobuf")
// Marshal returns the GOGOPB encoding of v.
func (c *GOGOPBCodec) Marshal(buf []byte, v interface{}) ([]byte, error) {
if p, ok := v.(GoGoProtobuf); ok {
size := p.Size()
if cap(buf) >= size {
buf = buf[:size]
n, err := p.MarshalTo(buf)
return buf[:n], err
}
return p.Marshal()
}
return nil, ErrorGOGOPB
}
// Unmarshal parses the GOGOPB-encoded data and stores the result in the value pointed to by v.
func (c *GOGOPBCodec) Unmarshal(data []byte, v interface{}) error {
if p, ok := v.(GoGoProtobuf); ok {
return p.Unmarshal(data)
}
return ErrorGOGOPB
}
// Code defines the interface for code.
type Code interface {
Marshal(buf []byte) ([]byte, error)
Unmarshal(buf []byte) (uint64, error)
}
//ErrorCODE is the error that v is not Code
var ErrorCODE = errors.New("is not Code")
// CODECodec struct
type CODECodec struct {
}
// Marshal returns the CODE encoding of v.
func (c *CODECodec) Marshal(buf []byte, v interface{}) ([]byte, error) {
if p, ok := v.(Code); ok {
return p.Marshal(buf)
}
return nil, ErrorCODE
}
// Unmarshal parses the CODE-encoded data and stores the result in the value pointed to by v.
func (c *CODECodec) Unmarshal(data []byte, v interface{}) error {
if p, ok := v.(Code); ok {
_, err := p.Unmarshal(data)
return err
}
return ErrorCODE
}
// MsgPack defines the interface for msgp.
type MsgPack interface {
MarshalMsg(buf []byte) ([]byte, error)
UnmarshalMsg(bts []byte) (o []byte, err error)
}
//ErrorMSGP is the error that v is not MSGP
var ErrorMSGP = errors.New("is not MSGP")
// MSGPCodec struct
type MSGPCodec struct {
}
// Marshal returns the MSGP encoding of v.
func (c *MSGPCodec) Marshal(buf []byte, v interface{}) ([]byte, error) {
if p, ok := v.(MsgPack); ok {
return p.MarshalMsg(buf)
}
return nil, ErrorMSGP
}
// Unmarshal parses the MSGP-encoded data and stores the result in the value pointed to by v.
func (c *MSGPCodec) Unmarshal(data []byte, v interface{}) error {
if p, ok := v.(MsgPack); ok {
_, err := p.UnmarshalMsg(data)
return err
}
return ErrorMSGP
}