Documentation
¶
Index ¶
- Constants
- Variables
- func NewPipeFactoryPair() (*PipeFactory, *PipeFactory)
- func NewPipeFactoryPairWithConfig(config PipeConfig) (*PipeFactory, *PipeFactory)
- type Factory
- type Manager
- type ManagerConfig
- type MessageHandler
- type NetworkCondition
- type PeerAddress
- type Pipe
- func (p *Pipe) AutoProcess() bool
- func (p *Pipe) Close() error
- func (p *Pipe) Condition() NetworkCondition
- func (p *Pipe) Conn0() net.Conn
- func (p *Pipe) Conn1() net.Conn
- func (p *Pipe) Process() int
- func (p *Pipe) SetAutoProcess(enabled bool)
- func (p *Pipe) SetCondition(cond NetworkCondition)
- func (p *Pipe) Tick() int
- type PipeAddr
- type PipeAddresses
- type PipeConfig
- type PipeFactory
- func (f *PipeFactory) CreateTCPListener(port int) (net.Listener, error)
- func (f *PipeFactory) CreateUDPConn(port int) (net.PacketConn, error)
- func (f *PipeFactory) GetTCPClientConn(port int) net.Conn
- func (f *PipeFactory) LocalAddr() net.Addr
- func (f *PipeFactory) PeerAddr() net.Addr
- func (f *PipeFactory) Pipe() *Pipe
- func (f *PipeFactory) SetCondition(cond NetworkCondition)
- type PipeManagerConfig
- type PipeManagerPair
- type PipePacketConn
- func (c *PipePacketConn) Close() error
- func (c *PipePacketConn) LocalAddr() net.Addr
- func (c *PipePacketConn) ReadFrom(b []byte) (n int, addr net.Addr, err error)
- func (c *PipePacketConn) SetDeadline(t time.Time) error
- func (c *PipePacketConn) SetReadDeadline(t time.Time) error
- func (c *PipePacketConn) SetWriteDeadline(t time.Time) error
- func (c *PipePacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error)
- type PipeTCPConn
- func (c *PipeTCPConn) Close() error
- func (c *PipeTCPConn) LocalAddr() net.Addr
- func (c *PipeTCPConn) Read(b []byte) (n int, err error)
- func (c *PipeTCPConn) RemoteAddr() net.Addr
- func (c *PipeTCPConn) SetDeadline(t time.Time) error
- func (c *PipeTCPConn) SetReadDeadline(t time.Time) error
- func (c *PipeTCPConn) SetWriteDeadline(t time.Time) error
- func (c *PipeTCPConn) Write(b []byte) (n int, err error)
- type PipeTCPListener
- type ReceivedMessage
- type TCP
- type TCPConfig
- type TransportType
- type UDP
- type UDPConfig
Constants ¶
const DefaultPort = 5540
DefaultPort is the default Matter port (Spec Section 2.5.6.3).
Variables ¶
var ( // ErrClosed is returned when an operation is attempted on a closed transport. ErrClosed = errors.New("transport: closed") // ErrInvalidAddress is returned when an invalid peer address is provided. ErrInvalidAddress = errors.New("transport: invalid address") // ErrNoHandler is returned when no message handler is configured. ErrNoHandler = errors.New("transport: no message handler configured") // ErrNotStarted is returned when an operation requires a started transport. ErrNotStarted = errors.New("transport: not started") // ErrAlreadyStarted is returned when Start is called on an already running transport. ErrAlreadyStarted = errors.New("transport: already started") // ErrConnectionNotFound is returned when no connection exists for a peer address. ErrConnectionNotFound = errors.New("transport: connection not found for peer") // ErrSendFailed is returned when sending a message fails. ErrSendFailed = errors.New("transport: send failed") // ErrMessageTooLarge is returned when a message exceeds the maximum size. ErrMessageTooLarge = errors.New("transport: message too large") )
Transport errors.
Functions ¶
func NewPipeFactoryPair ¶
func NewPipeFactoryPair() (*PipeFactory, *PipeFactory)
NewPipeFactoryPair creates a pair of PipeFactory instances connected to each other via a Pipe with auto-processing enabled.
Example:
f0, f1 := transport.NewPipeFactoryPair() // Use f0 for device, f1 for controller // Messages flow automatically - no manual pumping needed!
func NewPipeFactoryPairWithConfig ¶
func NewPipeFactoryPairWithConfig(config PipeConfig) (*PipeFactory, *PipeFactory)
NewPipeFactoryPairWithConfig creates a pair of PipeFactory instances with the given configuration.
For manual message control (deterministic testing):
f0, f1 := transport.NewPipeFactoryPairWithConfig(transport.PipeConfig{
AutoProcess: false,
})
// ... do work ...
f0.Pipe().Process() // manually deliver messages
Types ¶
type Factory ¶
type Factory interface {
// CreateUDPConn creates a UDP-like packet connection.
// The port parameter is used for address assignment.
CreateUDPConn(port int) (net.PacketConn, error)
// CreateTCPListener creates a TCP-like listener.
// The port parameter is used for address assignment.
// Returns nil if TCP is not supported.
CreateTCPListener(port int) (net.Listener, error)
}
Factory creates transport connections. Implementations can provide real network connections or virtual pipes for testing.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager coordinates UDP and TCP transports for Matter messaging. It provides a unified interface for sending and receiving messages over both transport types.
func NewManager ¶
func NewManager(config ManagerConfig) (*Manager, error)
NewManager creates a new transport manager with the given configuration.
func (*Manager) LocalAddresses ¶
LocalAddresses returns all local addresses the manager is listening on.
func (*Manager) Send ¶
func (m *Manager) Send(data []byte, peer PeerAddress) error
Send sends a message to the specified peer address. The transport type is determined by the PeerAddress.TransportType field.
type ManagerConfig ¶
type ManagerConfig struct {
// Port is the port to listen on (default: 5540).
Port int
// UDPEnabled controls whether UDP transport is enabled (default: true).
UDPEnabled bool
// TCPEnabled controls whether TCP transport is enabled (default: true).
TCPEnabled bool
// MessageHandler is called for each received message.
// Required.
MessageHandler MessageHandler
// UDPConn is an optional pre-existing UDP connection for testing.
UDPConn net.PacketConn
// TCPListener is an optional pre-existing TCP listener for testing.
TCPListener net.Listener
// LoggerFactory is the factory for creating loggers.
// If nil, logging is disabled.
LoggerFactory logging.LoggerFactory
}
ManagerConfig configures the transport manager.
type MessageHandler ¶
type MessageHandler func(msg *ReceivedMessage)
MessageHandler is called for each received message. Implementations should process messages quickly or dispatch to a goroutine to avoid blocking the transport's read loop.
type NetworkCondition ¶
type NetworkCondition struct {
// DropRate is the probability of dropping a packet (0.0 - 1.0).
DropRate float64
// DelayMin is the minimum delay to add to each packet.
DelayMin time.Duration
// DelayMax is the maximum delay to add to each packet.
// Actual delay is uniformly distributed between DelayMin and DelayMax.
DelayMax time.Duration
// DuplicateRate is the probability of duplicating a packet (0.0 - 1.0).
DuplicateRate float64
// ReorderRate is the probability of reordering packets (0.0 - 1.0).
// When triggered, the packet is delayed by an additional ReorderDelay.
ReorderRate float64
// ReorderDelay is the additional delay for reordered packets.
ReorderDelay time.Duration
}
NetworkCondition configures network behavior simulation. Use this to test protocol behavior under adverse network conditions.
type PeerAddress ¶
type PeerAddress struct {
// Addr is the network address of the peer.
Addr net.Addr
// TransportType identifies the transport protocol (UDP or TCP).
TransportType TransportType
}
PeerAddress identifies a remote peer by network address and transport type.
func NewTCPPeerAddress ¶
func NewTCPPeerAddress(addr net.Addr) PeerAddress
NewTCPPeerAddress creates a PeerAddress for a TCP peer.
func NewUDPPeerAddress ¶
func NewUDPPeerAddress(addr net.Addr) PeerAddress
NewUDPPeerAddress creates a PeerAddress for a UDP peer.
func TCPAddrFromString ¶
func TCPAddrFromString(addr string) (PeerAddress, error)
TCPAddrFromString parses an address string and creates a TCP PeerAddress.
func UDPAddrFromString ¶
func UDPAddrFromString(addr string) (PeerAddress, error)
UDPAddrFromString parses an address string and creates a UDP PeerAddress.
func (PeerAddress) IsValid ¶
func (p PeerAddress) IsValid() bool
IsValid returns true if the peer address has a valid transport type and address.
func (PeerAddress) String ¶
func (p PeerAddress) String() string
String returns a human-readable representation of the peer address.
type Pipe ¶
type Pipe struct {
// contains filtered or unexported fields
}
Pipe provides bidirectional in-memory packet communication between two endpoints. It wraps pion's test.Bridge and adds network condition simulation.
By default, Pipe automatically delivers messages in a background goroutine. Use SetAutoProcess(false) or NewPipeWithConfig for manual control.
This follows the "Virtual Network" testing pattern from docs/style.md. Use Pipe for deterministic, flaky-free tests without real network I/O.
func NewPipe ¶
func NewPipe() *Pipe
NewPipe creates a new bidirectional pipe with auto-processing enabled. Messages are automatically delivered in a background goroutine.
func NewPipeWithConfig ¶
func NewPipeWithConfig(config PipeConfig) *Pipe
NewPipeWithConfig creates a new pipe with the given configuration.
func (*Pipe) AutoProcess ¶
AutoProcess returns whether auto-processing is enabled.
func (*Pipe) Condition ¶
func (p *Pipe) Condition() NetworkCondition
Condition returns the current network condition configuration.
func (*Pipe) Process ¶
Process delivers all queued packets. Returns the number of packets delivered.
Note: When AutoProcess is enabled (default), you typically don't need to call this manually. Use SetAutoProcess(false) for manual control.
func (*Pipe) SetAutoProcess ¶
SetAutoProcess enables or disables automatic message delivery. When disabled, you must call Tick() or Process() manually. This is useful for deterministic testing of specific packet orderings.
func (*Pipe) SetCondition ¶
func (p *Pipe) SetCondition(cond NetworkCondition)
SetCondition configures network condition simulation. The conditions apply to packets in both directions.
type PipeAddresses ¶
type PipeAddresses struct {
// UDP is the UDP peer address, or invalid if UDP is not enabled.
UDP PeerAddress
// TCP is the TCP peer address, or invalid if TCP is not enabled.
TCP PeerAddress
}
PipeAddresses contains the addresses needed to reach a manager over the pipe.
type PipeConfig ¶
type PipeConfig struct {
// AutoProcess enables automatic message delivery in a background goroutine.
// Default: true
AutoProcess bool
// ProcessInterval is how often the auto-processor checks for messages.
// Default: 1ms
ProcessInterval time.Duration
}
PipeConfig configures a Pipe.
func DefaultPipeConfig ¶
func DefaultPipeConfig() PipeConfig
DefaultPipeConfig returns the default pipe configuration.
type PipeFactory ¶
type PipeFactory struct {
// contains filtered or unexported fields
}
PipeFactory creates transports that use a Pipe for communication. Use this for in-memory testing without real network I/O.
By default, messages are automatically delivered in a background goroutine. Use Pipe().SetAutoProcess(false) for manual control over message delivery.
func (*PipeFactory) CreateTCPListener ¶
func (f *PipeFactory) CreateTCPListener(port int) (net.Listener, error)
CreateTCPListener creates a TCP listener using a pipe. The listener will accept exactly one connection (the pipe's endpoint). This is suitable for point-to-point testing scenarios.
func (*PipeFactory) CreateUDPConn ¶
func (f *PipeFactory) CreateUDPConn(port int) (net.PacketConn, error)
CreateUDPConn creates a UDP-like connection using the pipe.
func (*PipeFactory) GetTCPClientConn ¶
func (f *PipeFactory) GetTCPClientConn(port int) net.Conn
GetTCPClientConn returns a TCP client connection for connecting to the peer's listener. This is the counterpart to CreateTCPListener - use it on the "client" side of the pipe.
Example:
f0, f1 := NewPipeFactoryPair() listener, _ := f0.CreateTCPListener(5540) // Server side clientConn := f1.GetTCPClientConn(5540) // Client side serverConn, _ := listener.Accept() // Now clientConn and serverConn are connected via the pipe
func (*PipeFactory) LocalAddr ¶
func (f *PipeFactory) LocalAddr() net.Addr
LocalAddr returns the local address for this side of the pipe.
func (*PipeFactory) PeerAddr ¶
func (f *PipeFactory) PeerAddr() net.Addr
PeerAddr returns the peer address for this side of the pipe.
func (*PipeFactory) Pipe ¶
func (f *PipeFactory) Pipe() *Pipe
Pipe returns the underlying pipe for configuration and manual message control.
To disable auto-processing for deterministic tests:
f.Pipe().SetAutoProcess(false)
To configure network conditions:
f.Pipe().SetCondition(transport.NetworkCondition{
DropRate: 0.1, // 10% packet loss
})
func (*PipeFactory) SetCondition ¶
func (f *PipeFactory) SetCondition(cond NetworkCondition)
SetCondition configures network condition simulation for this factory's pipe.
type PipeManagerConfig ¶
type PipeManagerConfig struct {
// UDP enables UDP transport (default: true if both UDP and TCP are false).
UDP bool
// TCP enables TCP transport (default: true if both UDP and TCP are false).
TCP bool
// Handlers are the message handlers for each manager.
// Handlers[0] is for Manager(0), Handlers[1] is for Manager(1).
Handlers [2]MessageHandler
// PipeConfig configures the underlying pipe (optional).
PipeConfig PipeConfig
}
PipeManagerConfig configures a PipeManagerPair.
type PipeManagerPair ¶
type PipeManagerPair struct {
// contains filtered or unexported fields
}
PipeManagerPair provides two connected Manager instances for testing. Messages sent from one manager arrive at the other via in-memory pipes.
Example:
pair := transport.NewPipeManagerPair(transport.PipeManagerConfig{
UDP: true,
TCP: true,
Handlers: [2]transport.MessageHandler{handler0, handler1},
})
defer pair.Close()
// Send from manager 0 to manager 1
pair.Manager(0).Send(data, pair.PeerAddresses(1).UDP)
// Send from manager 1 to manager 0 over TCP
pair.Manager(1).Send(data, pair.PeerAddresses(0).TCP)
func NewPipeManagerPair ¶
func NewPipeManagerPair(config PipeManagerConfig) (*PipeManagerPair, error)
NewPipeManagerPair creates a pair of connected Manager instances for testing. Both managers are started automatically and ready to use.
func (*PipeManagerPair) Close ¶
func (p *PipeManagerPair) Close() error
Close stops both managers and closes all pipes.
func (*PipeManagerPair) Manager ¶
func (p *PipeManagerPair) Manager(id int) *Manager
Manager returns the manager at the given index (0 or 1).
func (*PipeManagerPair) PeerAddresses ¶
func (p *PipeManagerPair) PeerAddresses(id int) PipeAddresses
PeerAddresses returns the addresses needed to send messages TO the manager at the given index. Use these addresses when sending from the other manager.
Example:
// Send from manager 0 to manager 1 pair.Manager(0).Send(data, pair.PeerAddresses(1).UDP)
func (*PipeManagerPair) Pipe ¶
func (p *PipeManagerPair) Pipe() *Pipe
Pipe returns the underlying UDP pipe for configuration (e.g., network conditions). Returns nil if UDP is not enabled.
func (*PipeManagerPair) TCPPipe ¶
func (p *PipeManagerPair) TCPPipe() *Pipe
TCPPipe returns the underlying TCP pipe for configuration. Returns nil if TCP is not enabled.
type PipePacketConn ¶
type PipePacketConn struct {
// contains filtered or unexported fields
}
PipePacketConn wraps a Pipe endpoint to implement net.PacketConn. This allows pipes to be used with Matter's UDP transport layer.
func (*PipePacketConn) Close ¶
func (c *PipePacketConn) Close() error
Close closes the pipe connection.
func (*PipePacketConn) LocalAddr ¶
func (c *PipePacketConn) LocalAddr() net.Addr
LocalAddr returns the local address.
func (*PipePacketConn) ReadFrom ¶
ReadFrom reads a packet from the pipe. The returned address is the peer's address.
func (*PipePacketConn) SetDeadline ¶
func (c *PipePacketConn) SetDeadline(t time.Time) error
SetDeadline sets the read and write deadlines.
func (*PipePacketConn) SetReadDeadline ¶
func (c *PipePacketConn) SetReadDeadline(t time.Time) error
SetReadDeadline sets the read deadline.
func (*PipePacketConn) SetWriteDeadline ¶
func (c *PipePacketConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the write deadline.
type PipeTCPConn ¶
type PipeTCPConn struct {
// contains filtered or unexported fields
}
PipeTCPConn wraps a net.Conn with pipe-specific addresses. This provides proper LocalAddr and RemoteAddr for pipe-based TCP connections.
func (*PipeTCPConn) LocalAddr ¶
func (c *PipeTCPConn) LocalAddr() net.Addr
LocalAddr returns the local network address.
func (*PipeTCPConn) Read ¶
func (c *PipeTCPConn) Read(b []byte) (n int, err error)
Read reads data from the connection.
func (*PipeTCPConn) RemoteAddr ¶
func (c *PipeTCPConn) RemoteAddr() net.Addr
RemoteAddr returns the remote network address.
func (*PipeTCPConn) SetDeadline ¶
func (c *PipeTCPConn) SetDeadline(t time.Time) error
SetDeadline sets the read and write deadlines.
func (*PipeTCPConn) SetReadDeadline ¶
func (c *PipeTCPConn) SetReadDeadline(t time.Time) error
SetReadDeadline sets the read deadline.
func (*PipeTCPConn) SetWriteDeadline ¶
func (c *PipeTCPConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the write deadline.
type PipeTCPListener ¶
type PipeTCPListener struct {
// contains filtered or unexported fields
}
PipeTCPListener implements net.Listener for pipe-based TCP testing. It accepts exactly one connection (the pipe's endpoint), making it suitable for point-to-point testing scenarios.
func (*PipeTCPListener) Accept ¶
func (l *PipeTCPListener) Accept() (net.Conn, error)
Accept waits for and returns the next connection to the listener. Since a pipe only has two endpoints, Accept will return exactly one connection (the peer's endpoint). Subsequent calls block until Close.
func (*PipeTCPListener) Addr ¶
func (l *PipeTCPListener) Addr() net.Addr
Addr returns the listener's network address.
type ReceivedMessage ¶
type ReceivedMessage struct {
// Data contains the raw message bytes.
Data []byte
// PeerAddr identifies the source of the message.
PeerAddr PeerAddress
}
ReceivedMessage represents an incoming message from the network. The Data field contains the raw message bytes as received from the wire, including the Matter message header, payload, and MIC (if encrypted). Higher layers are responsible for parsing and processing the message.
type TCP ¶
type TCP struct {
// contains filtered or unexported fields
}
TCP provides TCP transport for Matter messages. It wraps a net.Listener and manages persistent connections with peers. Messages are framed with a 4-byte length prefix per Spec Section 4.5.
func (*TCP) AddConnection ¶
AddConnection adds an existing connection to the transport. This is useful for testing with net.Pipe().
func (*TCP) Send ¶
Send sends a message to the specified address over TCP. If no connection exists, one will be established.
type TCPConfig ¶
type TCPConfig struct {
// Listener is an optional pre-existing Listener to use.
// If nil, a new listener will be created using ListenAddr.
Listener net.Listener
// ListenAddr is the address to listen on (e.g., ":5540").
// Ignored if Listener is provided.
ListenAddr string
// MessageHandler is called for each received message.
// Required.
MessageHandler MessageHandler
// LoggerFactory is the factory for creating loggers.
// If nil, logging is disabled.
LoggerFactory logging.LoggerFactory
}
TCPConfig configures the TCP transport.
type TransportType ¶
type TransportType int
TransportType identifies the transport protocol used for a message.
const ( // TransportTypeUnknown is the zero value for unknown transport. TransportTypeUnknown TransportType = iota // TransportTypeUDP indicates UDP transport. TransportTypeUDP // TransportTypeTCP indicates TCP transport. TransportTypeTCP )
func (TransportType) IsValid ¶
func (t TransportType) IsValid() bool
IsValid returns true if the transport type is a known valid type.
func (TransportType) String ¶
func (t TransportType) String() string
String returns the string representation of the transport type.
type UDP ¶
type UDP struct {
// contains filtered or unexported fields
}
UDP provides UDP transport for Matter messages. It wraps a net.PacketConn and provides a read loop that calls the configured MessageHandler for each received message.
type UDPConfig ¶
type UDPConfig struct {
// Conn is an optional pre-existing PacketConn to use.
// If nil, a new connection will be created using ListenAddr.
Conn net.PacketConn
// ListenAddr is the address to listen on (e.g., ":5540").
// Ignored if Conn is provided.
ListenAddr string
// MessageHandler is called for each received message.
// Required.
MessageHandler MessageHandler
// LoggerFactory is the factory for creating loggers.
// If nil, logging is disabled.
LoggerFactory logging.LoggerFactory
}
UDPConfig configures the UDP transport.