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.


Package Files

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


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

func DebugF Uses

func DebugF(format string, argvs ...interface{})

DebugF .

func JSON Uses

func JSON(w http.ResponseWriter, statusCode int, v interface{}) error


func String Uses

func String(w http.ResponseWriter, statusCode int, byts []byte) error

String .

type Client Uses

type Client struct {
    // contains filtered or unexported fields

Client as a data struct to connect to server, send and recv data TODO: finish multi request support

func NewClientWithCodec Uses

func NewClientWithCodec(codec ClientCodec, tcpAddr string) *Client

NewClientWithCodec generate a Client prototype rpc.NewClientWithCodec(codec Codec, tcpAddr 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) Call Uses

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

Call .

func (*Client) Close Uses

func (c *Client) Close()

Close the client connectio to the server

type ClientCodec Uses

type ClientCodec interface {
    // generate a single NewRequest with needed params
    NewRequest(method string, argv interface{}) Request
    // EncodeRequests .
    EncodeRequests(v interface{}) ([]byte, error)
    // parse encoded data into a Response
    ReadResponse(data []byte) ([]Response, error)
    // ReadResponseBody .
    ReadResponseBody(respBody []byte, rcvr interface{}) error

ClientCodec .

type Codec Uses

type Codec interface {

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 Error Uses

type Error struct {
    ErrCode int
    ErrMsg  string

Error . of rpc protocol

func (*Error) Error Uses

func (r *Error) Error() string

func (*Error) Wrap Uses

func (r *Error) Wrap(err error) error

Wrap an error into this

type Request Uses

type Request interface {
    // 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() []byte

    // Ident to get indentify of request
    Ident() string

Request interface contains necessary methods

type Response Uses

type Response interface {
    // 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() []byte

    // SetReqIdent .
    SetReqIdent(ident string)

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 ServerCodec) *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

type ServerCodec Uses

type ServerCodec interface {
    // parse encoded data into a Request
    ReadRequest(data []byte) ([]Request, error)
    // ReadRequestBody parse params
    ReadRequestBody(reqBody []byte, rcvr interface{}) error
    // generate a single Response with needed params
    NewResponse(replyv interface{}) Response
    // ErrResponse to generate a Reponse contains error
    ErrResponse(errcode int, err error) Response
    // EncodeResponses .
    EncodeResponses(v interface{}) ([]byte, error)

ServerCodec . parse request and write response to client.


jsonrpcPackage jsonrpc is JSON-RPC 2.0 support codec and more functions link to url: https://www.jsonrpc.org/specification
protoPackage proto .

Package rpc imports 22 packages (graph) and is imported by 1 packages. Updated 2020-02-10. Refresh now. Tools for package owners.