| 1 |
// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT license found in the LICENSE file. |
| 3 |
|
| 4 |
package codec |
| 5 |
|
| 6 |
import ( |
| 7 |
"bufio" |
| 8 |
"errors" |
| 9 |
"io" |
| 10 |
"net/rpc" |
| 11 |
"sync" |
| 12 |
) |
| 13 |
|
| 14 |
// // rpcEncodeTerminator allows a handler specify a []byte terminator to send after each Encode. |
| 15 |
// // |
| 16 |
// // Some codecs like json need to put a space after each encoded value, to serve as a |
| 17 |
// // delimiter for things like numbers (else json codec will continue reading till EOF). |
| 18 |
// type rpcEncodeTerminator interface { |
| 19 |
// rpcEncodeTerminate() []byte |
| 20 |
// } |
| 21 |
|
| 22 |
// Rpc provides a rpc Server or Client Codec for rpc communication. |
| 23 |
type Rpc interface { |
| 24 |
ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec |
| 25 |
ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec |
| 26 |
} |
| 27 |
|
| 28 |
// RpcCodecBuffered allows access to the underlying bufio.Reader/Writer |
| 29 |
// used by the rpc connection. It accommodates use-cases where the connection |
| 30 |
// should be used by rpc and non-rpc functions, e.g. streaming a file after |
| 31 |
// sending an rpc response. |
| 32 |
type RpcCodecBuffered interface { |
| 33 |
BufferedReader() *bufio.Reader |
| 34 |
BufferedWriter() *bufio.Writer |
| 35 |
} |
| 36 |
|
| 37 |
// ------------------------------------- |
| 38 |
|
| 39 |
// rpcCodec defines the struct members and common methods. |
| 40 |
type rpcCodec struct { |
| 41 |
rwc io.ReadWriteCloser |
| 42 |
dec *Decoder |
| 43 |
enc *Encoder |
| 44 |
bw *bufio.Writer |
| 45 |
br *bufio.Reader |
| 46 |
mu sync.Mutex |
| 47 |
h Handle |
| 48 |
|
| 49 |
cls bool |
| 50 |
clsmu sync.RWMutex |
| 51 |
} |
| 52 |
|
| 53 |
func newRPCCodec(conn io.ReadWriteCloser, h Handle) rpcCodec { |
| 54 |
bw := bufio.NewWriter(conn) |
| 55 |
br := bufio.NewReader(conn) |
| 56 |
|
| 57 |
// defensive: ensure that jsonH has TermWhitespace turned on. |
| 58 |
if jsonH, ok := h.(*JsonHandle); ok && !jsonH.TermWhitespace { |
| 59 |
panic(errors.New("rpc requires a JsonHandle with TermWhitespace set to true")) |
| 60 |
} |
| 61 |
|
| 62 |
return rpcCodec{ |
| 63 |
rwc: conn, |
| 64 |
bw: bw, |
| 65 |
br: br, |
| 66 |
enc: NewEncoder(bw, h), |
| 67 |
dec: NewDecoder(br, h), |
| 68 |
h: h, |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func (c *rpcCodec) BufferedReader() *bufio.Reader { |
| 73 |
return c.br |
| 74 |
} |
| 75 |
|
| 76 |
func (c *rpcCodec) BufferedWriter() *bufio.Writer { |
| 77 |
return c.bw |
| 78 |
} |
| 79 |
|
| 80 |
func (c *rpcCodec) write(obj1, obj2 interface{}, writeObj2, doFlush bool) (err error) { |
| 81 |
if c.isClosed() { |
| 82 |
return io.EOF |
| 83 |
} |
| 84 |
if err = c.enc.Encode(obj1); err != nil { |
| 85 |
return |
| 86 |
} |
| 87 |
// t, tOk := c.h.(rpcEncodeTerminator) |
| 88 |
// if tOk { |
| 89 |
// c.bw.Write(t.rpcEncodeTerminate()) |
| 90 |
// } |
| 91 |
if writeObj2 { |
| 92 |
if err = c.enc.Encode(obj2); err != nil { |
| 93 |
return |
| 94 |
} |
| 95 |
// if tOk { |
| 96 |
// c.bw.Write(t.rpcEncodeTerminate()) |
| 97 |
// } |
| 98 |
} |
| 99 |
if doFlush { |
| 100 |
return c.bw.Flush() |
| 101 |
} |
| 102 |
return |
| 103 |
} |
| 104 |
|
| 105 |
func (c *rpcCodec) read(obj interface{}) (err error) { |
| 106 |
if c.isClosed() { |
| 107 |
return io.EOF |
| 108 |
} |
| 109 |
//If nil is passed in, we should still attempt to read content to nowhere. |
| 110 |
if obj == nil { |
| 111 |
var obj2 interface{} |
| 112 |
return c.dec.Decode(&obj2) |
| 113 |
} |
| 114 |
return c.dec.Decode(obj) |
| 115 |
} |
| 116 |
|
| 117 |
func (c *rpcCodec) isClosed() bool { |
| 118 |
c.clsmu.RLock() |
| 119 |
x := c.cls |
| 120 |
c.clsmu.RUnlock() |
| 121 |
return x |
| 122 |
} |
| 123 |
|
| 124 |
func (c *rpcCodec) Close() error { |
| 125 |
if c.isClosed() { |
| 126 |
return io.EOF |
| 127 |
} |
| 128 |
c.clsmu.Lock() |
| 129 |
c.cls = true |
| 130 |
c.clsmu.Unlock() |
| 131 |
return c.rwc.Close() |
| 132 |
} |
| 133 |
|
| 134 |
func (c *rpcCodec) ReadResponseBody(body interface{}) error { |
| 135 |
return c.read(body) |
| 136 |
} |
| 137 |
|
| 138 |
// ------------------------------------- |
| 139 |
|
| 140 |
type goRpcCodec struct { |
| 141 |
rpcCodec |
| 142 |
} |
| 143 |
|
| 144 |
func (c *goRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error { |
| 145 |
// Must protect for concurrent access as per API |
| 146 |
c.mu.Lock() |
| 147 |
defer c.mu.Unlock() |
| 148 |
return c.write(r, body, true, true) |
| 149 |
} |
| 150 |
|
| 151 |
func (c *goRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error { |
| 152 |
c.mu.Lock() |
| 153 |
defer c.mu.Unlock() |
| 154 |
return c.write(r, body, true, true) |
| 155 |
} |
| 156 |
|
| 157 |
func (c *goRpcCodec) ReadResponseHeader(r *rpc.Response) error { |
| 158 |
return c.read(r) |
| 159 |
} |
| 160 |
|
| 161 |
func (c *goRpcCodec) ReadRequestHeader(r *rpc.Request) error { |
| 162 |
return c.read(r) |
| 163 |
} |
| 164 |
|
| 165 |
func (c *goRpcCodec) ReadRequestBody(body interface{}) error { |
| 166 |
return c.read(body) |
| 167 |
} |
| 168 |
|
| 169 |
// ------------------------------------- |
| 170 |
|
| 171 |
// goRpc is the implementation of Rpc that uses the communication protocol |
| 172 |
// as defined in net/rpc package. |
| 173 |
type goRpc struct{} |
| 174 |
|
| 175 |
// GoRpc implements Rpc using the communication protocol defined in net/rpc package. |
| 176 |
// Its methods (ServerCodec and ClientCodec) return values that implement RpcCodecBuffered. |
| 177 |
var GoRpc goRpc |
| 178 |
|
| 179 |
func (x goRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec { |
| 180 |
return &goRpcCodec{newRPCCodec(conn, h)} |
| 181 |
} |
| 182 |
|
| 183 |
func (x goRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec { |
| 184 |
return &goRpcCodec{newRPCCodec(conn, h)} |
| 185 |
} |
| 186 |
|
| 187 |
var _ RpcCodecBuffered = (*rpcCodec)(nil) // ensure *rpcCodec implements RpcCodecBuffered |