rpc

package
v0.0.0-...-9b57bea Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2017 License: Apache-2.0 Imports: 15 Imported by: 0

README

rpc

为了兼容现有的一些项目, 仿照go标准库的rpc而实现了一套新的rpc框架,

跟标准库的rpc相比, 主要有下面几个区别:

  1. seq使用的是uint32, 而不是uint64
  2. 不使用string类型的method name来寻找被调用函数, 而使用uint32类型的cmd
  3. 不返回string类型的Error, 使用uint32作为error code
  4. 更好的支持有状态的连接
  5. 被调函数不必是某个type的method

Documentation

Index

Constants

View Source
const (
	// Defaults used by HandleHTTP
	DefaultRPCPath   = "/_goRPC_"
	DefaultDebugPath = "/debug/rpc"
)

Variables

View Source
var DefaultServer = NewServer()
View Source
var ErrShutdown = errors.New("connection is shut down")
View Source
var ErrTimeout = errors.New("operation is timeout")

Functions

func Register

func Register(cmd uint32, function interface{}) error

Register publishes the receiver's methods in the DefaultServer.

func ServeCodec

func ServeCodec(codec ServerCodec)

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

func ServeConn

func ServeConn(conn io.ReadWriteCloser)

ServeConn runs the DefaultServer 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.

Types

type Call

type Call struct {
	Cmd   uint32      // The name of the service and method to call.
	Seq   uint32      // sequence of current 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.
	// contains filtered or unexported fields
}

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 Dial

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

Dial connects to an RPC server at the specified network address.

func DialHTTP

func DialHTTP(network, address string) (*Client, error)

DialHTTP connects to an HTTP RPC server at the specified network address listening on the default HTTP RPC path.

func DialHTTPPath

func DialHTTPPath(network, address, path string) (*Client, error)

DialHTTPPath connects to an HTTP RPC server at the specified network address and path.

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

func NewClientWithCodec(codec ClientCodec) *Client

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

func (*Client) Call

func (client *Client) Call(cmd uint32, args interface{}, reply interface{}) error

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

func (*Client) CallWithTimeout

func (client *Client) CallWithTimeout(cmd uint32, args interface{}, reply interface{}, d time.Duration) error

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

func (*Client) Close

func (client *Client) Close() error

func (*Client) Go

func (client *Client) Go(cmd uint32, 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) GoWithTimeout

func (client *Client) GoWithTimeout(cmd uint32, args interface{}, reply interface{}, done chan *Call, d time.Duration) *Call

func (*Client) OnNotify

func (client *Client) OnNotify(cmd uint32, reply interface{})

type ClientCodec

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

	Close() error
}

A ClientCodec implements writing of RPC requests and reading of RPC responses for the client side of an RPC session. The client calls WriteRequest to write a request to the connection and calls ReadResponseHeader and ReadResponseBody in pairs to read responses. The client calls Close when finished with the connection. ReadResponseBody may be called with a nil argument to force the body of the response to be read and then discarded.

type Error

type Error uint32

func (Error) Error

func (e Error) Error() string

type JsonRequest

type JsonRequest struct {
	Cmd    uint32           `json:"cmd"`
	Seq    uint32           `json:"seq"`
	Params *json.RawMessage `json:"params"`
}

type JsonResponse

type JsonResponse struct {
	Cmd    uint32      `json:"cmd"`
	Seq    uint32      `json:"seq"`
	Error  string      `json:"error"`
	Result interface{} `json:"result"`
}

type PendingCall

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

test

func (*PendingCall) Context

func (pc *PendingCall) Context() context.Context

type Request

type Request struct {
	Cmd uint32
	Seq uint32 // sequence number chosen by client
	// contains filtered or unexported fields
}

Request is a header written before every RPC call. It is used internally but documented here as an aid to debugging, such as when analyzing network traffic.

type Response

type Response struct {
	Cmd   uint32 // echoes that of the Request
	Seq   uint32 // echoes that of the request
	Error uint32 // error, if any.
	// contains filtered or unexported fields
}

Response is a header written before every RPC return. It is used internally but documented here as an aid to debugging, such as when analyzing network traffic.

type Server

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

Server represents an RPC Server.

func NewServer

func NewServer() *Server

NewServer returns a new Server.

func (*Server) Call

func (server *Server) Call(pc interface{})

test

func (*Server) Register

func (server *Server) Register(cmd uint32, function interface{}) error

func (*Server) ServeCodec

func (server *Server) ServeCodec(ctx context.Context, codec ServerCodec)

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

func (*Server) ServeCodec2

func (server *Server) ServeCodec2(ctx context.Context, codec ServerCodec, ch chan interface{})

test

func (*Server) ServeConn

func (server *Server) ServeConn(ctx context.Context, 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 ServerCodec

type ServerCodec interface {
	ReadRequestHeader(*Request) error
	ReadRequestBody(interface{}) error
	WriteResponse(*Response, interface{}) error
	Close() error
}

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

Directories

Path Synopsis
Package jsonrpc implements a JSON-RPC ClientCodec and ServerCodec for the rpc package.
Package jsonrpc implements a JSON-RPC ClientCodec and ServerCodec for the rpc package.

Jump to

Keyboard shortcuts

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