jsonrpc

package
v3.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Overview

Package jsonrpc implements json rpc protocol.

Index

Constants

View Source
const (
	MAX_JSONRPC_ID = 0x7FFFFFFF // max jsonrpc request/response id
	VERSION        = "2.0"      // jsonrpc version
)
View Source
const (
	CodeParseError     = -32700
	CodeInvalidRequest = -32600
	CodeMethodNotFound = -32601
	CodeInvalidParams  = -32602
	CodeInternalError  = -32603
)

Errors defined in the JSON-RPC spec. See http://www.jsonrpc.org/specification#error_object.

View Source
const (
	// DefaultMaxSleepTime max sleep interval in accept
	DefaultMaxSleepTime = 1 * time.Second
	// DefaultHTTPRspBufferSize ...
	DefaultHTTPRspBufferSize = 1024
	// PathPrefix ...
	PathPrefix = byte('/')
)
View Source
const (
	// JSONRPC
	// module name
	JSONRPC = "jsonrpc"
)

Variables

View Source
var (

	// Version is json RPC's version
	Version = "2.0"
)

Functions

func GetProtocol

func GetProtocol() protocol.Protocol

GetProtocol gets JSON RPC protocol.

Types

type CodecData

type CodecData struct {
	ID     int64
	Method string
	Args   interface{}
	Error  string
}

CodecData is codec data for json RPC.

type Error

type Error struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

Error response Error

func NewError

func NewError(code int, message string) *Error

NewError creates a error with @code and @message

func (*Error) Error

func (e *Error) Error() string

Error decodes response error for a string.

type HTTPClient

type HTTPClient struct {
	ID int64
	// contains filtered or unexported fields
}

HTTPClient is a HTTP client ,include ID and options.

func NewHTTPClient

func NewHTTPClient(opt *HTTPOptions) *HTTPClient

NewHTTPClient creates a new HTTP client with HTTPOptions.

func (*HTTPClient) Call

func (c *HTTPClient) Call(ctx context.Context, service *common.URL, req *Request, rsp interface{}) error

Call makes a HTTP call with @ctx , @service ,@req and @rsp

func (*HTTPClient) Do

func (c *HTTPClient) Do(addr, path string, httpHeader http.Header, body []byte) ([]byte, error)

Do is the high level of complexity and the likelihood that the fasthttp client has not been extensively used in production means that you would need to expect a very large benefit to justify the adoption of fasthttp today.

func (*HTTPClient) NewRequest

func (c *HTTPClient) NewRequest(service *common.URL, method string, args interface{}) *Request

NewRequest creates a new HTTP request with @service ,@method and @arguments.

type HTTPOptions

type HTTPOptions struct {
	HandshakeTimeout time.Duration
	HTTPTimeout      time.Duration
}

HTTPOptions is a HTTP option include HandshakeTimeout and HTTPTimeout.

type JsonrpcExporter

type JsonrpcExporter struct {
	protocol.BaseExporter
}

JsonrpcExporter is JSON RPC exporter and extends from base invoker.

func NewJsonrpcExporter

func NewJsonrpcExporter(key string, invoker protocol.Invoker, exporterMap *sync.Map) *JsonrpcExporter

NewJsonrpcExporter creates JSON RPC exporter with @key, @invoker and @exporterMap

func (*JsonrpcExporter) Unexport

func (je *JsonrpcExporter) Unexport()

Unexport exported JSON RPC service.

type JsonrpcInvoker

type JsonrpcInvoker struct {
	protocol.BaseInvoker
	// contains filtered or unexported fields
}

JsonrpcInvoker is JSON RPC invoker

func NewJsonrpcInvoker

func NewJsonrpcInvoker(url *common.URL, client *HTTPClient) *JsonrpcInvoker

NewJsonrpcInvoker creates JSON RPC invoker with @url and @client

func (*JsonrpcInvoker) Invoke

func (ji *JsonrpcInvoker) Invoke(ctx context.Context, invocation protocol.Invocation) protocol.Result

Invoke the JSON RPC invocation and return result.

type JsonrpcProtocol

type JsonrpcProtocol struct {
	protocol.BaseProtocol
	// contains filtered or unexported fields
}

JsonrpcProtocol is JSON RPC protocol.

func NewJsonrpcProtocol

func NewJsonrpcProtocol() *JsonrpcProtocol

NewJsonrpcProtocol creates JSON RPC protocol

func (*JsonrpcProtocol) Destroy

func (jp *JsonrpcProtocol) Destroy()

Destroy will destroy all invoker and exporter, so it only is called once.

func (*JsonrpcProtocol) Export

func (jp *JsonrpcProtocol) Export(invoker protocol.Invoker) protocol.Exporter

Export JSON RPC service for remote invocation

func (*JsonrpcProtocol) Refer

func (jp *JsonrpcProtocol) Refer(url *common.URL) protocol.Invoker

Refer a remote JSON PRC service from registry

type Request

type Request struct {
	ID int64
	// contains filtered or unexported fields
}

Request is HTTP protocol request

type Server

type Server struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Server is JSON RPC server wrapper

func NewServer

func NewServer() *Server

NewServer creates new JSON RPC server.

func (*Server) Start

func (s *Server) Start(url *common.URL)

Start JSON RPC server then ready for accept request.

func (*Server) Stop

func (s *Server) Stop()

Stop JSON RPC server, just can be call once.

type ServerCodec

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

ServerCodec is codec data for request server.

func (*ServerCodec) ReadBody

func (c *ServerCodec) ReadBody(x interface{}) error

ReadBody reads @x as request body.

func (*ServerCodec) ReadHeader

func (c *ServerCodec) ReadHeader(header map[string]string, body []byte) error

ReadHeader reads header and unmarshal to server codec

func (*ServerCodec) Write

func (c *ServerCodec) Write(errMsg string, x interface{}) ([]byte, error)

Write responses as byte

Jump to

Keyboard shortcuts

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