-
Notifications
You must be signed in to change notification settings - Fork 1
/
codec_client.go
48 lines (39 loc) · 1.06 KB
/
codec_client.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
package codec
import (
"fmt"
"github.com/deepfabric/prophet/pb/rpcpb"
"github.com/fagongzi/goetty/buf"
gcodec "github.com/fagongzi/goetty/codec"
"github.com/fagongzi/goetty/codec/length"
"github.com/fagongzi/util/protoc"
)
var (
cc = &clientCodec{}
)
// NewClientCodec create client side codec
func NewClientCodec(maxBodySize int) (gcodec.Encoder, gcodec.Decoder) {
return length.NewWithSize(cc, cc, 0, 0, 0, maxBodySize)
}
type clientCodec struct {
}
func (c *clientCodec) Decode(in *buf.ByteBuf) (bool, interface{}, error) {
data := in.GetMarkedRemindData()
resp := &rpcpb.Response{}
err := resp.Unmarshal(data)
if err != nil {
return false, nil, err
}
in.MarkedBytesReaded()
return true, resp, nil
}
func (c *clientCodec) Encode(data interface{}, out *buf.ByteBuf) error {
if req, ok := data.(*rpcpb.Request); ok {
index := out.GetWriteIndex()
size := req.Size()
out.Expansion(size)
protoc.MustMarshalTo(req, out.RawBuf()[index:index+size])
out.SetWriterIndex(index + size)
return nil
}
return fmt.Errorf("not support %T %+v", data, data)
}