-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoder.go
65 lines (55 loc) · 1.34 KB
/
encoder.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
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package rpc
import (
"sync"
)
var encoders = sync.Map{}
func init() {
RegisterHeaderEncoder("json", NewJSONEncoder)
RegisterHeaderEncoder("code", NewCODEEncoder)
RegisterHeaderEncoder("pb", NewPBEncoder)
}
// RegisterHeaderEncoder registers a header Encoder.
func RegisterHeaderEncoder(name string, New func() Encoder) {
encoders.Store(name, New)
}
// NewHeaderEncoder returns a new header Encoder.
func NewHeaderEncoder(name string) func() Encoder {
if c, ok := encoders.Load(name); ok {
return c.(func() Encoder)
}
return nil
}
// DefaultEncoder returns a default header Encoder.
func DefaultEncoder() Encoder {
return NewPBEncoder()
}
// Request defines the interface of request.
type Request interface {
SetSeq(uint64)
GetSeq() uint64
SetUpgrade([]byte)
GetUpgrade() []byte
SetServiceMethod(string)
GetServiceMethod() string
SetArgs([]byte)
GetArgs() []byte
Reset()
}
// Response defines the interface of response.
type Response interface {
SetSeq(uint64)
GetSeq() uint64
SetError(string)
GetError() string
SetReply([]byte)
GetReply() []byte
Reset()
}
// Encoder defines the interface of Encoder.
type Encoder interface {
NewRequest() Request
NewResponse() Response
NewCodec() Codec
}