Documentation ¶
Overview ¶
Package bind provides mechanisms to facilitate listening TCP and UDP ports
Index ¶
- Constants
- func AddrPortSliceTCPListener(s []*net.TCPListener) ([]netip.AddrPort, bool)
- func AddrPortSliceUDPConn(s []*net.UDPConn) ([]netip.AddrPort, bool)
- func Bind(cfg *Config) ([]*net.TCPListener, []*net.UDPConn, error)
- func IPAddresses(s []netip.AddrPort) ([]netip.Addr, bool)
- func SamePort(s []netip.AddrPort) (uint16, bool)
- func StringIPAddresses(s []netip.AddrPort) ([]string, bool)
- type AllListener
- type AllTCPListener
- type AllUDPListener
- type Config
- func (cfg *Config) Addrs() ([]net.IP, error)
- func (cfg *Config) Bind() ([]*net.TCPListener, []*net.UDPConn, error)
- func (cfg *Config) Refresh(s []netip.AddrPort) bool
- func (cfg *Config) RefreshFromTCPListeners(tcpListeners []*net.TCPListener) bool
- func (cfg *Config) RefreshFromUDPConn(udpListeners []*net.UDPConn) bool
- func (cfg *Config) SetDefaults() error
- func (cfg *Config) UseListener(lc TCPUDPListener)
- type ListenConfig
- func (lc ListenConfig) Listen(network, addr string) (net.Listener, error)
- func (lc ListenConfig) ListenAll(network string, addrs []string) ([]net.Listener, error)
- func (lc ListenConfig) ListenAllPacket(network string, addrs []string) ([]net.PacketConn, error)
- func (lc ListenConfig) ListenAllTCP(network string, laddrs []*net.TCPAddr) ([]*net.TCPListener, error)
- func (lc ListenConfig) ListenAllUDP(network string, laddrs []*net.UDPAddr) ([]*net.UDPConn, error)
- func (lc ListenConfig) ListenPacket(network, addr string) (net.PacketConn, error)
- func (lc ListenConfig) ListenTCP(network string, laddr *net.TCPAddr) (*net.TCPListener, error)
- func (lc ListenConfig) ListenUDP(network string, laddr *net.UDPAddr) (*net.UDPConn, error)
- func (lc ListenConfig) WithUpgrader(upg Upgrader) *UpgraderListenConfig
- type Listener
- type TCPListener
- type TCPUDPListener
- type UDPListener
- type Upgrader
- type UpgraderListenConfig
- func (lu UpgraderListenConfig) Listen(network, addr string) (net.Listener, error)
- func (lu UpgraderListenConfig) ListenAll(network string, addrs []string) ([]net.Listener, error)
- func (lu UpgraderListenConfig) ListenAllPacket(network string, addrs []string) ([]net.PacketConn, error)
- func (lu UpgraderListenConfig) ListenAllTCP(network string, laddrs []*net.TCPAddr) ([]*net.TCPListener, error)
- func (lu UpgraderListenConfig) ListenAllUDP(network string, laddrs []*net.UDPAddr) ([]*net.UDPConn, error)
- func (lu UpgraderListenConfig) ListenPacket(network, addr string) (net.PacketConn, error)
- func (lu UpgraderListenConfig) ListenTCP(network string, laddr *net.TCPAddr) (*net.TCPListener, error)
- func (lu UpgraderListenConfig) ListenUDP(network string, laddr *net.UDPAddr) (*net.UDPConn, error)
Constants ¶
const ( // DefaultPortAttempts indicates how many times we will try binding a port DefaultPortAttempts = 4 // DefaultMaxRecvBufferSize indicates the receive buffer size of UDP listeners DefaultMaxRecvBufferSize = 2 * 1024 * 1024 // MinimumMaxRecvBufferSize indicates the minimum receive buffer size of UDP listeners // If a lower value is given, it will be reset to DefaultMaxRecvBufferSize MinimumMaxRecvBufferSize = 32 )
Variables ¶
This section is empty.
Functions ¶
func AddrPortSliceTCPListener ¶
func AddrPortSliceTCPListener(s []*net.TCPListener) ([]netip.AddrPort, bool)
AddrPortSliceTCPListener attempts to extract netip.AddrPort from a slice of *net.TCPListener, it also confirms if all entries were converted
func AddrPortSliceUDPConn ¶
AddrPortSliceUDPConn attempts to extract netip.AddrPort from a slice of *net.UDPConn, it also confirms if all entries were converted
func Bind ¶
Bind attempts to listen all addresses specified by the given configuration. TCP and UDP on the same port for all.
func IPAddresses ¶
IPAddresses extract all valid unique addresses on a slice, and if all were unique and valid
Types ¶
type AllListener ¶
type AllListener interface { ListenAll(network string, addr []string) ([]net.Listener, error) ListenAllPacket(network string, addr []string) ([]net.PacketConn, error) }
AllListener is equivalent to Listener but takes an array of addresses
type AllTCPListener ¶
type AllTCPListener interface {
ListenAllTCP(network string, ladders []*net.TCPAddr) ([]*net.TCPListener, error)
}
AllTCPListener is equivalent to TCPListener but takes an array of addresses
type AllUDPListener ¶
type AllUDPListener interface {
ListenAllUDP(network string, ladders []*net.UDPAddr) ([]*net.UDPConn, error)
}
AllUDPListener is equivalent to UDPListener but takes an array of addresses
type Config ¶
type Config struct { // Interface is the list of interfaces to listen on Interfaces []string // Addresses is the list of addresses to listen on Addresses []string // Port is the port to listen on, for both TCP and UDP Port uint16 // PortStrict tells us not to try other ports PortStrict bool // PortAttempts indicates how many times we will try finding a port PortAttempts int // Defaultport indicates the port to try on the first attempt if Port is zero DefaultPort uint16 // OnlyTCP tells Bind to skip listening UDP ports OnlyTCP bool // OnlyUDP tells Bind to skip listening TCP ports OnlyUDP bool // ListenTCP is the helper to use to listen on TCP ports ListenTCP func(network string, laddr *net.TCPAddr) (*net.TCPListener, error) // ListenUDP is the helper to use to listen on UDP ports ListenUDP func(network string, laddr *net.UDPAddr) (*net.UDPConn, error) // MaxRecvBufferSize is the buffer size we will attempt to set to // UDP listeners MaxRecvBufferSize int }
Config is the configuration for Bind()
func (*Config) Bind ¶
Bind attempts to listen all specified addresses. TCP and UDP on the same port for all.
func (*Config) Refresh ¶
Refresh attempts to update a Config based on a given slice of netip.AddrPort corresponding to the listeners
func (*Config) RefreshFromTCPListeners ¶
func (cfg *Config) RefreshFromTCPListeners(tcpListeners []*net.TCPListener) bool
RefreshFromTCPListeners attempts to update a Config based on a given slice of *net.TCPListener
func (*Config) RefreshFromUDPConn ¶
RefreshFromUDPConn attempts to update a Config based on a given slice of *net.UDPConn
func (*Config) SetDefaults ¶
SetDefaults attempts to fill any configuration gap, specially the IP Addresses when interfaces are provided instead
func (*Config) UseListener ¶
func (cfg *Config) UseListener(lc TCPUDPListener)
UseListener sets Bind's Config to use the provided ListenConfig
type ListenConfig ¶
type ListenConfig struct { net.ListenConfig // Context used when registering the listeners Context context.Context }
ListenConfig extends the standard net.ListeConfig with a central holder for the Context bound to the listeners
func NewListenConfig ¶
func NewListenConfig(ctx context.Context, keepalive time.Duration) *ListenConfig
NewListenConfig assists creating a ListenConfig due to the two-layer definition making difficult static declaration when `net` is shadowed
func (ListenConfig) Listen ¶
func (lc ListenConfig) Listen(network, addr string) (net.Listener, error)
Listen acts like the standard net.Listen but using the context.Context, KeepAlive, and optional Control function from our ListenConfig struct
func (ListenConfig) ListenAllPacket ¶
func (lc ListenConfig) ListenAllPacket(network string, addrs []string) ([]net.PacketConn, error)
ListenAllPacket acts like ListenPacket but on a list of addresses
func (ListenConfig) ListenAllTCP ¶
func (lc ListenConfig) ListenAllTCP(network string, laddrs []*net.TCPAddr) ( []*net.TCPListener, error)
ListenAllTCP acts like ListenTCP but on a list of addresses
func (ListenConfig) ListenAllUDP ¶
ListenAllUDP acts like ListenUDP but on a list of addresses
func (ListenConfig) ListenPacket ¶
func (lc ListenConfig) ListenPacket(network, addr string) (net.PacketConn, error)
ListenPacket acts like the standard net.ListenPacket but using the context.Context, KeepAlive, and optional Control function from our ListenConfig struct
func (ListenConfig) ListenTCP ¶
func (lc ListenConfig) ListenTCP(network string, laddr *net.TCPAddr) (*net.TCPListener, error)
ListenTCP acts like the standard net.ListenTCP but using the context.Context, KeepAlive, and optional Control function from our ListenConfig struct
func (ListenConfig) ListenUDP ¶
ListenUDP acts like the standard net.ListenUDP but using the context.Context, KeepAlive, and optional Control function from our ListenConfig struct
func (ListenConfig) WithUpgrader ¶
func (lc ListenConfig) WithUpgrader(upg Upgrader) *UpgraderListenConfig
WithUpgrader creates a new ListenConfig using the provided Upgrader
type Listener ¶
type Listener interface { Listen(network, addr string) (net.Listener, error) ListenPacket(network, addr string) (net.PacketConn, error) }
Listener mimics net.ListenConfig providing a configuration context for net.Listen() and net.ListenPacket() alternatives
type TCPListener ¶
type TCPListener interface {
ListenTCP(network string, laddr *net.TCPAddr) (*net.TCPListener, error)
}
TCPListener provides a context-aware alternative to net.ListenTCP
type TCPUDPListener ¶
type TCPUDPListener interface { TCPListener UDPListener }
TCPUDPListener provides the callbacks used by Bind(). ListenTCP() and ListenUDP()
type UDPListener ¶
UDPListener provides a context-aware alternative to net.ListenUDP
type Upgrader ¶
type Upgrader interface { ListenWithCallback(network, addr string, callback func(network, addr string) (net.Listener, error)) (net.Listener, error) ListenPacketWithCallback(network, addr string, callback func(netowkr, addr string) (net.PacketConn, error)) (net.PacketConn, error) }
Upgrader represents a tool that keep account of listening ports but allows us to provide our own helper to do the last step
type UpgraderListenConfig ¶
type UpgraderListenConfig struct {
// contains filtered or unexported fields
}
UpgraderListenConfig represents an object equivalent to a ListenConfig but using a Upgrader in the middle
func (UpgraderListenConfig) Listen ¶
func (lu UpgraderListenConfig) Listen(network, addr string) (net.Listener, error)
Listen acts like the standard net.Listen but using our ListenConfig and via an Upgrader tool
func (UpgraderListenConfig) ListenAllPacket ¶
func (lu UpgraderListenConfig) ListenAllPacket(network string, addrs []string) ( []net.PacketConn, error)
ListenAllPacket acts like ListenPacket but on a list of addresses
func (UpgraderListenConfig) ListenAllTCP ¶
func (lu UpgraderListenConfig) ListenAllTCP(network string, laddrs []*net.TCPAddr) ( []*net.TCPListener, error)
ListenAllTCP acts like ListenTCP but on a list of addresses
func (UpgraderListenConfig) ListenAllUDP ¶
func (lu UpgraderListenConfig) ListenAllUDP(network string, laddrs []*net.UDPAddr) ( []*net.UDPConn, error)
ListenAllUDP acts like ListenUDP but on a list of addresses
func (UpgraderListenConfig) ListenPacket ¶
func (lu UpgraderListenConfig) ListenPacket(network, addr string) (net.PacketConn, error)
ListenPacket acts like the standard net.ListenPacket but using our ListenConfig and via an Upgrader tool
func (UpgraderListenConfig) ListenTCP ¶
func (lu UpgraderListenConfig) ListenTCP(network string, laddr *net.TCPAddr) ( *net.TCPListener, error)
ListenTCP acts like the standard net.ListenTCP but using our ListenConfig and the UpgraderListen