p2p

package
v0.0.0-...-8b42ba9 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2025 License: CC0-1.0 Imports: 7 Imported by: 0

Documentation

Overview

Encoding and decoding utilities for P2P network messages in GoVaultFS This file provides decoders for handling both structured (GOB) and raw stream messages over the network.

Handshake utilities for P2P connections in GoVaultFS This file defines the handshake function type and a no-op implementation for peer connections.

Message definitions for P2P communication in GoVaultFS This file provides constants and the RPC struct for network messaging between nodes.

TCP transport implementation for GoVaultFS P2P networking This file provides types and logic for peer management, connection handling, and message passing over TCP.

Transport and Peer interfaces for GoVaultFS P2P networking This file defines abstractions for remote nodes and communication channels in the network.

Index

Constants

View Source
const (
	IncomingMessage = 0x1 // Indicates a regular message with payload
	IncomingStream  = 0x2 // Indicates a stream message (e.g., file transfer)
)

Message type constants used to identify the kind of network message received.

Variables

This section is empty.

Functions

func NOPHandshakeFunc

func NOPHandshakeFunc(Peer) error

NOPHandshakeFunc is a no-operation handshake function. It performs no handshake logic and always returns nil (success). Useful as a default or placeholder when no handshake is required.

Types

type Decoder

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

Decoder is an interface for decoding network messages into RPC structs

type DefaultDecoder

type DefaultDecoder struct{}

DefaultDecoder handles both stream and raw byte messages Used for decoding simple network signals and payloads

func (DefaultDecoder) Decode

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

Decode reads the first byte to check for a stream signal. If it's a stream, sets msg.Stream and returns. Otherwise, reads up to 1028 bytes as the message payload.

type GOBDecoder

type GOBDecoder struct{}

GOBDecoder decodes structured messages using Go's gob encoding

func (GOBDecoder) Decode

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

Decode decodes a gob-encoded message from the reader into the RPC struct

type HandshakeFunc

type HandshakeFunc func(Peer) error

HandshakeFunc defines the signature for a handshake function between peers. It allows custom logic to be executed when establishing a connection with a peer. For example, authentication, protocol negotiation, or capability exchange.

type Peer

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

Peer abstracts a remote node in the network. It embeds net.Conn for low-level network operations and adds methods for sending data and managing streams.

Send([]byte) error   - Send raw bytes to the peer
CloseStream()        - Signal the end of a stream (e.g., file transfer)

type RPC

type RPC struct {
	From    string // Sender identifier
	Payload []byte // Message or file data
	Stream  bool   // Stream flag for file/data streaming
}

RPC represents a Remote Procedure Call message sent between nodes. It is the main data structure for exchanging information over the transport layer. Fields:

From    - The sender's node ID or address
Payload - The actual message data or file chunk
Stream  - True if this message is part of a stream (e.g., file transfer)

type TCPPeer

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

TCPPeer represents a remote node connected via TCP. It wraps the net.Conn and tracks whether the connection is outbound (initiated by us) or inbound (accepted from another node). The WaitGroup is used for synchronizing stream operations (e.g., file transfers).

func NewTCPPeer

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

NewTCPPeer creates a new TCPPeer instance for a given connection and direction.

func (*TCPPeer) CloseStream

func (p *TCPPeer) CloseStream()

CloseStream signals that a stream operation (e.g., file transfer) is complete for this peer.

func (*TCPPeer) Send

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

Send writes a byte slice to the peer's TCP connection.

type TCPTransport

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

TCPTransport manages TCP connections and message passing between peers. It implements the Transport interface for GoVaultFS.

func NewTCPTransport

func NewTCPTransport(opts TCPTransportOpts) *TCPTransport

NewTCPTransport creates a new TCPTransport with the given options. The rpcch channel buffers incoming messages for consumption.

func (*TCPTransport) Addr

func (t *TCPTransport) Addr() string

Addr returns the address the transport is listening on (Transport interface).

func (*TCPTransport) Close

func (t *TCPTransport) Close() error

Close shuts down the TCP listener (Transport interface).

func (*TCPTransport) Consume

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

Consume returns a read-only channel for incoming RPC messages (Transport interface).

func (*TCPTransport) Dial

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

Dial connects to a remote peer at the given address and starts handling the connection (Transport interface).

func (*TCPTransport) ListenAndAccept

func (t *TCPTransport) ListenAndAccept() error

ListenAndAccept starts the TCP listener and begins accepting incoming connections. It launches the accept loop in a goroutine and logs the listening address.

type TCPTransportOpts

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

TCPTransportOpts holds configuration for TCPTransport.

ListenAddr    - Address to listen for incoming connections
HandshakeFunc - Function to run on new peer connections (e.g., authentication)
Decoder       - Message decoder for incoming data
OnPeer        - Optional callback for handling new peers

type Transport

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

Transport abstracts any communication channel between nodes (TCP, UDP, WebSockets, etc). It provides methods for connection management and message consumption:

Addr() string             - Get the listening address
Dial(string) error        - Connect to a remote node
ListenAndAccept() error   - Start listening and accepting connections
Consume() <-chan RPC      - Read-only channel for incoming RPC messages
Close() error             - Shut down the transport

Jump to

Keyboard shortcuts

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