-
Notifications
You must be signed in to change notification settings - Fork 9
/
clientcodec.go
198 lines (164 loc) · 4.7 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package sunrpc
import (
"bytes"
"io"
"net"
"net/rpc"
"sync"
"github.com/rasky/go-xdr/xdr2"
)
type clientCodec struct {
conn io.ReadWriteCloser // network connection
recordReader io.Reader // reader for RPC record
notifyClose chan<- io.ReadWriteCloser
// Sun RPC responses include Seq (XID) but not ServiceMethod (procedure
// number). Go package net/rpc expects both. So we save ServiceMethod
// when sending the request and look it up when filling rpc.Response
mutex sync.Mutex // protects pending
pending map[uint64]string // maps Seq (XID) to ServiceMethod
}
// NewClientCodec returns a new rpc.ClientCodec using Sun RPC on conn.
// If a non-nil channel is passed as second argument, the conn is sent on
// that channel when Close() is called on conn.
func NewClientCodec(conn io.ReadWriteCloser, notifyClose chan<- io.ReadWriteCloser) rpc.ClientCodec {
return &clientCodec{
conn: conn,
notifyClose: notifyClose,
pending: make(map[uint64]string),
}
}
// NewClient returns a new rpc.Client which internally uses Sun RPC codec
func NewClient(conn io.ReadWriteCloser) *rpc.Client {
return rpc.NewClientWithCodec(NewClientCodec(conn, nil))
}
// Dial connects to a Sun-RPC server at the specified network address
func Dial(network, address string) (*rpc.Client, error) {
conn, err := net.Dial(network, address)
if err != nil {
return nil, err
}
return NewClient(conn), err
}
func (c *clientCodec) WriteRequest(req *rpc.Request, param interface{}) error {
// rpc.Request.Seq is initialized (from 0) and incremented by net/rpc
// package on each call. This is unit64. But XID as per RFC should
// really be uint32. This increment should be capped till maxOf(uint32)
procedureID, ok := GetProcedureID(req.ServiceMethod)
if !ok {
return ErrProcUnavail
}
c.mutex.Lock()
c.pending[req.Seq] = req.ServiceMethod
c.mutex.Unlock()
// Encapsulate rpc.Request.Seq and rpc.Request.ServiceMethod
call := RPCMsg{
Xid: uint32(req.Seq),
Type: Call,
CBody: CallBody{
RPCVersion: RPCProtocolVersion,
Program: procedureID.ProgramNumber,
Version: procedureID.ProgramVersion,
Procedure: procedureID.ProcedureNumber,
},
}
payload := new(bytes.Buffer)
if _, err := xdr.Marshal(payload, &call); err != nil {
return err
}
if param != nil {
// Marshall actual params/args of the remote procedure
if _, err := xdr.Marshal(payload, ¶m); err != nil {
return err
}
}
// Write payload to network
_, err := WriteFullRecord(c.conn, payload.Bytes())
if err != nil {
if err == io.EOF && c.notifyClose != nil {
c.notifyClose <- c.conn
}
return err
}
return nil
}
func (c *clientCodec) checkReplyForErr(reply *RPCMsg) error {
if reply.Type != Reply {
return ErrInvalidRPCMessageType
}
switch reply.RBody.Stat {
case MsgAccepted:
switch reply.RBody.Areply.Stat {
case Success:
case ProgMismatch:
return ErrProgMismatch{
reply.RBody.Areply.MismatchInfo.Low,
reply.RBody.Areply.MismatchInfo.High}
case ProgUnavail:
return ErrProgUnavail
case ProcUnavail:
return ErrProcUnavail
case GarbageArgs:
return ErrGarbageArgs
case SystemErr:
return ErrSystemErr
default:
return ErrInvalidMsgAccepted
}
case MsgDenied:
switch reply.RBody.Rreply.Stat {
case RPCMismatch:
return ErrRPCMismatch{
reply.RBody.Rreply.MismatchInfo.Low,
reply.RBody.Rreply.MismatchInfo.High}
case AuthError:
return ErrAuthError
default:
return ErrInvalidMsgDeniedType
}
default:
return ErrInvalidRPCRepyType
}
return nil
}
func (c *clientCodec) ReadResponseHeader(resp *rpc.Response) error {
// Read entire RPC message from network
record, err := ReadFullRecord(c.conn)
if err != nil {
if err == io.EOF && c.notifyClose != nil {
c.notifyClose <- c.conn
}
return err
}
c.recordReader = bytes.NewReader(record)
// Unmarshal record as RPC reply
var reply RPCMsg
if _, err = xdr.Unmarshal(c.recordReader, &reply); err != nil {
return err
}
// Unpack rpc.Request.Seq and set rpc.Request.ServiceMethod
resp.Seq = uint64(reply.Xid)
c.mutex.Lock()
resp.ServiceMethod = c.pending[resp.Seq]
delete(c.pending, resp.Seq)
c.mutex.Unlock()
if err := c.checkReplyForErr(&reply); err != nil {
return err
}
return nil
}
func (c *clientCodec) ReadResponseBody(result interface{}) error {
if result == nil {
// read and drain it out ?
return nil
}
if _, err := xdr.Unmarshal(c.recordReader, &result); err != nil {
return err
}
return nil
}
func (c *clientCodec) Close() error {
return c.conn.Close()
}