Documentation
¶
Overview ¶
Package msgpackrpc implements a MessagePack-RPC ClientCodec and ServerCodec for the rpc package, using the same API as the Go standard library for jsonrpc.
Index ¶
- func CallWithCodec(cc rpc.ClientCodec, method string, args interface{}, resp interface{}) error
- func Dial(network, address string) (*rpc.Client, error)
- func NewClient(conn io.ReadWriteCloser) *rpc.Client
- func NewClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec
- func NewServerCodec(conn io.ReadWriteCloser) rpc.ServerCodec
- func ServeConn(conn io.ReadWriteCloser)
- type MsgpackCodec
- func (cc *MsgpackCodec) Close() error
- func (cc *MsgpackCodec) ReadRequestBody(out interface{}) error
- func (cc *MsgpackCodec) ReadRequestHeader(r *rpc.Request) error
- func (cc *MsgpackCodec) ReadResponseBody(out interface{}) error
- func (cc *MsgpackCodec) ReadResponseHeader(r *rpc.Response) error
- func (cc *MsgpackCodec) WriteRequest(r *rpc.Request, body interface{}) error
- func (cc *MsgpackCodec) WriteResponse(r *rpc.Response, body interface{}) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CallWithCodec ¶
func CallWithCodec(cc rpc.ClientCodec, method string, args interface{}, resp interface{}) error
CallWithCodec is used to perform the same actions as rpc.Client.Call but in a much cheaper way. It assumes the underlying connection is not being shared with multiple concurrent RPCs. The request/response must be syncronous.
func NewClient ¶
func NewClient(conn io.ReadWriteCloser) *rpc.Client
NewClient returns a new rpc.Client to handle requests to the set of services at the other end of the connection.
func NewClientCodec ¶
func NewClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec
NewClientCodec returns a new rpc.ClientCodec using MessagePack-RPC on conn.
func NewServerCodec ¶
func NewServerCodec(conn io.ReadWriteCloser) rpc.ServerCodec
NewServerCodec returns a new rpc.ServerCodec using MessagePack-RPC on conn.
func ServeConn ¶
func ServeConn(conn io.ReadWriteCloser)
ServeConn runs the MessagePack-RPC server on a single connection. ServeConn blocks, serving the connection until the client hangs up. The caller typically invokes ServeConn in a go statement.
Types ¶
type MsgpackCodec ¶
type MsgpackCodec struct {
// contains filtered or unexported fields
}
MsgpackCodec implements the rpc.ClientCodec and rpc.ServerCodec using the msgpack encoding
func NewCodec ¶
func NewCodec(bufReads, bufWrites bool, conn io.ReadWriteCloser) *MsgpackCodec
NewCodec returns a MsgpackCodec that can be used as either a Client or Server rpc Codec using a default handle. It also provides controls for enabling and disabling buffering for both reads and writes.
func NewCodecFromHandle ¶
func NewCodecFromHandle(bufReads, bufWrites bool, conn io.ReadWriteCloser, h *codec.MsgpackHandle) *MsgpackCodec
NewCodecFromHandle returns a MsgpackCodec that can be used as either a Client or Server rpc Codec using the passed handle. It also provides controls for enabling and disabling buffering for both reads and writes.
func (*MsgpackCodec) Close ¶
func (cc *MsgpackCodec) Close() error
func (*MsgpackCodec) ReadRequestBody ¶
func (cc *MsgpackCodec) ReadRequestBody(out interface{}) error
func (*MsgpackCodec) ReadRequestHeader ¶
func (cc *MsgpackCodec) ReadRequestHeader(r *rpc.Request) error
func (*MsgpackCodec) ReadResponseBody ¶
func (cc *MsgpackCodec) ReadResponseBody(out interface{}) error
func (*MsgpackCodec) ReadResponseHeader ¶
func (cc *MsgpackCodec) ReadResponseHeader(r *rpc.Response) error
func (*MsgpackCodec) WriteRequest ¶
func (cc *MsgpackCodec) WriteRequest(r *rpc.Request, body interface{}) error
WriteRequest encodes the provided *rpc.Response header and its associated body, writing both to the underlying connection using Msgpack encoding.
When WriteRequest returns an error to net/rpc, it is propagated to the caller. This allows the caller to deal with the error unlike how WriteResponse has to close the Codec (and connection).
func (*MsgpackCodec) WriteResponse ¶
func (cc *MsgpackCodec) WriteResponse(r *rpc.Response, body interface{}) error
WriteResponse encodes the provided *rpc.Response header and its associated body, writing both to the underlying connection using Msgpack encoding.
If an error occurs at any stage of encoding the header, encoding the body, or flushing the buffered writer (if one is used), the codec will close the underlying connection. This is done because the net/rpc package (which is frozen) does not propagate errors returned by WriteResponse, but optionally logs them. By closing the connection on error, we ensure that the codec (and underlying connection) is not used further in an inconsistent state, and subsequent calls immediately return io.EOF.
Note: It is assumed that once an error is encountered, further communication using this codec is unsafe.