types

package
v0.14.0-dev.4 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SocketType

func SocketType(listenAddr string) string

Determine if its a unix or tcp socket. If tcp, must specify the port; `0.0.0.0` will return incorrectly as "unix" since there's no port TODO: deprecate

func WithCallInfo

func WithCallInfo(ctx context.Context, ci *CallInfo) context.Context

WithCallInfo returns a child context of ctx with the ci attached.

Types

type CallInfo

type CallInfo struct {
	RPCRequest  *RPCRequest     // non-nil for requests via HTTP or websocket
	HTTPRequest *http.Request   // non-nil for requests via HTTP
	WSConn      WSRPCConnection // non-nil for requests via websocket
}

CallInfo carries JSON-RPC request metadata for RPC functions invoked via JSON-RPC. It can be recovered from the context with GetCallInfo.

func GetCallInfo

func GetCallInfo(ctx context.Context) *CallInfo

GetCallInfo returns the CallInfo record attached to ctx, or nil if ctx does not contain a call record.

func (*CallInfo) RemoteAddr

func (ci *CallInfo) RemoteAddr() string

RemoteAddr returns the remote address (usually a string "IP:port"). If neither HTTPRequest nor WSConn is set, an empty string is returned.

For HTTP requests, this reports the request's RemoteAddr. For websocket requests, this reports the connection's GetRemoteAddr.

type ErrorCode

type ErrorCode int

ErrorCode is the type of JSON-RPC error codes.

const (
	CodeParseError     ErrorCode = -32700 // Invalid JSON received by the server
	CodeInvalidRequest ErrorCode = -32600 // The JSON sent is not a valid request object
	CodeMethodNotFound ErrorCode = -32601 // The method does not exist or is unavailable
	CodeInvalidParams  ErrorCode = -32602 // Invalid method parameters
	CodeInternalError  ErrorCode = -32603 // Internal JSON-RPC error
)

Constants defining the standard JSON-RPC error codes.

func (ErrorCode) String

func (e ErrorCode) String() string

type RPCError

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

func (RPCError) Error

func (err RPCError) Error() string

type RPCRequest

type RPCRequest struct {
	Method string
	Params json.RawMessage
	// contains filtered or unexported fields
}

func NewRequest

func NewRequest(id int) RPCRequest

NewRequest returns an empty request with the specified ID.

func (RPCRequest) ID

func (req RPCRequest) ID() string

ID returns a string representation of the request ID.

func (RPCRequest) IsNotification

func (req RPCRequest) IsNotification() bool

IsNotification reports whether req is a notification (has an empty ID).

func (RPCRequest) MakeError

func (req RPCRequest) MakeError(err error) RPCResponse

MakeError constructs an error response to req from the given error value. This function will panic if err == nil.

func (RPCRequest) MakeErrorf

func (req RPCRequest) MakeErrorf(code ErrorCode, msg string, args ...interface{}) RPCResponse

MakeErrorf constructs an error response to req with the given code and a message constructed by formatting msg with args.

func (RPCRequest) MakeResponse

func (req RPCRequest) MakeResponse(result interface{}) RPCResponse

MakeResponse constructs a success response to req with the given result. If there is an error marshaling result to JSON, it returns an error response.

func (RPCRequest) MarshalJSON

func (req RPCRequest) MarshalJSON() ([]byte, error)

MarshalJSON marshals a request with the appropriate version tag.

func (*RPCRequest) SetMethodAndParams

func (req *RPCRequest) SetMethodAndParams(method string, params interface{}) error

SetMethodAndParams updates the method and parameters of req with the given values, leaving the ID unchanged.

func (RPCRequest) String

func (req RPCRequest) String() string

func (*RPCRequest) UnmarshalJSON

func (req *RPCRequest) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a request from a JSON-RPC 2.0 request object.

type RPCResponse

type RPCResponse struct {
	Result json.RawMessage
	Error  *RPCError
	// contains filtered or unexported fields
}

func (RPCResponse) ID

func (resp RPCResponse) ID() string

ID returns a representation of the response ID.

func (RPCResponse) MarshalJSON

func (resp RPCResponse) MarshalJSON() ([]byte, error)

MarshalJSON marshals a response with the appropriate version tag.

func (RPCResponse) String

func (resp RPCResponse) String() string

func (*RPCResponse) UnmarshalJSON

func (resp *RPCResponse) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a response from a JSON-RPC 2.0 response object.

type WSRPCConnection

type WSRPCConnection interface {
	// GetRemoteAddr returns a remote address of the connection.
	GetRemoteAddr() string
	// WriteRPCResponse writes the response onto connection (BLOCKING).
	WriteRPCResponse(context.Context, RPCResponse) error
	// TryWriteRPCResponse tries to write the response onto connection (NON-BLOCKING).
	TryWriteRPCResponse(context.Context, RPCResponse) bool
	// Context returns the connection's context.
	Context() context.Context
}

WSRPCConnection represents a websocket connection.

Jump to

Keyboard shortcuts

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