-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
80 lines (68 loc) · 1.48 KB
/
client.go
File metadata and controls
80 lines (68 loc) · 1.48 KB
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
package bsonrpc
import (
"io"
"net/rpc"
)
/*
type ClientCodec interface {
WriteRequest(*Request, interface{}) error
ReadResponseHeader(*Response) error
ReadResponseBody(interface{}) error
Close() error
}
*/
type ccodec struct {
conn io.ReadWriteCloser
enc *Encoder
dec *Decoder
}
func NewClientCodec(conn io.ReadWriteCloser) (codec rpc.ClientCodec) {
cc := &ccodec{
conn: conn,
enc: NewEncoder(conn),
dec: NewDecoder(conn),
}
codec = cc
return
}
/*
type Request struct {
ServiceMethod string // format: "Service.Method"
Seq uint64 // sequence number chosen by client
next *Request // for free list in Server
}
type Response struct {
ServiceMethod string // echoes that of the Request
Seq uint64 // echoes that of the request
Error string // error, if any.
next *Response // for free list in Server
}
*/
func (cc *ccodec) WriteRequest(req *rpc.Request, v interface{}) (err error) {
err = cc.enc.Encode(req)
if err != nil {
return
}
err = cc.enc.Encode(v)
if err != nil {
return
}
return
}
func (cc *ccodec) ReadResponseHeader(res *rpc.Response) (err error) {
err = cc.dec.Decode(res)
return
}
func (cc *ccodec) ReadResponseBody(v interface{}) (err error) {
err = cc.dec.Decode(v)
return
}
func (cc *ccodec) Close() (err error) {
err = cc.conn.Close()
return
}
func NewClient(conn io.ReadWriteCloser) (c *rpc.Client) {
cc := NewClientCodec(conn)
c = rpc.NewClientWithCodec(cc)
return
}