2018-01-22 13:07:50 +03:00
|
|
|
// Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
|
2017-10-03 21:43:55 +03:00
|
|
|
// Use of this source code is governed by a MIT license found in the LICENSE file.
|
|
|
|
|
|
|
|
package codec
|
|
|
|
|
|
|
|
import (
|
2018-01-22 13:07:50 +03:00
|
|
|
"bufio"
|
2017-11-04 13:45:08 +03:00
|
|
|
"errors"
|
2017-10-03 21:43:55 +03:00
|
|
|
"io"
|
|
|
|
"net/rpc"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Rpc provides a rpc Server or Client Codec for rpc communication.
|
|
|
|
type Rpc interface {
|
|
|
|
ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec
|
|
|
|
ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec
|
|
|
|
}
|
|
|
|
|
2018-01-22 13:07:50 +03:00
|
|
|
// RPCOptions holds options specific to rpc functionality
|
|
|
|
type RPCOptions struct {
|
|
|
|
// RPCNoBuffer configures whether we attempt to buffer reads and writes during RPC calls.
|
|
|
|
//
|
|
|
|
// Set RPCNoBuffer=true to turn buffering off.
|
|
|
|
// Buffering can still be done if buffered connections are passed in, or
|
|
|
|
// buffering is configured on the handle.
|
|
|
|
RPCNoBuffer bool
|
2017-11-04 13:45:08 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 21:43:55 +03:00
|
|
|
// rpcCodec defines the struct members and common methods.
|
|
|
|
type rpcCodec struct {
|
2017-11-04 13:45:08 +03:00
|
|
|
c io.Closer
|
|
|
|
r io.Reader
|
|
|
|
w io.Writer
|
2018-01-22 13:07:50 +03:00
|
|
|
f ioFlusher
|
2017-11-04 13:45:08 +03:00
|
|
|
|
2017-10-03 21:43:55 +03:00
|
|
|
dec *Decoder
|
|
|
|
enc *Encoder
|
2017-11-04 13:45:08 +03:00
|
|
|
// bw *bufio.Writer
|
|
|
|
// br *bufio.Reader
|
|
|
|
mu sync.Mutex
|
|
|
|
h Handle
|
2017-10-03 21:43:55 +03:00
|
|
|
|
2018-01-22 13:07:50 +03:00
|
|
|
cls bool
|
|
|
|
clsmu sync.RWMutex
|
|
|
|
clsErr error
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRPCCodec(conn io.ReadWriteCloser, h Handle) rpcCodec {
|
2017-11-04 13:45:08 +03:00
|
|
|
// return newRPCCodec2(bufio.NewReader(conn), bufio.NewWriter(conn), conn, h)
|
|
|
|
return newRPCCodec2(conn, conn, conn, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRPCCodec2(r io.Reader, w io.Writer, c io.Closer, h Handle) rpcCodec {
|
|
|
|
// defensive: ensure that jsonH has TermWhitespace turned on.
|
|
|
|
if jsonH, ok := h.(*JsonHandle); ok && !jsonH.TermWhitespace {
|
|
|
|
panic(errors.New("rpc requires a JsonHandle with TermWhitespace set to true"))
|
|
|
|
}
|
2018-01-22 13:07:50 +03:00
|
|
|
// always ensure that we use a flusher, and always flush what was written to the connection.
|
|
|
|
// we lose nothing by using a buffered writer internally.
|
|
|
|
f, ok := w.(ioFlusher)
|
|
|
|
bh := h.getBasicHandle()
|
|
|
|
if !bh.RPCNoBuffer {
|
|
|
|
if bh.WriterBufferSize <= 0 {
|
|
|
|
if !ok {
|
|
|
|
bw := bufio.NewWriter(w)
|
|
|
|
f, w = bw, bw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bh.ReaderBufferSize <= 0 {
|
|
|
|
if _, ok = w.(ioPeeker); !ok {
|
|
|
|
if _, ok = w.(ioBuffered); !ok {
|
|
|
|
br := bufio.NewReader(r)
|
|
|
|
r = br
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-03 21:43:55 +03:00
|
|
|
return rpcCodec{
|
2017-11-04 13:45:08 +03:00
|
|
|
c: c,
|
|
|
|
w: w,
|
|
|
|
r: r,
|
|
|
|
f: f,
|
2017-10-03 21:43:55 +03:00
|
|
|
h: h,
|
2017-11-04 13:45:08 +03:00
|
|
|
enc: NewEncoder(w, h),
|
|
|
|
dec: NewDecoder(r, h),
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 13:07:50 +03:00
|
|
|
func (c *rpcCodec) write(obj1, obj2 interface{}, writeObj2 bool) (err error) {
|
2017-10-03 21:43:55 +03:00
|
|
|
if c.isClosed() {
|
2018-01-22 13:07:50 +03:00
|
|
|
return c.clsErr
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
2018-01-22 13:07:50 +03:00
|
|
|
err = c.enc.Encode(obj1)
|
|
|
|
if err == nil {
|
|
|
|
if writeObj2 {
|
|
|
|
err = c.enc.Encode(obj2)
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
2018-01-22 13:07:50 +03:00
|
|
|
// if err == nil && c.f != nil {
|
|
|
|
// err = c.f.Flush()
|
|
|
|
// }
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
2018-01-22 13:07:50 +03:00
|
|
|
if c.f != nil {
|
|
|
|
if err == nil {
|
|
|
|
err = c.f.Flush()
|
|
|
|
} else {
|
2018-04-22 14:51:19 +03:00
|
|
|
_ = c.f.Flush() // swallow flush error, so we maintain prior error on write
|
2018-01-22 13:07:50 +03:00
|
|
|
}
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-22 13:07:50 +03:00
|
|
|
func (c *rpcCodec) swallow(err *error) {
|
|
|
|
defer panicToErr(c.dec, err)
|
|
|
|
c.dec.swallow()
|
|
|
|
}
|
|
|
|
|
2017-10-03 21:43:55 +03:00
|
|
|
func (c *rpcCodec) read(obj interface{}) (err error) {
|
|
|
|
if c.isClosed() {
|
2018-01-22 13:07:50 +03:00
|
|
|
return c.clsErr
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
2018-01-22 13:07:50 +03:00
|
|
|
//If nil is passed in, we should read and discard
|
2017-10-03 21:43:55 +03:00
|
|
|
if obj == nil {
|
2018-01-22 13:07:50 +03:00
|
|
|
// var obj2 interface{}
|
|
|
|
// return c.dec.Decode(&obj2)
|
|
|
|
c.swallow(&err)
|
|
|
|
return
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
|
|
|
return c.dec.Decode(obj)
|
|
|
|
}
|
|
|
|
|
2018-01-22 13:07:50 +03:00
|
|
|
func (c *rpcCodec) isClosed() (b bool) {
|
|
|
|
if c.c != nil {
|
|
|
|
c.clsmu.RLock()
|
|
|
|
b = c.cls
|
|
|
|
c.clsmu.RUnlock()
|
2017-11-04 13:45:08 +03:00
|
|
|
}
|
2018-01-22 13:07:50 +03:00
|
|
|
return
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rpcCodec) Close() error {
|
2018-01-22 13:07:50 +03:00
|
|
|
if c.c == nil || c.isClosed() {
|
|
|
|
return c.clsErr
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
|
|
|
c.clsmu.Lock()
|
|
|
|
c.cls = true
|
2018-01-22 13:07:50 +03:00
|
|
|
c.clsErr = c.c.Close()
|
2017-10-03 21:43:55 +03:00
|
|
|
c.clsmu.Unlock()
|
2018-01-22 13:07:50 +03:00
|
|
|
return c.clsErr
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rpcCodec) ReadResponseBody(body interface{}) error {
|
|
|
|
return c.read(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
type goRpcCodec struct {
|
|
|
|
rpcCodec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *goRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error {
|
|
|
|
// Must protect for concurrent access as per API
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2018-01-22 13:07:50 +03:00
|
|
|
return c.write(r, body, true)
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *goRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2018-01-22 13:07:50 +03:00
|
|
|
return c.write(r, body, true)
|
2017-10-03 21:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *goRpcCodec) ReadResponseHeader(r *rpc.Response) error {
|
|
|
|
return c.read(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *goRpcCodec) ReadRequestHeader(r *rpc.Request) error {
|
|
|
|
return c.read(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *goRpcCodec) ReadRequestBody(body interface{}) error {
|
|
|
|
return c.read(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
// goRpc is the implementation of Rpc that uses the communication protocol
|
|
|
|
// as defined in net/rpc package.
|
|
|
|
type goRpc struct{}
|
|
|
|
|
|
|
|
// GoRpc implements Rpc using the communication protocol defined in net/rpc package.
|
2018-01-22 13:07:50 +03:00
|
|
|
//
|
|
|
|
// Note: network connection (from net.Dial, of type io.ReadWriteCloser) is not buffered.
|
|
|
|
//
|
|
|
|
// For performance, you should configure WriterBufferSize and ReaderBufferSize on the handle.
|
|
|
|
// This ensures we use an adequate buffer during reading and writing.
|
|
|
|
// If not configured, we will internally initialize and use a buffer during reads and writes.
|
|
|
|
// This can be turned off via the RPCNoBuffer option on the Handle.
|
|
|
|
// var handle codec.JsonHandle
|
|
|
|
// handle.RPCNoBuffer = true // turns off attempt by rpc module to initialize a buffer
|
|
|
|
//
|
|
|
|
// Example 1: one way of configuring buffering explicitly:
|
|
|
|
// var handle codec.JsonHandle // codec handle
|
|
|
|
// handle.ReaderBufferSize = 1024
|
|
|
|
// handle.WriterBufferSize = 1024
|
|
|
|
// var conn io.ReadWriteCloser // connection got from a socket
|
|
|
|
// var serverCodec = GoRpc.ServerCodec(conn, handle)
|
|
|
|
// var clientCodec = GoRpc.ClientCodec(conn, handle)
|
|
|
|
//
|
|
|
|
// Example 2: you can also explicitly create a buffered connection yourself,
|
|
|
|
// and not worry about configuring the buffer sizes in the Handle.
|
|
|
|
// var handle codec.Handle // codec handle
|
|
|
|
// var conn io.ReadWriteCloser // connection got from a socket
|
|
|
|
// var bufconn = struct { // bufconn here is a buffered io.ReadWriteCloser
|
|
|
|
// io.Closer
|
|
|
|
// *bufio.Reader
|
|
|
|
// *bufio.Writer
|
|
|
|
// }{conn, bufio.NewReader(conn), bufio.NewWriter(conn)}
|
|
|
|
// var serverCodec = GoRpc.ServerCodec(bufconn, handle)
|
|
|
|
// var clientCodec = GoRpc.ClientCodec(bufconn, handle)
|
|
|
|
//
|
2017-10-03 21:43:55 +03:00
|
|
|
var GoRpc goRpc
|
|
|
|
|
|
|
|
func (x goRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec {
|
|
|
|
return &goRpcCodec{newRPCCodec(conn, h)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x goRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec {
|
|
|
|
return &goRpcCodec{newRPCCodec(conn, h)}
|
|
|
|
}
|