p2p

package
v0.0.0-...-9f99ff9 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IncomingMessage = 0x1
	IncomingStream  = 0x2
)

Variables

This section is empty.

Functions

func NOPHandshakeFunc

func NOPHandshakeFunc(Peer) error

Types

type Decoder

type Decoder interface {
	Decode(io.Reader, *RPC) error
}

type DefaultDecoder

type DefaultDecoder struct{}

func (DefaultDecoder) Decode

func (d DefaultDecoder) Decode(r io.Reader, msg *RPC) error

type GOBDecoder

type GOBDecoder struct{}

func (GOBDecoder) Decode

func (d GOBDecoder) Decode(r io.Reader, msg *RPC) error

type HandshakeFunc

type HandshakeFunc func(Peer) error

HandshakeFunc is a function that is called when a new connection is established.

type Peer

type Peer interface {
	net.Conn
	Send([]byte) error
	CloseStream()
}

Peer is an interface that represents the remote node.

type RPC

type RPC struct {
	From    net.Addr
	Payload []byte
	Stream  bool
}

RPC holds any arbitrary data that is being sent over each transport between nodes.

type TCPPeer

type TCPPeer struct {
	// The underlying TCP connection of the peer.
	net.Conn
	// contains filtered or unexported fields
}

TCPPeer represents the remote node over a TCP established connection.

func NewTCPPeer

func NewTCPPeer(conn net.Conn, outbound bool) *TCPPeer

func (*TCPPeer) CloseStream

func (p *TCPPeer) CloseStream()

func (*TCPPeer) Send

func (p *TCPPeer) Send(data []byte) error

type TCPTransport

type TCPTransport struct {
	TCPTransportOpts
	// contains filtered or unexported fields
}

func NewTCPTransport

func NewTCPTransport(opts TCPTransportOpts) *TCPTransport

func (*TCPTransport) Addr

func (t *TCPTransport) Addr() string

Addr implements the Transport interface, which will return the address of the transport, which is accepting connection.

func (*TCPTransport) Close

func (t *TCPTransport) Close() error

Close implements the Transport interface, which will close the underlying TCP listener.

func (*TCPTransport) Consume

func (t *TCPTransport) Consume() <-chan RPC

Consume implements the Transport interface, which will return read-only channel for reading incoming messages received from another peer in the network.

func (*TCPTransport) Dial

func (t *TCPTransport) Dial(addr string) error

Dial implements the Transport interface, which will dial a remote node in the network.

func (*TCPTransport) ListenAndAccept

func (t *TCPTransport) ListenAndAccept() error

type TCPTransportOpts

type TCPTransportOpts struct {
	ListenAddr    string
	HandshakeFunc HandshakeFunc
	Decoder       Decoder
	OnPeer        func(Peer) error
}

type Transport

type Transport interface {
	Addr() string
	Dial(string) error
	ListenAndAccept() error
	Consume() <-chan RPC
	Close() error
}

Transport is anything that handles communication between nodes in the network. This can be of the form TCP, UDP, WebSockets, etc.

Jump to

Keyboard shortcuts

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