gojsonrpc2

package module
v0.0.0-...-c6afb2e Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: GPL-3.0 Imports: 10 Imported by: 2

README

I needed my own JSON-RPC 2 implimentation for my own specific purposes

so here it is

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IdField

type IdField struct {
	Id any `json:"id,omitempty"`
}

type JSONRPC2Error

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

type JSONRPC2Field

type JSONRPC2Field struct {
	JSONRPC string `json:"jsonrpc"`
}

type JSONRPC2Node

type JSONRPC2Node struct {
	// the resulting errors are returned to PushMessageFromOutsied caller.
	//   error #0 - if protocol error
	//   error #1 - error preventing normal error response
	OnRequestCB           func(msg *Message) (error, error)
	OnUnhandledResponseCB func(msg *Message)

	// JSONRPC2Node doesn't use error returned by this CB. error is simply
	// passed to SendMessage caller
	PushMessageToOutsideCB func(data []byte) error
	// contains filtered or unexported fields
}

func NewJSONRPC2Node

func NewJSONRPC2Node() *JSONRPC2Node

func (*JSONRPC2Node) Close

func (self *JSONRPC2Node) Close()

func (*JSONRPC2Node) DebugPrintfln

func (self *JSONRPC2Node) DebugPrintfln(format string, data ...any)

func (*JSONRPC2Node) DebugPrintln

func (self *JSONRPC2Node) DebugPrintln(data ...any)

func (*JSONRPC2Node) GetDebugName

func (self *JSONRPC2Node) GetDebugName() string

func (*JSONRPC2Node) PushMessageFromOutside

func (self *JSONRPC2Node) PushMessageFromOutside(data []byte) (error, error)

push new message from outside into node returned values:

#0 protocol violation - not critical for server running,
#1 error - should be treated as server errors

func (*JSONRPC2Node) ResetResponseTimeout

func (self *JSONRPC2Node) ResetResponseTimeout(
	id any,
	new_orig_timeout time.Duration,
) bool

returns true on successful operation

func (*JSONRPC2Node) SendError

func (self *JSONRPC2Node) SendError(msg *Message) error

func (*JSONRPC2Node) SendMessage

func (self *JSONRPC2Node) SendMessage(msg *Message) error

send message without performing any protocol compliance checks. except: this function resets msg's jsonrpc field

func (*JSONRPC2Node) SendNotification

func (self *JSONRPC2Node) SendNotification(msg *Message) error

func (*JSONRPC2Node) SendRequest

func (self *JSONRPC2Node) SendRequest(
	msg *Message,
	genid bool,
	unhandled bool,
	rh *JSONRPC2NodeRespHandler,
	response_timeout time.Duration,
	request_id_hook *JSONRPC2NodeNewRequestIdHook,
) (ret_any any, ret_err error)

returns the final message id. this function can be used to automatically generate new Id for message and for defining a response handler for response (or errors). if request_id_hook is defined, it is to send id used in request being sent and waits for signal request_id_hook.Continue before actually perform sending use (pass obj created by you) rh param to be used for response handling

func (*JSONRPC2Node) SendResponse

func (self *JSONRPC2Node) SendResponse(msg *Message) error

func (*JSONRPC2Node) SetDebug

func (self *JSONRPC2Node) SetDebug(val bool)

func (*JSONRPC2Node) SetDebugName

func (self *JSONRPC2Node) SetDebugName(name string)

type JSONRPC2NodeNewRequestIdHook

type JSONRPC2NodeNewRequestIdHook struct {
	NewId    chan<- any
	Continue <-chan struct{}
}

type JSONRPC2NodeRespHandler

type JSONRPC2NodeRespHandler struct {
	OnTimeout  func()
	OnClose    func()
	OnResponse func(message *Message)
}

func NewChannelledJSONRPC2NodeRespHandler

func NewChannelledJSONRPC2NodeRespHandler() (
	timedout <-chan struct{},
	closed <-chan struct{},
	msg <-chan *Message,
	rh *JSONRPC2NodeRespHandler,
)

type Message

type Message struct {
	JSONRPC2Field
	IdField
	Request
	Response
}

func NewRequestFromAny

func NewRequestFromAny(data any) (*Message, error)

func NewRequestFromString

func NewRequestFromString(text string) (*Message, error)

func NewResponseFromAny

func NewResponseFromAny(data any) (*Message, error)

func NewResponseFromString

func NewResponseFromString(text string) (*Message, error)

func (*Message) DelId

func (self *Message) DelId()

func (*Message) GetId

func (self *Message) GetId() (any, bool)

func (*Message) HasErrorField

func (self *Message) HasErrorField() bool

Error != nil

func (*Message) HasId

func (self *Message) HasId() bool

func (*Message) HasMethodField

func (self *Message) HasMethodField() bool

Method != ""

func (*Message) HasRequestFields

func (self *Message) HasRequestFields() bool

HasMethodField()

func (*Message) HasResponseFields

func (self *Message) HasResponseFields() bool

self.HasResultField() || self.HasErrorField()

func (*Message) HasResultField

func (self *Message) HasResultField() bool

Result != nil

func (*Message) IsError

func (self *Message) IsError() bool

is response and is error

func (*Message) IsInvalid

func (self *Message) IsInvalid() bool

message is invalid if it's IsInvalidError() isn't nil

func (*Message) IsInvalidError

func (self *Message) IsInvalidError() error

message is invalid if:

  • it has (or has not) both request and response fields
  • has response fields, but has no ID
  • has response fields, and has both result and error field

func (*Message) IsNotification

func (self *Message) IsNotification() bool

func (*Message) IsRequestAndNotNotification

func (self *Message) IsRequestAndNotNotification() bool

func (*Message) IsRequestOrNotification

func (self *Message) IsRequestOrNotification() bool

func (*Message) IsResponseAndNotError

func (self *Message) IsResponseAndNotError() bool

func (*Message) IsResponseOrError

func (self *Message) IsResponseOrError() bool

func (*Message) SetId

func (self *Message) SetId(val any) error

passing nil - is same as calling DelId()

func (*Message) String

func (val *Message) String() string

type ProtocolErrorConst

type ProtocolErrorConst int
const (
	ProtocolErrorConstParseError     ProtocolErrorConst = -32700
	ProtocolErrorConstInvalidRequest ProtocolErrorConst = -32600
	ProtocolErrorMethodNotFound      ProtocolErrorConst = -32601
	ProtocolErrorInvalidParams       ProtocolErrorConst = -32602
	ProtocolErrorInternalError       ProtocolErrorConst = -32603
	ProtocolErrorServerError_32000   ProtocolErrorConst = -32000
	ProtocolErrorServerError_32099   ProtocolErrorConst = -32099
)

type Request

type Request struct {
	Method string `json:"method,omitempty"`
	Params any    `json:"params,omitempty"`
}

type Response

type Response struct {
	Result any            `json:"result,omitempty"`
	Error  *JSONRPC2Error `json:"error,omitempty"`
}

Jump to

Keyboard shortcuts

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