-
Notifications
You must be signed in to change notification settings - Fork 1
/
codec_client.go
executable file
·166 lines (155 loc) · 3.73 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
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
// 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 (
"errors"
"github.com/hslam/buffer"
"github.com/hslam/socket"
"io"
"sync/atomic"
)
type clientCodec struct {
headerEncoder Encoder
bodyCodec Codec
writeBufSize int
bufferPool *buffer.Pool
messages socket.Messages
closed uint32
}
// NewClientCodec returns a new ClientCodec.
func NewClientCodec(bodyCodec Codec, headerEncoder Encoder, messages socket.Messages, writeBufSize int) ClientCodec {
if messages == nil {
return nil
}
if headerEncoder != nil {
if bodyCodec == nil {
bodyCodec = headerEncoder.NewCodec()
}
}
if bodyCodec == nil {
return nil
}
if writeBufSize < 1 {
writeBufSize = bufferSize
}
c := &clientCodec{
headerEncoder: headerEncoder,
bodyCodec: bodyCodec,
writeBufSize: writeBufSize,
bufferPool: buffer.AssignPool(writeBufSize),
}
c.messages = messages
if set, ok := c.messages.(socket.BufferedOutput); ok {
set.SetBufferedOutput(writeBufSize)
}
return c
}
//SetBufferSize sets buffer size.
func (c *clientCodec) SetBufferSize(size int) {
if size < 1 {
size = bufferSize
}
c.writeBufSize = size
c.bufferPool = buffer.AssignPool(size)
if set, ok := c.messages.(socket.BufferedOutput); ok {
set.SetBufferedOutput(size)
}
}
// SetDirectIO disables async io.
func (c *clientCodec) SetDirectIO(directIO bool) {
if directIO {
if set, ok := c.messages.(socket.BufferedOutput); ok {
set.SetBufferedOutput(c.writeBufSize)
}
} else {
if set, ok := c.messages.(socket.BufferedOutput); ok {
set.SetBufferedOutput(0)
}
}
}
func (c *clientCodec) Messages() socket.Messages {
return c.messages
}
func (c *clientCodec) WriteRequest(ctx *Context, param interface{}) error {
if atomic.LoadUint32(&c.closed) > 0 {
return io.EOF
}
var args []byte
var data []byte
var err error
var argsBuffer []byte
if ctx.upgrade.NoRequest != noRequest {
argsBuffer = c.bufferPool.GetBuffer(0)
args, err = c.bodyCodec.Marshal(argsBuffer, param)
if err != nil {
return err
}
}
var requestBuffer = c.bufferPool.GetBuffer(0)
if c.headerEncoder != nil {
req := c.headerEncoder.NewRequest()
req.SetSeq(ctx.Seq)
req.SetUpgrade(ctx.Upgrade)
req.SetServiceMethod(ctx.ServiceMethod)
req.SetArgs(args)
codec := c.headerEncoder.NewCodec()
data, err = codec.Marshal(requestBuffer, req)
} else {
req := &pbRequest{}
req.SetSeq(ctx.Seq)
req.SetUpgrade(ctx.Upgrade)
req.SetServiceMethod(ctx.ServiceMethod)
req.SetArgs(args)
size := req.Size()
var buf = checkBuffer(requestBuffer, size)
var n int
n, err = req.MarshalTo(buf)
data = buf[:n]
}
if err == nil {
err = c.messages.WriteMessage(data)
}
if ctx.upgrade.NoRequest != noRequest {
c.bufferPool.PutBuffer(argsBuffer)
}
c.bufferPool.PutBuffer(requestBuffer)
return err
}
func (c *clientCodec) ReadResponseHeader(ctx *Context) error {
if atomic.LoadUint32(&c.closed) > 0 {
return io.EOF
}
var data = ctx.data
var err error
if c.headerEncoder != nil {
res := c.headerEncoder.NewResponse()
res.Reset()
codec := c.headerEncoder.NewCodec()
err = codec.Unmarshal(data, res)
if err == nil {
ctx.Seq = res.GetSeq()
ctx.Error = res.GetError()
ctx.value = res.GetReply()
}
} else {
res := &pbResponse{}
res.Reset()
err = res.Unmarshal(data)
if err == nil {
ctx.Seq = res.GetSeq()
ctx.Error = res.GetError()
ctx.value = res.GetReply()
}
}
return err
}
func (c *clientCodec) ReadResponseBody(reply []byte, x interface{}) error {
if x == nil {
return errors.New("x is nil")
}
return c.bodyCodec.Unmarshal(reply, x)
}
func (c *clientCodec) Close() error {
atomic.StoreUint32(&c.closed, 1)
return c.messages.Close()
}