inet256ipc

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2022 License: GPL-3.0, MPL-2.0 Imports: 18 Imported by: 0

Documentation

Overview

package inet256ipc provides a generic client and server for using an inet256.Service across process boundaries.

Index

Constants

View Source
const (
	// MaxFrameLen is the maximum length of a frame on the wire
	MaxFrameLen = 4 + MaxMessageLen
	// MaxFrameBodyLen is the maximum length of the body of a frame.
	MaxFrameBodyLen = MaxMessageLen
)
View Source
const (
	DefaultKeepAliveInterval = 1 * time.Second
	DefaultKeepAliveTimeout  = 3 * time.Second
)
View Source
const (
	MT_Data      = MessageType('D'<<24 | 'A'<<16 | 'T'<<8 | 'A')
	MT_PublicKey = MessageType('P'<<24 | 'U'<<16 | 'B'<<8 | 'K')
	MT_FindAddr  = MessageType('F'<<24 | 'I'<<16 | 'N'<<8 | 'D')
	MT_MTU       = MessageType('M'<<24 | 'T'<<16 | 'U'<<8 | '_')
	MT_KeepAlive = MessageType('K'<<24 | 'E'<<16 | 'E'<<8 | 'P')
)
View Source
const (
	MaxMessageLen = 4 + 32 + inet256.MaxMTU
	MinMessageLen = 4
	ReqIDLen      = 16

	MinDataMsgLen = MinMessageLen + 32
	MinAskMsgLen  = MinMessageLen + ReqIDLen
)

Variables

This section is empty.

Functions

func NewRequestID

func NewRequestID() (ret [16]byte)

func ServeNode

func ServeNode(ctx context.Context, node inet256.Node, framer Framer, opts ...ServeNodeOption) error

ServeNode reads and writes packets from a Framer, using the node to send network data and answer requests. If the context is cancelled ServeNode returns nil.

func WriteAskMessage

func WriteAskMessage(fr *Frame, reqID [16]byte, mtype MessageType, x any)

func WriteDataMessage

func WriteDataMessage(fr *Frame, addr inet256.Addr, data []byte)

WriteDataMessage places a message in the frame

func WriteError

func WriteError[T any](fr *Frame, reqID [16]byte, mtype MessageType, err error)

func WriteKeepAlive

func WriteKeepAlive(fr *Frame)

func WriteRequest

func WriteRequest(fr *Frame, reqID [16]byte, mtype MessageType, x any)

func WriteSuccess

func WriteSuccess[T any](fr *Frame, reqID [16]byte, mtype MessageType, x T)

Types

type DataMsg

type DataMsg struct {
	Addr    inet256.Addr
	Payload []byte
}

type FindAddrReq

type FindAddrReq struct {
	Prefix []byte `json:"prefix"`
	Nbits  int    `json:"nbits"`
}

type FindAddrRes

type FindAddrRes struct {
	Addr inet256.Addr `json:"addr,omitempty"`
}

type Frame

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

Frame is a length-prefixed frame suitable for transmitting messages over a stream.

func NewFrame

func NewFrame() *Frame

func (*Frame) Body

func (fr *Frame) Body() []byte

func (*Frame) End

func (fr *Frame) End() int

End is the end of the frame in the underlying slice

func (*Frame) Len

func (fr *Frame) Len() int

func (*Frame) Payload

func (fr *Frame) Payload() []byte

Payload is the part of the frame to be transmitted on the wire

func (*Frame) SetLen

func (fr *Frame) SetLen(l int)

type Framer

type Framer interface {
	WriteFrame(ctx context.Context, fr *Frame) error
	ReadFrame(ctx context.Context, fr *Frame) error
}

type LookupPublicKeyReq

type LookupPublicKeyReq struct {
	Target inet256.Addr `json:"target"`
}

type LookupPublicKeyRes

type LookupPublicKeyRes struct {
	PublicKey []byte `json:"public_key,omitempty"`
}

type MTUReq

type MTUReq struct {
	Target inet256.Addr `json:"target"`
}

type MTURes

type MTURes struct {
	MTU int `json:"mtu"`
}

type Message

type Message []byte

func AsMessage

func AsMessage(x []byte, isOutgoing bool) (Message, error)

func (Message) AskBody

func (m Message) AskBody() []byte

func (Message) DataAddrBytes

func (m Message) DataAddrBytes() []byte

func (Message) DataMsg

func (m Message) DataMsg() DataMsg

func (Message) DataPayload

func (m Message) DataPayload() []byte

func (Message) FindAddrReq

func (m Message) FindAddrReq() (*FindAddrReq, error)

func (Message) FindAddrRes

func (m Message) FindAddrRes() (*FindAddrRes, error)

func (Message) GetRequestID

func (m Message) GetRequestID() (ret [16]byte)

func (Message) GetType

func (m Message) GetType() MessageType

func (Message) IsAsk

func (m Message) IsAsk() bool

func (Message) IsTell

func (m Message) IsTell() bool

func (Message) LookupPublicKeyReq

func (m Message) LookupPublicKeyReq() (*LookupPublicKeyReq, error)

func (Message) LookupPublicKeyRes

func (m Message) LookupPublicKeyRes() (*LookupPublicKeyRes, error)

func (Message) MTUReq

func (m Message) MTUReq() (*MTUReq, error)

func (Message) MTURes

func (m Message) MTURes() (*MTURes, error)

func (Message) SetRequestID

func (m Message) SetRequestID(id [16]byte)

func (Message) SetType

func (m Message) SetType(mt MessageType)

type MessageType

type MessageType uint32

type NodeClient

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

func NewNodeClient

func NewNodeClient(fr Framer, localPubKey inet256.PublicKey, opts ...NodeClientOption) *NodeClient

NewNodeClient returns a client which will read and write from rwc in the background

func (*NodeClient) Close

func (c *NodeClient) Close() error

func (*NodeClient) FindAddr

func (c *NodeClient) FindAddr(ctx context.Context, prefix []byte, nbits int) (inet256.Addr, error)

func (*NodeClient) LocalAddr

func (c *NodeClient) LocalAddr() inet256.Addr

func (*NodeClient) LookupPublicKey

func (c *NodeClient) LookupPublicKey(ctx context.Context, target inet256.Addr) (inet256.PublicKey, error)

func (*NodeClient) MTU

func (c *NodeClient) MTU(ctx context.Context, target inet256.Addr) int

func (*NodeClient) PublicKey

func (c *NodeClient) PublicKey() inet256.PublicKey

func (*NodeClient) Receive

func (c *NodeClient) Receive(ctx context.Context, fn inet256.ReceiveFunc) error

func (*NodeClient) Send

func (c *NodeClient) Send(ctx context.Context, dst inet256.Addr, data []byte) error

type NodeClientOption

type NodeClientOption func(*clientConfig)

type Response

type Response[T any] struct {
	Success T      `json:"ok,omitempty"`
	Error   string `json:"error,omitempty"`
}

type ServeNodeOption

type ServeNodeOption func(c *serveNodeConfig)

type StreamFramer

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

func NewStreamFramer

func NewStreamFramer(r io.Reader, w io.Writer) *StreamFramer

func (*StreamFramer) ReadFrame

func (sf *StreamFramer) ReadFrame(ctx context.Context, fr *Frame) error

func (*StreamFramer) WriteFrame

func (sf *StreamFramer) WriteFrame(ctx context.Context, fr *Frame) error

Jump to

Keyboard shortcuts

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