net

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FirstTCPAddr

func FirstTCPAddr(h *Host) string

FirstTCPAddr returns the best TCP listen multiaddr of h (with peer ID appended). Prefers non-loopback addresses so the displayed address is reachable from other machines.

func GenerateIdentityB64

func GenerateIdentityB64() (string, error)

func PrivateKeyFromB64

func PrivateKeyFromB64(encoded string) (crypto.PrivKey, error)

Types

type BLEWebRTCUpgrader added in v0.3.0

type BLEWebRTCUpgrader struct {
	TURNServers []config.TURNServer

	// OnConnected is called when WebRTC data channel is ready.
	// source is "ble→webrtc".
	OnConnected func(peerUUID, peerNickname string, conn *WebRTCConn)
	OnLog       func(string)

	// BLE send functions (wired by service layer)
	SendOfferFn  func(peerUUID, sdp string)
	SendAnswerFn func(sdp string) // broadcast to all centrals
	// contains filtered or unexported fields
}

BLEWebRTCUpgrader manages WebRTC connection upgrades triggered by BLE discovery. Flow:

Initiator (lower nickname alphabetically):
  BLE peer found → create offer → send via BLE → receive answer → connected

Responder (higher nickname):
  BLE central subscribed → wait for offer → create answer → send via BLE → connected

func NewBLEWebRTCUpgrader added in v0.3.0

func NewBLEWebRTCUpgrader(myNickname string) *BLEWebRTCUpgrader

func (*BLEWebRTCUpgrader) OnBLEPeerFound added in v0.3.0

func (u *BLEWebRTCUpgrader) OnBLEPeerFound(ctx context.Context, peerNickname, peerUUID string)

OnBLEPeerFound is called when BLE discovers a peer. If we are the initiator, create and send an offer.

func (*BLEWebRTCUpgrader) OnSDPReceived added in v0.3.0

func (u *BLEWebRTCUpgrader) OnSDPReceived(ctx context.Context, peerUUID, sdp string, isOffer bool)

OnSDPReceived handles incoming SDP (offer or answer) from BLE.

type Host

type Host struct {
	Libp2p       host.Host
	Nickname     string
	OnFrame      func(peer.ID, proto.Frame)
	OnFileStream func(peer.ID, proto.Frame, io.Reader)
	OnConnected  func(peer.ID)
	OnLog        func(string)
	// contains filtered or unexported fields
}

func NewHost

func NewHost(ctx context.Context, nickname string) (*Host, error)

func NewHostWithIdentity

func NewHostWithIdentity(ctx context.Context, nickname string, priv crypto.PrivKey) (*Host, error)

func (*Host) AttachDiscovery

func (h *Host) AttachDiscovery(mgr *discovery.Manager)

func (*Host) Close

func (h *Host) Close() error

func (*Host) ConnectByAddr

func (h *Host) ConnectByAddr(ctx context.Context, addrStr string) (PeerInfo, error)

ConnectByAddr connects to a peer by full multiaddr string (e.g., /ip4/1.2.3.4/tcp/4001/p2p/QmXxx)

func (*Host) FindByNickname added in v0.2.8

func (h *Host) FindByNickname(ctx context.Context, nickname string) (peer.AddrInfo, error)

FindByNickname searches the DHT for a peer advertising under the given nickname.

func (*Host) NicknameFor

func (h *Host) NicknameFor(id peer.ID) string

func (*Host) RememberNickname

func (h *Host) RememberNickname(id peer.ID, nickname string)

func (*Host) SendFrame

func (h *Host) SendFrame(ctx context.Context, peerID peer.ID, frame proto.Frame) error

SendFrame opens a stream to peerID and sends one frame.

func (*Host) StartDHTDiscovery

func (h *Host) StartDHTDiscovery(ctx context.Context, mgr *discovery.Manager)

StartDHTDiscovery advertises on the DHT and periodically finds peers.

func (*Host) StartStreamHandler

func (h *Host) StartStreamHandler()

StartStreamHandler registers the /bdpeer/1.0.0 stream handler.

type PeerInfo

type PeerInfo struct {
	ID       peer.ID
	Nickname string
	Addrs    []multiaddr.Multiaddr
	Source   string
}

type WebRTCConn added in v0.2.11

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

WebRTCConn wraps a pion PeerConnection with a reliable data channel. It implements io.ReadWriteCloser so it can carry bdpeer protocol frames. io.Pipe is used for the read path so transfer.ReadFrame (io.ReadFull) works correctly.

func NewWebRTCAnswer added in v0.2.11

func NewWebRTCAnswer(ctx context.Context, offerSDP string, turnServers []config.TURNServer) (*WebRTCConn, string, error)

NewWebRTCAnswer creates a peer connection from a remote offer and returns an SDP answer.

func NewWebRTCOffer added in v0.2.11

func NewWebRTCOffer(ctx context.Context, turnServers []config.TURNServer) (*WebRTCConn, string, error)

NewWebRTCOffer creates a peer connection and returns an SDP offer string. Call SetAnswer with the remote answer to complete the handshake.

func (*WebRTCConn) Close added in v0.2.11

func (c *WebRTCConn) Close() error

func (*WebRTCConn) Connected added in v0.2.11

func (c *WebRTCConn) Connected() <-chan struct{}

Connected returns a channel that closes when the ICE connection is established.

func (*WebRTCConn) Read added in v0.2.11

func (c *WebRTCConn) Read(p []byte) (int, error)

func (*WebRTCConn) SetAnswer added in v0.2.11

func (c *WebRTCConn) SetAnswer(answerSDP string) error

SetAnswer completes the offer side handshake with the remote answer SDP.

func (*WebRTCConn) Write added in v0.2.11

func (c *WebRTCConn) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

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