Documentation ¶
Overview ¶
Package rpc2 provides bi-directional RPC client and server similar to net/rpc.
Index ¶
- Variables
- type Call
- type CallQueReq
- type Client
- func (c *Client) Call(method string, args interface{}, reply interface{}) error
- func (c *Client) Close() error
- func (c *Client) CloseConnection() (err error)
- func (c *Client) Create(callback func(error)) (err error)
- func (c *Client) DisconnectNotify() chan struct{}
- func (c *Client) Go(method string, args interface{}, reply interface{}, done chan *Call) *Call
- func (c *Client) Handle(method string, handlerFunc interface{})
- func (c *Client) Notify(method string, args interface{}) error
- func (c *Client) Run()
- func (c *Client) SetBlocking(blocking bool)
- type ClientOption
- type Codec
- type NotificationUpdate
- type Request
- type Response
- type Server
- type ServerError
- type ServerOption
- type State
Constants ¶
This section is empty.
Variables ¶
var DebugLog = false
DebugLog controls the printing of internal and I/O errors.
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 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 ¶
NewClientWithCodec is like NewClient but uses the specified codec to encode requests and decode responses.
func (*Client) Call ¶
Call invokes the named function, waits for it to complete, and returns its error status.
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 ¶
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 ¶
Handle registers the handler function for the given method. If a handler already exists for method, Handle panics.
func (*Client) Notify ¶
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 ¶
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 ¶
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 (*Server) Accept ¶
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 ¶
Handle registers the handler function for the given method. If a handler already exists for method, Handle panics.
func (*Server) ServeCodec ¶
ServeCodec is like ServeConn but uses the specified codec to decode requests and encode responses.
func (*Server) ServeCodecWithState ¶
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.
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 ¶
ServerOption -