-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclientcodec.go
170 lines (147 loc) · 4.19 KB
/
clientcodec.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
package sigsci
import (
"fmt"
"io"
"net"
"net/rpc"
"github.com/tinylib/msgp/msgp"
)
// Adaptors for golang's RPC mechanism using MSGPACK
//
// * http://msgpack.org
// * https://golang.org/pkg/net/rpc/
//
// defines the MSGPACK RPC format
type msgpClientCodec struct {
dec *msgp.Reader
enc *msgp.Writer
c io.Closer
}
// NewMsgpClientCodec creates a new rpc.ClientCodec from an existing connection
func NewMsgpClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec {
return msgpClientCodec{
dec: msgp.NewReader(conn),
enc: msgp.NewWriter(conn),
c: conn,
}
}
func (c msgpClientCodec) Close() error {
return c.c.Close()
}
func (c msgpClientCodec) WriteRequest(r *rpc.Request, x interface{}) error {
if err := c.enc.WriteArrayHeader(4); err != nil {
return fmt.Errorf("WriteRequest failed in writing array header: %s", err)
}
if err := c.enc.WriteInt(0); err != nil {
return fmt.Errorf("WriteRequest failed in requiting rpc msg type 0: %s", err)
}
if err := c.enc.WriteUint32(uint32(r.Seq)); err != nil {
return fmt.Errorf("WriteRequest failed in writing sequence id: %s", err)
}
if err := c.enc.WriteString(r.ServiceMethod); err != nil {
return fmt.Errorf("WriteRequest failed in writing service method: %s", err)
}
if err := c.enc.WriteArrayHeader(1); err != nil {
return fmt.Errorf("WriteRequest failed in writing arg array header: %s", err)
}
if err := c.enc.WriteIntf(x); err != nil {
return fmt.Errorf("WriteRequest failed in writing %T: %s", x, err)
}
if err := c.enc.Flush(); err != nil {
return fmt.Errorf("WriteRequest failed in flushing: %s", err)
}
return nil
}
func (c msgpClientCodec) ReadResponseHeader(r *rpc.Response) error {
sz, err := c.dec.ReadArrayHeader()
if err != nil || sz != 4 {
if cerr := knownError(err); cerr != nil {
return cerr
}
if err == nil && sz != 4 {
err = fmt.Errorf("invalid array size %d", sz)
}
return fmt.Errorf("ReadResponseHeader failed in initial array: %s", err)
}
msgtype, err := c.dec.ReadUint()
if err != nil || msgtype != 1 {
if cerr := knownError(err); cerr != nil {
return cerr
}
if err == nil && msgtype != 1 {
err = fmt.Errorf("invalid message type %d", msgtype)
}
return fmt.Errorf("ReadResponseHeader failed in message type: %s", err)
}
seqID, err := c.dec.ReadUint()
if err != nil {
if cerr := knownError(err); cerr != nil {
return cerr
}
return fmt.Errorf("ReadResponseHeader failed in error type: %s", err)
}
r.Seq = uint64(seqID)
err = c.dec.ReadNil()
if err != nil {
if cerr := knownError(err); cerr != nil {
return cerr
}
// if there is an error maybe its not nil
// try to read string. if still an error
// then assume its bad response
rawerr, err := c.dec.ReadString()
if err != nil {
if cerr := knownError(err); cerr != nil {
return cerr
}
return fmt.Errorf("ReadResponseHeader failed in error message: %s", err)
}
return fmt.Errorf("remote error: %s", rawerr)
}
return nil
}
func (c msgpClientCodec) ReadResponseBody(x interface{}) error {
if x == nil {
return nil
}
// if its a decode-able object, then sort it out.
if obj, ok := x.(msgp.Decodable); ok {
if err := obj.DecodeMsg(c.dec); err != nil {
if cerr := knownError(err); cerr != nil {
return cerr
}
return fmt.Errorf("ReadResponseBody failed in obj decode: %s", err)
}
return nil
}
// we use a plain "int" for response codes just hardwired this
// case. in future use an object to simplify this.
//
if xint, ok := x.(*int); ok {
val, err := c.dec.ReadInt()
if err != nil {
if cerr := knownError(err); cerr != nil {
return cerr
}
return fmt.Errorf("ReadResponseBody failed in int decode: %s", err)
}
*xint = val
return nil
}
return fmt.Errorf("ReadResponseBody failed: unable to decode")
}
// knownError checks the error against known errors, does any fixups and returns an error to indicate it is a known error that should not be handled further as well as the replaced error. A nil error is returned if the original error should be handled further.
func knownError(err error) error {
if err == nil {
return nil
}
if err == io.EOF {
return err
}
if nerr, ok := err.(net.Error); ok {
if nerr.Timeout() {
return nerr
}
}
return nil
}