-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
90 lines (78 loc) · 2.09 KB
/
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
package codec
import (
"bufio"
"fmt"
"io"
"net/rpc"
"sync"
"github.com/golang/protobuf/proto"
"github.com/mars9/codec/wirepb"
)
type clientCodec struct {
mu sync.Mutex // exclusive writer lock
req wirepb.RequestHeader
enc *Encoder
w *bufio.Writer
resp wirepb.ResponseHeader
dec *Decoder
c io.Closer
}
// NewClientCodec returns a new rpc.Client.
//
// A 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.
func NewClientCodec(rwc io.ReadWriteCloser) rpc.ClientCodec {
w := bufio.NewWriterSize(rwc, defaultBufferSize)
r := bufio.NewReaderSize(rwc, defaultBufferSize)
return &clientCodec{
enc: NewEncoder(w),
w: w,
dec: NewDecoder(r),
c: rwc,
}
}
func (c *clientCodec) WriteRequest(req *rpc.Request, body interface{}) error {
c.mu.Lock()
c.req.Method = req.ServiceMethod
c.req.Seq = req.Seq
err := encode(c.enc, &c.req)
if err != nil {
c.mu.Unlock()
return err
}
if err = encode(c.enc, body); err != nil {
c.mu.Unlock()
return err
}
err = c.w.Flush()
c.mu.Unlock()
return err
}
func (c *clientCodec) ReadResponseHeader(resp *rpc.Response) error {
c.resp.Reset()
if err := c.dec.Decode(&c.resp); err != nil {
return err
}
resp.ServiceMethod = c.resp.Method
resp.Seq = c.resp.Seq
resp.Error = c.resp.Error
return nil
}
func (c *clientCodec) ReadResponseBody(body interface{}) (err error) {
if pb, ok := body.(proto.Message); ok {
return c.dec.Decode(pb)
}
return fmt.Errorf("%T does not implement proto.Message", body)
}
func encode(enc *Encoder, m interface{}) (err error) {
if pb, ok := m.(proto.Message); ok {
return enc.Encode(pb)
}
return fmt.Errorf("%T does not implement proto.Message", m)
}
func (c *clientCodec) Close() error { return c.c.Close() }