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) CallWithContext(ctx context.Context, 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()
- func (c *Client) SetBlocking(blocking bool)
- type Codec
- type Request
- type Response
- type Server
- func (s *Server) Accept(lis net.Listener)
- func (s *Server) Handle(method string, handlerFunc interface{})
- func (s *Server) OnConnect(f func(*Client))
- func (s *Server) OnDisconnect(f func(*Client))
- func (s *Server) ServeCodec(codec Codec)
- func (s *Server) ServeCodecWithState(codec Codec, state *State)
- func (s *Server) ServeConn(conn io.ReadWriteCloser)
- type ServerError
- 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. Done chan *Call // Strobes when call is complete. }
Call represents an active RPC.
type Client ¶
type Client struct { State *State // additional information to associate with client // 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) CallWithContext ¶
func (c *Client) CallWithContext(ctx context.Context, method string, args interface{}, reply interface{}) error
CallWithContext invokes the named function, waits for it to complete, and returns its error status, or an error from Context timeout.
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 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 Server ¶
type Server struct {
// 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) 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) 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