msgpackrpc

package module
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 9 Imported by: 3

README

net-rpc-msgpackrpc

This library provides the same functions as net/rpc/jsonrpc but for communicating with MessagePack instead. The library is modeled directly after the Go standard library so it should be easy to use and obvious.

See the GoDoc for API documentation.

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

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 Dial

func Dial(network, address string) (*rpc.Client, error)

Dial connects to a MessagePack-RPC server at the specified network address.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL