rpc: github.com/yeqown/rpc Index | Files | Directories

package rpc

import "github.com/yeqown/rpc"

Package rpc support rpc call methos for both server and client.

Index

Package Files

client.go codec.go common.go constant.go debug.go doc.go server.go service_map.go

Constants

const (
    SUCCESS         = 0
    ParseErr        = -32700 // ParseErr -32700 语法解析错误,服务端接收到无效的json。该错误发送于服务器尝试解析json文本
    InvalidRequest  = -32600 // InvalidRequest -32600 无效请求发送的json不是一个有效的请求对象。
    MethodNotFound  = -32601 // MethodNotFound -32601 找不到方法 该方法不存在或无效
    InvalidParamErr = -32602 // InvalidParamErr -32602 无效的参数 无效的方法参数。
    InternalErr     = -32603 // InternalErr -32603 内部错误 JSON-RPC内部错误。

)

type Client Uses

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

Client as a data struct to connect to server, send and recv data

func NewClientWithCodec Uses

func NewClientWithCodec(codec Codec, tcpAddr, httpAddr string) *Client

NewClientWithCodec generate a Client prototype rpc.NewClientWithCodec(codec Codec, tcpAddr string, httpAddr string) if codec is nil will use default gobCodec, tcpAddr or httpAddr is empty only when you are sure about it will never be used, otherwise it panic while using some functions.

func (*Client) CallOverHTTP Uses

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

CallOverHTTP generate a http request and send to the server

func (*Client) CallOverHTTPMulti Uses

func (c *Client) CallOverHTTPMulti(rpcReqs []*RequestConfig) error

CallOverHTTPMulti ...

func (*Client) CallOverTCP Uses

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

CallOverTCP call server over tcp TODO: timeout cancel

func (*Client) CallOverTCPMulti Uses

func (c *Client) CallOverTCPMulti(rpcReqs []*RequestConfig) error

CallOverTCPMulti ...

func (*Client) Close Uses

func (c *Client) Close()

Close the client connectio to the server

type Codec Uses

type Codec interface {
    // Encode an interface value into []byte
    Encode(argv interface{}) ([]byte, error)

    // Decode encoded data([]byte) back to an interface which the origin data belongs to
    Decode(data []byte, argv interface{}) error

    // generate a single Response with needed params
    Response(req Request, reply []byte, errcode int) Response

    // parse encoded data into a Response
    ParseResponse(respBody []byte) (Response, error)

    // generate a single Request with needed params
    Request(method string, argv interface{}) Request

    // parse encoded data into a Request
    ParseRequest(data []byte) (Request, error)

    // if MultiSupported return true means, can provide funcs
    // ResponseMulti, ParseResponseMulti, RequestMulti, ParseRequestMulti
    MultiSupported() bool

    // generate a Response which cann support Iter(iterator interface)
    ResponseMulti(resps []Response) Response

    // generate a Request which cann support Iter(iterator interface)
    RequestMulti(cfgs []*RequestConfig) Request
}

Codec ... to encode and decode for client to encode request and decode response for server to encode response den decode request

func NewGobCodec Uses

func NewGobCodec() Codec

NewGobCodec to generate a new gobCodec instance

type Iter Uses

type Iter interface {
    HasNext() bool
    Next() interface{}
}

Iter interface means data structure can be itered

type Request Uses

type Request interface {

    // Iter interface contains 'HasNext() bool' and 'Next() interface{}'
    Iter

    // Method() to return the string of method name.
    // exmaple, "StructDemo.Method1"
    // and, should has no more form
    Method() string

    // Params means all ([]byte) data contains request args
    // all these origin params (interface{} type)
    // should be result of codec encoded
    Params(codec Codec) []byte
}

Request interface contains necessary methods

type RequestConfig Uses

type RequestConfig struct {
    // Method that called by client, if not existed will recv an err.
    Method string

    // Args should be params pointer type
    Args interface{}

    // Reply shoule be result pointer type
    Reply interface{}
}

RequestConfig ... to support request multi

type Response Uses

type Response interface {
    // Iter interface contains 'HasNext() bool' and 'Next() interface{}'
    Iter

    // Error to return err that response struct contains
    // if there is no any error happend, should return nil
    Error() error

    // ErrorCode to return errcode(int) if ok return SUCCESS(0)
    ErrCode() int

    // Reply means all ([]byte) data contains response body
    // all these response data (interface{} type)
    // should be result of codec encoded
    Reply(codec Codec) []byte
}

Response interface contains necessary methods

type Server Uses

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

Server data struct to serve RPC request over TCP and HTTP

func NewServerWithCodec Uses

func NewServerWithCodec(codec Codec) *Server

NewServerWithCodec generate a server to handle all tcp request from rpc client, if codec is nil will use default gobCodec

func (*Server) ListenAndServe Uses

func (s *Server) ListenAndServe(addr string)

ListenAndServe open http support can serve http request

func (*Server) Register Uses

func (s *Server) Register(rcvr interface{}) error

Register parse register type and method maybe save into a Map, input value is a varible want to got varible type name, and all Method Name

func (*Server) RegisterName Uses

func (s *Server) RegisterName(rcvr interface{}, methodName string) error

RegisterName ... only want to export one method of rcvr

func (*Server) ServeHTTP Uses

func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP handle request over HTTP, it also implement the interface of http.Handler

func (*Server) ServeTCP Uses

func (s *Server) ServeTCP(addr string)

ServeTCP Dealing with request decode and Call and response

func (*Server) Start Uses

func (s *Server) Start(tcpAddr, httpAddr string)

Start open tcp and http to serve request, open or not depends on that is addr an empty string, you can also open tcp by s.ServeTCP(addr), at the same time open http by s.ListenAndServe(addr)

Directories

PathSynopsis
json2Package json2 is JSON-RPC 2.0 support codec and more functions link to url: https://www.jsonrpc.org/specification
utils

Package rpc imports 18 packages (graph) and is imported by 1 packages. Updated 2019-06-10. Refresh now. Tools for package owners.