rpc2

package
v0.0.0-...-8ecc595 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2020 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package rpc2 provides bi-directional RPC client and server similar to net/rpc.

Index

Constants

This section is empty.

Variables

View Source
var DebugLog = false

DebugLog controls the printing of internal and I/O errors.

View Source
var ErrShutdown = errors.New("connection is shut down")

ErrShutdown is returned when the connection is closing or closed.

Functions

This section is empty.

Types

type Call

type Call struct {
	Method string      // The name of the service and method to call.
	Args   interface{} // The argument to the function (*struct).
	Reply  interface{} // The reply from the function (*struct).
	Error  error       // After completion, the error status.
	Time   time.Time
	Done   chan *Call // Strobes when call is complete.
}

Call represents an active RPC.

type CallQueReq

type CallQueReq struct {
	// contains filtered or unexported fields
}

CallQueReq -

type Client

type Client struct {
	Option ClientOption

	OnConnected          func(*Client)
	OnDisconnected       func(*Client)
	OnError              func(*Client, error)
	OnNotificationUpdate func(*NotificationUpdate) NotificationUpdate

	Conn net.Conn

	State *State // additional information to associate with client

	TickerPing *time.Ticker
	DeviceID   string
	Others     string
	Token      string
	Debug      bool
	Data       sync.Map

	ConnID string
	sync.Mutex
	// contains filtered or unexported fields
}

Client represents an RPC Client. There may be multiple outstanding Calls associated with a single Client, and a Client may be used by multiple goroutines simultaneously.

func NewClientWithCodec

func NewClientWithCodec(codec Codec) *Client

NewClientWithCodec is like NewClient but uses the specified codec to encode requests and decode responses.

func (*Client) Call

func (c *Client) Call(method string, args interface{}, reply interface{}) error

Call invokes the named function, waits for it to complete, and returns its error status.

func (*Client) Close

func (c *Client) Close() error

Close waits for active calls to finish and closes the codec.

func (*Client) CloseConnection

func (c *Client) CloseConnection() (err error)

CloseConnection -

func (*Client) Create

func (c *Client) Create(callback func(error)) (err error)

Create -

func (*Client) DisconnectNotify

func (c *Client) DisconnectNotify() chan struct{}

DisconnectNotify returns a channel that is closed when the client connection has gone away.

func (*Client) Go

func (c *Client) Go(method string, args interface{}, reply interface{}, done chan *Call) *Call

Go invokes the function asynchronously. It returns the Call structure representing the invocation. The done channel will signal when the call is complete by returning the same Call object. If done is nil, Go will allocate a new channel. If non-nil, done must be buffered or Go will deliberately crash.

func (*Client) Handle

func (c *Client) Handle(method string, handlerFunc interface{})

Handle registers the handler function for the given method. If a handler already exists for method, Handle panics.

func (*Client) Notify

func (c *Client) Notify(method string, args interface{}) error

Notify sends a request to the receiver but does not wait for a return value.

func (*Client) Run

func (c *Client) Run()

Run the client's read loop. You must run this method before calling any methods on the server.

func (*Client) SetBlocking

func (c *Client) SetBlocking(blocking bool)

SetBlocking puts the client in blocking mode. In blocking mode, received requests are processes synchronously. If you have methods that may take a long time, other subsequent requests may time out.

type ClientOption

type ClientOption struct {
	Address       string
	Port          int
	Autoreconnect bool
	Logger        *logger.Logger
	Handler       map[string]interface{}
	Others        string
	Token         string
	DeviceID      string
	TLS           bool
	Debug         bool
}

ClientOption -

type Codec

type Codec interface {
	// ReadHeader must read a message and populate either the request
	// or the response by inspecting the incoming message.
	ReadHeader(*Request, *Response) error

	// ReadRequestBody into args argument of handler function.
	ReadRequestBody(interface{}) error

	// ReadResponseBody into reply argument of handler function.
	ReadResponseBody(interface{}) error

	// WriteRequest must be safe for concurrent use by multiple goroutines.
	WriteRequest(*Request, interface{}) error

	// WriteResponse must be safe for concurrent use by multiple goroutines.
	WriteResponse(*Response, interface{}) error

	// Close is called when client/server finished with the connection.
	Close() error
}

A Codec implements reading and writing of RPC requests and responses. The client calls ReadHeader to read a message header. The implementation must populate either Request or Response argument. Depending on which argument is populated, ReadRequestBody or ReadResponseBody is called right after ReadHeader. ReadRequestBody and ReadResponseBody may be called with a nil argument to force the body to be read and then discarded.

func NewGobCodec

func NewGobCodec(conn io.ReadWriteCloser) Codec

NewGobCodec returns a new rpc2.Codec using gob encoding/decoding on conn.

type NotificationUpdate

type NotificationUpdate struct {
	Scope string `json:"scope"`
	Data  []byte `json:"data"`
}

NotificationUpdate -

type Request

type Request struct {
	Seq      uint64 // sequence number chosen by client
	Method   string
	Others   string
	DeviceID string
	Token    string
}

Request is a header written before every RPC call.

type Response

type Response struct {
	Seq      uint64 // echoes that of the request
	Error    string // error, if any.
	Others   string
	DeviceID string
	Token    string
}

Response is a header written before every RPC return.

type Server

type Server struct {
	OnConnect    func(*Client)
	OnDisconnect func(*Client)

	Options *ServerOption
	// contains filtered or unexported fields
}

Server responds to RPC requests made by Client.

func NewServer

func NewServer(opt *ServerOption) *Server

NewServer returns a new Server.

func (*Server) Accept

func (s *Server) Accept(lis net.Listener)

Accept accepts connections on the listener and serves requests for each incoming connection. Accept blocks; the caller typically invokes it in a go statement.

func (*Server) Handle

func (s *Server) Handle(method string, handlerFunc interface{})

Handle registers the handler function for the given method. If a handler already exists for method, Handle panics.

func (*Server) ServeCodec

func (s *Server) ServeCodec(codec Codec)

ServeCodec is like ServeConn but uses the specified codec to decode requests and encode responses.

func (*Server) ServeCodecWithState

func (s *Server) ServeCodecWithState(codec Codec, state *State)

ServeCodecWithState is like ServeCodec but also gives the ability to associate a state variable with the client that persists across RPC calls.

func (*Server) ServeConn

func (s *Server) ServeConn(conn io.ReadWriteCloser)

ServeConn runs the server on a single connection. ServeConn blocks, serving the connection until the client hangs up. The caller typically invokes ServeConn in a go statement. ServeConn uses the gob wire format (see package gob) on the connection. To use an alternate codec, use ServeCodec.

func (*Server) Start

func (s *Server) Start() (err error)

Start -

type ServerError

type ServerError string

ServerError represents an error that has been returned from the remote side of the RPC connection.

func (ServerError) Error

func (e ServerError) Error() string

type ServerOption

type ServerOption struct {
	Host        string
	Port        int
	TLS         bool
	ClientDebug bool
	Key         []byte
	Crt         []byte
}

ServerOption -

type State

type State struct {
	// contains filtered or unexported fields
}

State -

func NewState

func NewState() *State

NewState -

func (*State) Get

func (s *State) Get(key string) (value interface{}, ok bool)

Get -

func (*State) Set

func (s *State) Set(key string, value interface{})

Set -

Directories

Path Synopsis
Package hub provides a simple event dispatcher for publish/subscribe pattern.
Package hub provides a simple event dispatcher for publish/subscribe pattern.

Jump to

Keyboard shortcuts

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