mplex

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package mplex provides an explicit /mplex/6.7.0 transport for consensus peers.

Dial opens one TCP connection to Target.ConsensusP2P. It runs multistream-select and Noise with the case identity. It then negotiates /mplex/6.7.0 and returns the connection to the caller.

The caller owns the framing. Open, Data, Close, Reset, and Frame send one frame each, with a caller-chosen stream ID and payload. Drain is the only read path. The package starts no goroutine and reads nothing on its own. Between Drain calls the caller withholds its reads, which applies TCP backpressure to the peer.

A connection has one identity and a cap on outbound bytes (MaxSend). Each frame is reported to the session observer with Protocol "mplex" and Raw set.

conn, err := mplex.Dial(ctx, target, 0)
if err != nil {
	return err
}
defer conn.Shut()
if err := conn.Open(1, "stream"); err != nil {
	return err
}
frames, err := conn.Drain(time.Second)

Index

Constants

View Source
const (
	// MaxSend caps the bytes the connection writes after the multiplexer is negotiated.
	MaxSend = 64 << 20
	// MaxFrame is the largest frame payload (the mplex limit, 1 MiB).
	MaxFrame = 1 << 20
)
View Source
const (
	FlagOpen          = 0
	FlagDataReceiver  = 1
	FlagDataInitiator = 2
	FlagCloseReceiver = 3
	FlagCloseInit     = 4
	FlagResetReceiver = 5
	FlagResetInit     = 6
)

Frame flags. The mplex header is (stream ID << 3) | flag.

View Source
const ProtocolID = "/mplex/6.7.0"

ProtocolID is the only stream multiplexer this package negotiates.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

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

Conn is the negotiated mplex connection to the peer.

func Dial

func Dial(ctx context.Context, t target.Target, maxSend uint64) (*Conn, error)

Dial connects to t.ConsensusP2P with the current case identity. It returns once /mplex/6.7.0 is negotiated. Any other outcome is an error. The peer can select another multiplexer or refuse mplex. maxSend bounds the outbound bytes after negotiation (0 means MaxSend).

func DialWithIdentity

func DialWithIdentity(ctx context.Context, t target.Target, key libp2pcrypto.PrivKey, maxSend uint64) (*Conn, error)

DialWithIdentity is Dial with an explicit local identity. Multi-identity cases must opt in explicitly through this path.

func (*Conn) Close

func (c *Conn) Close(id uint64) error

Close sends a Close frame (initiator side) for stream id.

func (*Conn) Data

func (c *Conn) Data(id uint64, payload []byte) error

Data sends a Data frame (initiator side) on stream id.

func (*Conn) Drain

func (c *Conn) Drain(d time.Duration) ([]Frame, error)

Drain reads the peer's frames for up to d and returns them. If the peer closes the connection, the read ends with its error after the frames already read. Between Drain calls nothing is read, which is the backpressure.

func (*Conn) Frame

func (c *Conn) Frame(id, flag uint64, payload []byte) error

Frame sends one frame with a caller-chosen flag (0 to 6). Use it for a flag that the helpers above do not cover.

func (*Conn) Negotiated

func (c *Conn) Negotiated() string

Negotiated is the multiplexer the connection runs; it is always ProtocolID, since Dial fails otherwise.

func (*Conn) Open

func (c *Conn) Open(id uint64, name string) error

Open sends an Open frame for stream id with the given name.

func (*Conn) Received

func (c *Conn) Received() uint64

func (*Conn) Reset

func (c *Conn) Reset(id uint64) error

Reset sends a Reset frame (initiator side) for stream id.

func (*Conn) Sent

func (c *Conn) Sent() uint64

Sent and Received are the exact bytes written to and read from the multiplexed (encrypted) connection since negotiation, counted as the plaintext mplex bytes.

func (*Conn) Shut

func (c *Conn) Shut() error

Shut closes the connection.

type Frame

type Frame struct {
	Stream  uint64
	Flag    uint64
	Payload []byte
}

Frame is one frame read from the peer.

Jump to

Keyboard shortcuts

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