Documentation
¶
Index ¶
- Constants
- func GetEventData(size int) []byte
- func GetPacketData() *[]byte
- func PutEventData(buf []byte)
- func PutPacketData(buf *[]byte)
- type Channel
- type Event
- type EventType
- type Host
- func (h *Host) BandwidthLimit(incoming, outgoing uint32)
- func (h *Host) Broadcast(channelID uint8, data []byte, flags PacketFlag)
- func (h *Host) ChannelLimit(limit uint32)
- func (h *Host) CheckEvents() (Event, bool)
- func (h *Host) Close()
- func (h *Host) Connect(address string, channelCount int, data uint32) (*Peer, error)
- func (h *Host) Flush()
- func (h *Host) SetInterceptor(fn func(data []byte, addr *net.UDPAddr) bool)
- type IncomingCommand
- type IncomingFragmentedPacket
- type OutgoingCommand
- type Packet
- type PacketFlag
- type Peer
- func (p *Peer) Disconnect(data uint32)
- func (p *Peer) DisconnectLater(data uint32)
- func (p *Peer) DisconnectNow(data uint32)
- func (p *Peer) Ping()
- func (p *Peer) PingInterval(interval uint32)
- func (p *Peer) Reset()
- func (p *Peer) Send(data []byte, flags PacketFlag) error
- func (p *Peer) SendOnChannel(channelID uint8, data []byte, flags PacketFlag) error
- func (p *Peer) SetState(s PeerState)
- func (p *Peer) State() PeerState
- func (p *Peer) ThrottleConfigure(interval, acceleration, deceleration uint32)
- func (p *Peer) Timeout(limit, min, max uint32)
- type PeerState
- type SimulatedTransport
- func (st *SimulatedTransport) Close() error
- func (st *SimulatedTransport) Inject(data []byte, addr net.Addr)
- func (st *SimulatedTransport) LocalAddr() net.Addr
- func (st *SimulatedTransport) Packets() <-chan Packet
- func (st *SimulatedTransport) Send(addr net.Addr, data []byte) error
- func (st *SimulatedTransport) SentPacketsChan() <-chan Packet
- func (st *SimulatedTransport) SetBandwidthLimit(bytesPerSec int64)
- func (st *SimulatedTransport) SetJitter(jitter time.Duration)
- func (st *SimulatedTransport) SetLatency(min, max time.Duration)
- func (st *SimulatedTransport) SetPacketLossRate(rate float64)
- type SimulatedTransportConfig
- type Transport
- type UDPTransport
Constants ¶
const ( PeerStateDisconnected PeerState = iota PeerStateConnecting = 1 PeerStateAcknowledgingConnect = 2 PeerStateConnectionPending = 3 PeerStateConnectionSucceeded = 4 PeerStateConnected = 5 PeerStateDisconnectLater = 6 PeerStateDisconnecting = 7 PeerStateAcknowledgingDisconnect = 8 PeerStateZombie = 9 )
Variables ¶
This section is empty.
Functions ¶
func GetEventData ¶
func GetPacketData ¶
func GetPacketData() *[]byte
func PutEventData ¶
func PutEventData(buf []byte)
func PutPacketData ¶
func PutPacketData(buf *[]byte)
Types ¶
type Channel ¶
type Channel struct {
IncomingReliableSequenceNumber atomicUint16
OutgoingReliableSequenceNumber atomicUint16
IncomingUnreliableSequenceNumber atomicUint16
OutgoingUnreliableSequenceNumber atomicUint16
IncomingReliableCommands []*IncomingCommand
IncomingUnreliableCommands []*IncomingCommand
// contains filtered or unexported fields
}
type Host ¶
type Host struct {
Events chan Event
EventsDropped atomic.Uint64
Interceptor func(data []byte, addr *net.UDPAddr) bool
ChannelLimitVal uint32
IncomingBandwidth uint32
OutgoingBandwidth uint32
MTU uint32
RandomSeed uint32
// contains filtered or unexported fields
}
func (*Host) BandwidthLimit ¶
BandwidthLimit sets the bandwidth limits for the host.
func (*Host) Broadcast ¶
func (h *Host) Broadcast(channelID uint8, data []byte, flags PacketFlag)
Broadcast sends a packet to all connected peers.
func (*Host) ChannelLimit ¶
ChannelLimit sets the maximum number of channels.
func (*Host) CheckEvents ¶
type IncomingCommand ¶
type OutgoingCommand ¶
type PacketFlag ¶
type PacketFlag uint32
const ( PacketFlagReliable PacketFlag = 1 << 0 PacketFlagUnsequenced PacketFlag = 1 << 1 )
type Peer ¶
type Peer struct {
OutgoingQueue []*OutgoingCommand
SentReliableCommands []*OutgoingCommand
OutgoingReliableSequenceNumber atomicUint16
IncomingReliableSequenceNumber atomicUint16
Address *net.UDPAddr
Host *Host
Channels []*Channel
RoundTripTime uint32
RoundTripTimeVariance uint32
LowestRoundTripTime uint32
LastRoundTripTimeVariance uint32
LastRoundTripTime uint32
HighestRoundTripTimeVariance uint32
LastReceiveTime uint32
LastSendTime uint32
EarliestTimeout uint32
// IDs
IncomingPeerID uint16
OutgoingPeerID uint16 // Remote index
ConnectID uint32
EventData uint32
// Session
IncomingSessionID uint8
OutgoingSessionID uint8
// Configuration
MTU uint32
WindowSize uint32
ChannelCount uint32
IncomingBandwidth uint32
OutgoingBandwidth uint32
PacketThrottleInterval uint32
PacketThrottleAcceleration uint32
PacketThrottleDeceleration uint32
// Timeouts
TimeoutLimit uint32
TimeoutMinimum uint32
TimeoutMaximum uint32
PingIntervalMs uint32
// Unsequenced
OutgoingUnsequencedGroup uint16
UnsequencedWindow [protocol.PeerUnsequencedWindowSize / 32]uint32
// Statistics
PacketsSent uint32
PacketsLost uint32
PacketLoss uint32
PacketLossVariance uint32
// Throttle
PacketThrottle uint32
PacketThrottleLimit uint32
PacketThrottleCounter uint32
PacketThrottleEpoch uint32
// contains filtered or unexported fields
}
func (*Peer) Disconnect ¶
Disconnect sends a disconnect command to the peer.
func (*Peer) DisconnectLater ¶
DisconnectLater schedules a disconnect after all queued outgoing commands are sent.
func (*Peer) DisconnectNow ¶
DisconnectNow immediately disconnects the peer without notification.
func (*Peer) Ping ¶
func (p *Peer) Ping()
Ping sends a ping command to the peer for RTT measurement.
func (*Peer) PingInterval ¶
PingInterval sets the ping interval for the peer.
func (*Peer) SendOnChannel ¶
func (p *Peer) SendOnChannel(channelID uint8, data []byte, flags PacketFlag) error
SendOnChannel sends a packet on the specified channel.
func (*Peer) ThrottleConfigure ¶
ThrottleConfigure sends a throttle configuration command to the peer.
type SimulatedTransport ¶
type SimulatedTransport struct {
PacketsDropped int64
PacketsSent int64
PacketsDuplicated int64
PacketsDelayed int64
// contains filtered or unexported fields
}
SimulatedTransport is a Transport implementation that simulates network impairments: packet loss, latency, jitter, bandwidth limits, duplication, and reordering.
func NewSimulatedTransport ¶
func NewSimulatedTransport(config SimulatedTransportConfig, bufSize int) *SimulatedTransport
NewSimulatedTransport creates a SimulatedTransport with the given config. bufSize controls the capacity of the internal packet channel.
func (*SimulatedTransport) Close ¶
func (st *SimulatedTransport) Close() error
Close shuts down the delivery goroutine and closes channels.
func (*SimulatedTransport) Inject ¶
func (st *SimulatedTransport) Inject(data []byte, addr net.Addr)
Inject bypasses network simulation and pushes a packet directly. Used by tests to simulate a remote peer sending data.
func (*SimulatedTransport) LocalAddr ¶
func (st *SimulatedTransport) LocalAddr() net.Addr
LocalAddr returns a dummy address since this is an in-memory transport.
func (*SimulatedTransport) Packets ¶
func (st *SimulatedTransport) Packets() <-chan Packet
Packets returns the channel of incoming packets (after network delay). Read by the host's run() goroutine.
func (*SimulatedTransport) Send ¶
func (st *SimulatedTransport) Send(addr net.Addr, data []byte) error
Send applies network impairments and schedules the packet for delivery. Called by the host to send outgoing data.
func (*SimulatedTransport) SentPacketsChan ¶
func (st *SimulatedTransport) SentPacketsChan() <-chan Packet
SentPackets returns the channel of sent packets for test observation.
func (*SimulatedTransport) SetBandwidthLimit ¶
func (st *SimulatedTransport) SetBandwidthLimit(bytesPerSec int64)
SetBandwidthLimit dynamically adjusts bandwidth limit.
func (*SimulatedTransport) SetJitter ¶
func (st *SimulatedTransport) SetJitter(jitter time.Duration)
SetJitter dynamically adjusts jitter.
func (*SimulatedTransport) SetLatency ¶
func (st *SimulatedTransport) SetLatency(min, max time.Duration)
SetLatency dynamically adjusts min/max latency.
func (*SimulatedTransport) SetPacketLossRate ¶
func (st *SimulatedTransport) SetPacketLossRate(rate float64)
SetPacketLossRate dynamically adjusts packet loss.
type SimulatedTransportConfig ¶
type SimulatedTransportConfig struct {
Seed int64 // RNG seed for reproducibility (0 = random)
PacketLossRate float64 // 0.0 – 1.0
MinLatency time.Duration // minimum one-way delay
MaxLatency time.Duration // maximum one-way delay
Jitter time.Duration // ± random variation added to latency
BandwidthLimit int64 // bytes/sec, 0 = unlimited
DuplicateRate float64 // 0.0 – 1.0, probability of duplicating a packet
ReorderProb float64 // 0.0 – 1.0, probability of reordering adjacent packets
}
SimulatedTransportConfig configures network impairment parameters. Zero values mean "no impairment" for that dimension.
type Transport ¶
type Transport interface {
// Send sends data to the specified address.
Send(addr net.Addr, data []byte) error
// Close closes the transport.
Close() error
// Packets returns a channel for receiving packets.
Packets() <-chan Packet
// LocalAddr returns the local network address.
LocalAddr() net.Addr
}
Transport defines the interface for network communication. This allows for different implementations (UDP, in-memory for testing, etc.).
type UDPTransport ¶
type UDPTransport struct {
// contains filtered or unexported fields
}
UDPTransport implements the Transport interface using net.UDPConn.
func NewUDPTransport ¶
func NewUDPTransport(address string) (*UDPTransport, error)
NewUDPTransport creates a new UDPTransport listening on the specified address.
func (*UDPTransport) Close ¶
func (t *UDPTransport) Close() error
func (*UDPTransport) LocalAddr ¶
func (t *UDPTransport) LocalAddr() net.Addr
func (*UDPTransport) Packets ¶
func (t *UDPTransport) Packets() <-chan Packet
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
chat/client
command
|
|
|
chat/server
command
|
|
|
echo/client
command
|
|
|
echo/server
command
|
|
|
gamestate/client
command
|
|
|
gamestate/server
command
|
|