p2p

package
v0.0.0-...-f24b3ef Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: Apache-2.0 Imports: 5 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

NOPHandshakeFunc are generally used for testing purposes It is a no-op function - which means it does nothing use case eg- when we want to test the transport layer without actually doing the handshake

Types

type Decoder

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

type DefaultDecoder

type DefaultDecoder struct{}

func (DefaultDecoder) Decode

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

type GOBDecoder

type GOBDecoder struct{}

func (GOBDecoder) Decode

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

type HandshakeFunc

type HandshakeFunc func(Peer) error

type Peer

type Peer interface {
	Close() error      // Close closes the connection between the local node and the remote node
	Send([]byte) error // Send sends a message to the remote node
	net.Conn           // Conn returns the connection between the local node and the remote node

}

Peer is an interface that defines the methods that a peer must implement ( It represents a node in the network )

type RPC

type RPC struct {
	From    string
	Payload []byte
	Stream  bool
}

type TCPPeer

type TCPPeer struct {
	net.Conn
	// contains filtered or unexported fields
}

func NewTCPPeer

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

func (*TCPPeer) CloseStream

func (p *TCPPeer) CloseStream()

1. CloseStream ---------------------------//

func (*TCPPeer) Send

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

2. Send ---------------------------//

type TCPTransport

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

func NewTCPTransport

func NewTCPTransport(options TCPTransportOptions) *TCPTransport

func (*TCPTransport) Addr

func (t *TCPTransport) Addr() string

1. Addr ---------------------------//

func (*TCPTransport) Close

func (t *TCPTransport) Close() error

3. Close ---------------------------//

func (*TCPTransport) Consume

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

2. Consume ---------------------------//

func (*TCPTransport) Dial

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

5. Dial ---------------------------//

func (*TCPTransport) ListenAndAccept

func (t *TCPTransport) ListenAndAccept() error

4. ListenAndAccept ---------------------------//

type TCPTransportOptions

type TCPTransportOptions struct {
	ListenAddress string
	HandshakeFunc HandshakeFunc    // in this project we are using NOPHandshakeFunc does nothing. But if we want to implement the handshake we can implement it by creating a function and passing it here
	Decoder       Decoder          // Decoder is an interface that defines the methods that a decoder must implement
	OnPeer        func(Peer) error // When new peer is connected, this function does something - here we are doing nothing
}

type Transport

type Transport interface {
	Addr() string           // Addr returns the address of the local node
	Dial(string) error      // Dial dials a remote node and returns a peer
	ListenAndAccept() error // ListenAndAccept listens for incoming connections and accepts them if they are of the correct protocol may it be TCP, UDP  websockets etc
	Close() error           // Close closes the connection between the local node and the remote node
	Consume() <-chan RPC    // Consume returns a channel that will be used to receive messages from the network

}

Transport is an interface that defines the methods that a transport must implement Transport is anything that handles communication between the nodes in the network (peers) eg. TCP, UDP, Websockets, etc.

Jump to

Keyboard shortcuts

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