rpc

package
v0.0.0-...-4392d9b Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2022 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package Rpc provides a simple JSON-RPC 2.0 client & server implementation.

Index

Constants

View Source
const (
	CodeParseError     = -32700
	CodeInvalidRequest = -32600
	CodeMethodNotFound = -32601
	CodeInvalidParams  = -32602
	CodeInternalError  = -32603
	CodeServerError    = -32000
)

Variables

This section is empty.

Functions

func StartTCPTransport

func StartTCPTransport(ctx context.Context, port int, matcher Matcher) error

StartTCPTransport starts a TCP server.

Types

type Client

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

Client represent jsonrpc client object.

func NewClient

func NewClient(ip string, port int) *Client

NewClient returns a new Client object.

func (*Client) Call

func (c *Client) Call(ctx context.Context, id any, method string, params json.RawMessage) (*Response, error)

Call calls the given method with the given arguments.

type Error

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

Error represent jsonrpc error object.

type HandlerFunc

type HandlerFunc func(context.Context, json.RawMessage) (json.RawMessage, error)

HandlerFunc represent jsonrpc handler function.

func Wrap

func Wrap[Req, Res any](f func(context.Context, *Req) (Res, error)) HandlerFunc

Wrap wrap the given handler with the rpc handler.

type Matcher

type Matcher interface {
	Match(context.Context, io.Reader, io.Writer)
}

Matcher is a interface that matches a request to a handler.

type Request

type Request struct {
	Jsonrpc string          `json:"jsonrpc"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params"`
	Id      any             `json:"id"`
}

Request represent jsonrpc request object.

type Response

type Response struct {
	Jsonrpc string          `json:"jsonrpc"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *Error          `json:"error,omitempty"`
	Id      any             `json:"id"`
}

Response represent jsonrpc response object.

func RespErr

func RespErr(code int, msg string) *Response

RespErr returns the error response.

func RespOk

func RespOk(result json.RawMessage) *Response

RespOk returns the success response.

type Server

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

Server represent jsonrpc server object.

func New

func New(port int) *Server

New returns a new instance of the rpc server.

func (*Server) Match

func (s *Server) Match(ctx context.Context, r io.Reader, w io.Writer)

Match match the request method with the registered handlers and run the handler.

func (*Server) RegisterService

func (s *Server) RegisterService(method string, handler HandlerFunc)

RegisterService registers a service with the rpc server.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the rpc server.

Jump to

Keyboard shortcuts

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