-
Notifications
You must be signed in to change notification settings - Fork 0
/
dummycodec.go
67 lines (56 loc) · 1.13 KB
/
dummycodec.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
package rpc_bench
import (
"net/rpc"
"io"
"encoding/binary"
)
type DummyCodec struct {
rwc io.ReadWriteCloser
b []byte
}
// server interfaces
func (d *DummyCodec) ReadRequestHeader(r *rpc.Request) error {
r.ServiceMethod = "Foo.Dummy"
_, err := d.rwc.Read(d.b)
if err != nil {
return err
}
seq, _ := binary.Uvarint(d.b)
r.Seq = seq
return nil
}
func (d *DummyCodec) ReadRequestBody(body interface{}) error {
return nil
}
func (d *DummyCodec) WriteResponse(r *rpc.Response, body interface{}) (err error) {
binary.PutUvarint(d.b, r.Seq)
_, err = d.rwc.Write(d.b)
if err != nil {
return err
}
return nil
}
// client interfaces
func (d *DummyCodec) WriteRequest(r *rpc.Request, body interface{}) error {
binary.PutUvarint(d.b, r.Seq)
_, err := d.rwc.Write(d.b)
if err != nil {
return err
}
return nil
}
func (d *DummyCodec) ReadResponseHeader(r *rpc.Response) error {
_, err := d.rwc.Read(d.b)
if err != nil {
return err
}
seq, _ := binary.Uvarint(d.b)
r.Seq = seq
return nil
}
func (d *DummyCodec) ReadResponseBody(body interface{}) error {
return nil
}
func (d *DummyCodec) Close() error {
return d.rwc.Close()
}