mtp

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultWindowSize     = 64
	MaxWindowSize         = 256
	InitialRTO            = 500 * time.Millisecond
	MinRTO                = 100 * time.Millisecond
	MaxRTO                = 10 * time.Second
	MaxRetransmissions    = 10
	DuplicateACKThreshold = 3
)

ARQ engine constants

View Source
const (
	FECDataShards   = 8
	FECParityShards = 2
	FECMaxPayload   = MaxPayloadSize
)
View Source
const (
	PacketDATA   uint8 = 0x01
	PacketACK    uint8 = 0x02
	PacketSYN    uint8 = 0x03
	PacketSYNACK uint8 = 0x04
	PacketFIN    uint8 = 0x05
	PacketFINACK uint8 = 0x06
	PacketPING   uint8 = 0x07
	PacketPONG   uint8 = 0x08
	PacketFEC    uint8 = 0x09
)

Packet types

View Source
const (
	FlagNone     uint8 = 0x00
	FlagMigrate  uint8 = 0x01 // Session migration
	FlagSACK     uint8 = 0x02 // Selective ACK
	FlagFragment uint8 = 0x04 // Fragmented payload
)

Packet flags

View Source
const MaxPayloadSize = 1200

MaxPayloadSize is the max payload per MTP packet (safe UDP MTU)

Variables

This section is empty.

Functions

This section is empty.

Types

type ARQEngine

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

ARQEngine manages reliable delivery over unreliable UDP

func NewARQEngine

func NewARQEngine(codec *PacketCodec, sendFunc func([]byte) error, deliverBufSize int) *ARQEngine

NewARQEngine creates a new ARQ engine

func (*ARQEngine) Close

func (a *ARQEngine) Close()

Close stops the ARQ engine and cancels all pending retransmissions

func (*ARQEngine) Delivered

func (a *ARQEngine) Delivered() <-chan *Packet

Delivered returns the channel for in-order delivered packets

func (*ARQEngine) HandlePacket

func (a *ARQEngine) HandlePacket(pkt *Packet)

HandlePacket processes a received packet (called from the recv loop)

func (*ARQEngine) Send

func (a *ARQEngine) Send(payload []byte) error

Send queues a DATA packet for reliable delivery. It blocks if the congestion window is full.

func (*ARQEngine) SendControl

func (a *ARQEngine) SendControl(pkt *Packet) error

SendControl sends a control packet (SYN, ACK, FIN, etc.) without ARQ tracking

func (*ARQEngine) Stats

func (a *ARQEngine) Stats() (sent, recv, retransmissions uint64, window int, rto time.Duration)

Stats returns current engine statistics

type FecDecoder

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

FecDecoder collects incoming DATA and FEC packets, and recovers missing DATA packets.

func NewFecDecoder

func NewFecDecoder(onRecover func(uint32, []byte)) (*FecDecoder, error)

func (*FecDecoder) AddData

func (f *FecDecoder) AddData(seq uint32, payload []byte)

AddData records a received DATA packet to aid in recovery of others

func (*FecDecoder) AddParity

func (f *FecDecoder) AddParity(startSeq uint32, parityIdx uint8, payload []byte)

AddParity records a received FEC packet

type FecEncoder

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

FecEncoder collects outgoing DATA payloads and generates Parity packets

func NewFecEncoder

func NewFecEncoder(onParity func(uint32, uint8, []byte)) (*FecEncoder, error)

func (*FecEncoder) AddDataPacket

func (f *FecEncoder) AddDataPacket(seq uint32, payload []byte)

AddDataPacket adds a DATA packet to the current FEC group. It sends parity packets immediately when the group is full.

type Listener

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

Listener is an MTP server-side listener that accepts MTP connections over UDP. It implements a net.Listener-compatible interface.

func Listen

func Listen(address string, secret string) (*Listener, error)

Listen creates a new MTP listener on the given address

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

Accept waits for and returns the next incoming connection. It implements net.Listener.Accept().

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr returns the listener's network address.

func (*Listener) Close

func (l *Listener) Close() error

Close stops the listener and closes the UDP socket.

func (*Listener) GetSession

func (l *Listener) GetSession(sessionID string) *MTPConn

GetSession returns the MTPConn for a given session ID (for VirtualConn swap)

func (*Listener) RemoveConnection

func (l *Listener) RemoveConnection(sessionID string)

RemoveConnection removes a connection from tracking (called on disconnect)

type MTPConn

type MTPConn struct {

	// Traffic stats
	BytesSent   atomic.Int64
	BytesRecv   atomic.Int64
	PacketsSent atomic.Int64
	PacketsRecv atomic.Int64
	// contains filtered or unexported fields
}

MTPConn implements net.Conn over a reliable UDP transport using the MTP protocol. It provides ordered, reliable delivery with polymorphic packet encoding.

func Dial

func Dial(resolver UDPResolver, address string, secret string) (*MTPConn, error)

Dial creates a client-side MTPConn to the given server address

func DialMigrate

func DialMigrate(resolver UDPResolver, address string, secret string, sessionID string) (*MTPConn, error)

DialMigrate creates a new MTPConn for session migration (seamless rotation)

func (*MTPConn) Close

func (c *MTPConn) Close() error

Close implements net.Conn

func (*MTPConn) DispatchPacket

func (c *MTPConn) DispatchPacket(data []byte)

DispatchPacket is called by the listener to route a raw datagram to this connection

func (*MTPConn) LocalAddr

func (c *MTPConn) LocalAddr() net.Addr

LocalAddr implements net.Conn

func (*MTPConn) Read

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

Read implements net.Conn. Blocks until data is available, deadline expires, or conn closes.

func (*MTPConn) RemoteAddr

func (c *MTPConn) RemoteAddr() net.Addr

RemoteAddr implements net.Conn

func (*MTPConn) SessionID

func (c *MTPConn) SessionID() string

SessionID returns the session identifier

func (*MTPConn) SetDeadline

func (c *MTPConn) SetDeadline(t time.Time) error

SetDeadline implements net.Conn

func (*MTPConn) SetReadDeadline

func (c *MTPConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements net.Conn

func (*MTPConn) SetWriteDeadline

func (c *MTPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements net.Conn

func (*MTPConn) Write

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

Write implements net.Conn. It segments data and sends via ARQ.

type Packet

type Packet struct {
	Type       uint8
	SeqNum     uint32
	AckNum     uint32
	Flags      uint8
	Payload    []byte
	SACKBlocks []uint32 // For selective ACK: list of received sequence numbers
}

Packet represents a decoded MTP packet

type PacketCodec

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

PacketCodec handles polymorphic encoding and decoding of MTP packets

func NewPacketCodec

func NewPacketCodec(secret string) *PacketCodec

NewPacketCodec creates a new codec with the given shared secret

func (*PacketCodec) Decode

func (c *PacketCodec) Decode(wire []byte) (*Packet, error)

Decode decrypts and deserializes wire bytes into a Packet. It uses the seqNum-hint from the padding length derivation to strip junk.

func (*PacketCodec) Encode

func (c *PacketCodec) Encode(pkt *Packet) ([]byte, error)

Encode serializes and encrypts a Packet into wire format: [ random junk padding ] [ nonce ] [ encrypted(header + payload) ]

type UDPResolver

type UDPResolver interface {
	ResolveUDPAddr(network, address string) (*net.UDPAddr, error)
}

Jump to

Keyboard shortcuts

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