Documentation ¶
Overview ¶
Package rpc2 provides bi-directional RPC client and server similar to net/rpc.
Index ¶
- Variables
- type Call
- type Client
- func (c *Client) Call(method string, args interface{}, reply interface{}) error
- func (c *Client) Close() 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()
- type Codec
- type Request
- type Response
- type Server
- type ServerError
Constants ¶
This section is empty.
Variables ¶
var ErrShutdown = errors.New("connection is shut down")
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. Done chan *Call // Strobes when call is complete. }
Call represents an active RPC.
type Client ¶
type Client struct {
// 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 NewClient ¶
func NewClient(conn io.ReadWriteCloser) *Client
NewClient returns a new Client to handle requests to the set of services at the other end of the connection. It adds a buffer to the write side of the connection so the header and payload are sent as a unit.
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.
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.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
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) OnDisconnect ¶
OnDisconnect registers a function to run when a client disconnects.
func (*Server) ServeCodec ¶
ServeCodec is like ServeConn but uses the specified codec to decode requests and encode responses.
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