rpc

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2022 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package rpc 简化[https://github.com/ethereum/go-ethereum]供当前项目使用

Index

Constants

View Source
const MethodSeparator = "."

Variables

View Source
var (
	//ErrInvalidRequest The JSON sent is not a valid Request object.
	ErrInvalidRequest = &preDefinedError{code: -32600, message: "Invalid Request"}
	//ErrMethodNotFound The method does not exist / is not available.
	ErrMethodNotFound = &preDefinedError{code: -32601, message: "Method not found"}
	//ErrInvalidParams Invalid method parameter(s).
	ErrInvalidParams = &preDefinedError{code: -32602, message: "Invalid params"}
	//ErrInternalError Internal JSON-RPC error.
	ErrInternalError = &preDefinedError{code: -32603, message: "Internal error"}
	//ErrParseError Invalid JSON was received by the server.An error occurred on the server while parsing the JSON text.
	ErrParseError = &preDefinedError{code: -32700, message: "Parse error"}
)

下面定义的错误类型是JSON-RPC内置错误

View Source
var ErrCallbackNameExist = fmt.Errorf("callback name exist")

Functions

func EnableDebugErrorOnClient

func EnableDebugErrorOnClient()

Types

type BatchElem

type BatchElem struct {
	Method MessageMethod
	Params []interface{}
	Result interface{}
	Error  error
	// contains filtered or unexported fields
}

type Call

type Call struct {
	Result interface{}
	Error  error

	Elems []BatchElem

	Done chan *Call
	// contains filtered or unexported fields
}

type Client

type Client struct {
	Logger *zap.Logger
	// contains filtered or unexported fields
}

func Dial

func Dial(ctx context.Context, endpoint string, newCodecFunc NewClientCodecFunc) (client *Client, err error)

func DialHTTP

func DialHTTP(ctx context.Context, url *url.URL, newCodecFunc NewClientCodecFunc) *Client

func DialHTTPWithClient

func DialHTTPWithClient(ctx context.Context, url *url.URL, newCodecFunc NewClientCodecFunc, client *http.Client) *Client

func DialIPC

func DialIPC(ctx context.Context, endpoint string, newCodecFunc NewClientCodecFunc) (*Client, error)

func DialInProc

func DialInProc(ctx context.Context, endpoint *Server, newCodecFunc NewClientCodecFunc) *Client

func NewClient

func NewClient(codec ClientCodec) *Client

func (*Client) Bath

func (c *Client) Bath(ctx context.Context, elems ...BatchElem) error

func (*Client) BathAsync

func (c *Client) BathAsync(ctx context.Context, done chan *Call, elems ...BatchElem) *Call

func (*Client) Call

func (c *Client) Call(ctx context.Context, method MessageMethod, result interface{}, params ...interface{}) (err error)

func (*Client) CallAsync

func (c *Client) CallAsync(ctx context.Context, done chan *Call, method MessageMethod, result interface{}, params ...interface{}) *Call

func (*Client) Close

func (c *Client) Close()

func (*Client) Notice

func (c *Client) Notice(ctx context.Context, method string, params ...interface{}) (err error)

type ClientCodec

type ClientCodec interface {
	WriteRequest(RawMessage) error
	MarshalRequestParams(...interface{}) (MessageParams, error)
	MarshalRequest(*RequestMessages) (RawMessage, error)
	UnmarshalResponse(RawMessage) (*ResponseMessages, error)
	UnmarshalResponseResult(MessageResult, interface{}) error
	ReadResponse() (RawMessage, error)
	Close() error
}

type ErrorCode

type ErrorCode interface{ RPCErrorCode() int64 }

type ErrorData

type ErrorData interface{ RPCErrorData() interface{} }

type ErrorMessage

type ErrorMessage interface{ RPCErrorMessage() string }

type Ins

type Ins struct {
	Position   []reflect.Type
	Name       map[string]reflect.Type
	HasContext bool
}

type MessageError

type MessageError struct {
	Code    int64
	Message string
	Data    interface{}
}

func (*MessageError) Error

func (err *MessageError) Error() string

type MessageID

type MessageID = RawMessage

type MessageMethod

type MessageMethod = string

type MessageParams

type MessageParams = RawMessage

type MessageResult

type MessageResult = RawMessage

type NewClientCodecFunc

type NewClientCodecFunc func(io.ReadWriteCloser) ClientCodec

type NewServerCodecFunc

type NewServerCodecFunc func(io.ReadWriteCloser) ServerCodec

type RawMessage

type RawMessage = []byte

type RequestMessage

type RequestMessage struct {
	ID     MessageID
	Method MessageMethod
	Params MessageParams
}

func (*RequestMessage) HasValidID

func (rm *RequestMessage) HasValidID() bool

func (*RequestMessage) IsCallBack

func (rm *RequestMessage) IsCallBack() bool

func (*RequestMessage) IsNotification

func (rm *RequestMessage) IsNotification() bool

func (*RequestMessage) ResponseError

func (rm *RequestMessage) ResponseError(err error) *ResponseMessage

func (*RequestMessage) ResponseResult

func (rm *RequestMessage) ResponseResult(result MessageResult) *ResponseMessage

type RequestMessages

type RequestMessages struct {
	Batch bool
	Elems []*RequestMessage
}

func NewRequestMessages

func NewRequestMessages(messages ...*RequestMessage) *RequestMessages

func (*RequestMessages) Append

func (rms *RequestMessages) Append(m *RequestMessage)

type ResponseMessage

type ResponseMessage struct {
	ID     MessageID
	Result MessageResult
	Error  *MessageError
}

func (*ResponseMessage) HasValidID

func (rm *ResponseMessage) HasValidID() bool

func (*ResponseMessage) IsResponse

func (rm *ResponseMessage) IsResponse() bool

type ResponseMessages

type ResponseMessages struct {
	Batch bool
	Elems []*ResponseMessage
}

func NewResponseMessages

func NewResponseMessages(messages ...*ResponseMessage) *ResponseMessages

func (*ResponseMessages) Append

func (rms *ResponseMessages) Append(message *ResponseMessage)

type Server

type Server struct {
	Logger *zap.Logger
	// contains filtered or unexported fields
}

func NewServer

func NewServer(newCodecFunc NewServerCodecFunc) *Server

func (*Server) Accept

func (s *Server) Accept(listener net.Listener)

func (*Server) Register

func (s *Server) Register(namespaces string, receiver interface{}) error

func (*Server) ServeCodec

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

func (*Server) ServeConn

func (s *Server) ServeConn(ctx context.Context, conn io.ReadWriteCloser)

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(response http.ResponseWriter, request *http.Request)

func (*Server) ServeRequest

func (s *Server) ServeRequest(ctx context.Context, codec ServerCodec) (err error)

ServeRequest 处理单次,不会关闭连接

func (*Server) Shutdown

func (s *Server) Shutdown()

type ServerCodec

type ServerCodec interface {
	ReadRequest() (RawMessage, error)
	UnmarshalRequest(RawMessage) (*RequestMessages, error)
	UnmarshalRequestParams(MessageParams, Ins) ([]reflect.Value, error)
	MarshalResponseResult(interface{}) (MessageResult, error)
	MarshalResponse(*ResponseMessages) (RawMessage, error)
	WriteResponse(RawMessage) error
	Close() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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