p2p

package
v0.0.0-...-487b18a Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StartMessage = 0x0
	StartStream  = 0x1
)

Variables

View Source
var ErrFailedHandshake = errors.New("handshake failed")

ErrFailedHandshake is returned if the handshake between the local and remote node was not successful.

Functions

func DefaultHandshake

func DefaultHandshake(any) error

func DefaultOnHandshakeSuccess

func DefaultOnHandshakeSuccess(p Peer) error

Types

type Channel

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

func (*Channel) Close

func (c *Channel) Close() error

func (*Channel) ConsumeRequests

func (c *Channel) ConsumeRequests() chan *Message

func (*Channel) Id

func (c *Channel) Id() uint32

func (*Channel) Read

func (c *Channel) Read(b []byte) (int, error)

Read implements the net.Conn interface

func (*Channel) RecvResponse

func (c *Channel) RecvResponse() (*Message, error)

func (*Channel) SendRequest

func (c *Channel) SendRequest(req []byte) error

func (*Channel) SendResponse

func (c *Channel) SendResponse(res []byte) error

func (*Channel) Write

func (c *Channel) Write(b []byte) (int, error)

type Decoder

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

type DefaultDecoder

type DefaultDecoder struct {
}

func (DefaultDecoder) Decode

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

type GobDecoder

type GobDecoder struct {
}

func (GobDecoder) Decode

func (d GobDecoder) Decode(r io.Reader, x *Message) error

type HandshakeFunc

type HandshakeFunc func(any) error

A handshake is used to verify authenticity of the connection.

type MConn

type MConn interface {
	OpenChannel() (*Channel, error)
	GetChannel(uint32) (*Channel, bool)

	RegisterRequestHandler(func(*Message) error)

	// SendMessage([]byte) error
	ConsumeMessages() <-chan []byte

	RemoteAddr() net.Addr

	Close() error
	IsClosed() bool
}

type Message

type Message struct {
	// Once we have a handshake defined we could use the
	// persistent public keys to identify the sender instead
	// of a net.Addr.
	From net.Addr

	// Payload will be some encoded bytes with the custom
	// message types as defined and used by the server.
	Payload []byte

	// Unique Id that determines which channel the message
	// was sent through.
	ChannelId uint32
}

A message contains arbitrary data that is sent over a transport between two nodes.

type Peer

type Peer interface {
	// net.Conn
	MConn
	// Send([]byte) error
	WaitGroup() *sync.WaitGroup
}

Peer is any legitimate node on the network that we can connect to.

type TCPMConn

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

func NewTCPMConn

func NewTCPMConn(conn net.Conn, isOutbound bool) *TCPMConn

func (*TCPMConn) Close

func (m *TCPMConn) Close() error

func (*TCPMConn) ConsumeMessages

func (m *TCPMConn) ConsumeMessages() <-chan []byte

func (*TCPMConn) GetChannel

func (m *TCPMConn) GetChannel(id uint32) (*Channel, bool)

func (*TCPMConn) IsClosed

func (m *TCPMConn) IsClosed() bool

func (*TCPMConn) OpenChannel

func (m *TCPMConn) OpenChannel() (*Channel, error)

func (*TCPMConn) RegisterRequestHandler

func (m *TCPMConn) RegisterRequestHandler(f func(*Message) error)

func (*TCPMConn) RemoteAddr

func (m *TCPMConn) RemoteAddr() net.Addr

type TCPPeer

type TCPPeer struct {

	// Embedding the MConn allows us to call it's methods
	// directly on the peer.
	MConn
	// contains filtered or unexported fields
}

tcpPeer represents a remote node over a tcp connection.

func NewTcpPeer

func NewTcpPeer(conn *TCPMConn, isOutbound bool) *TCPPeer

func (*TCPPeer) WaitGroup

func (t *TCPPeer) WaitGroup() *sync.WaitGroup

type TCPTransport

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

func NewTCPTransport

func NewTCPTransport(opts TCPTransportOpts) *TCPTransport

func (*TCPTransport) Close

func (t *TCPTransport) Close() error

func (*TCPTransport) Dial

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

func (*TCPTransport) LAddr

func (t *TCPTransport) LAddr() string

func (*TCPTransport) ListenAndAccept

func (t *TCPTransport) ListenAndAccept() error

func (*TCPTransport) MsgChan

func (t *TCPTransport) MsgChan() <-chan Message

type TCPTransportOpts

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

type Transport

type Transport interface {
	Dial(string) error
	ListenAndAccept() error
	MsgChan() <-chan Message
	Close() error
	LAddr() string
}

Transport will be a method for nodes to communicate with eachother.

Jump to

Keyboard shortcuts

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