Documentation ¶
Index ¶
- Constants
- func GetLocalIPs() []string
- type Addr
- type PacketHandler
- type Port
- type PortIncrementer
- type Porter
- type Server
- func (s *Server) Close() error
- func (s *Server) GetPort() Port
- func (s *Server) IsOpen() bool
- func (s *Server) IsRunning() bool
- func (s *Server) Run()
- func (s *Server) Send(packet []byte, addr *Addr) error
- func (s *Server) SendAll(packets [][]byte, addr *Addr) (errs []error)
- func (s *Server) Stop() error
Constants ¶
const ErrNilAddr = errors.String("Addr is nil")
ErrNilAddr is returned when trying to marshal a nil address.
const ErrWriteToNilUDP = errors.String("Cannot write to nil UDP connection")
ErrWriteToNilUDP is returned when trying to write a server after calling Close.
const MaxUDPPacketLength = 65507
MaxUDPPacketLength is the max possible length after the UDP headers are removed
Variables ¶
This section is empty.
Functions ¶
func GetLocalIPs ¶
func GetLocalIPs() []string
GetLocalIPs returns all local IP addresses that are not loopback addresses
Types ¶
type Addr ¶
Addr wraps net.UDPAddr and adds additional useful methods
func ResolveAddr ¶
ResolveAddr takes a string and returns an Addr
type PacketHandler ¶
PacketHandler is an interface for receiving packets from a UDP server
type Port ¶
type Port uint16
Port is a convenience as sometimes a port needs to be a number and sometimes it needs to be a string.
func RandomPort ¶
func RandomPort() Port
RandomPort picks a random port number between 1000 and 65534 (inclusive)
type PortIncrementer ¶
type PortIncrementer struct {
// contains filtered or unexported fields
}
PortIncrementer provides a threadsafe way to return an incrementing port value.
func NewPortIncrementer ¶
func NewPortIncrementer(start uint16) *PortIncrementer
NewPortIncrementer returns a PortIncrementer. The first value returned will be one greater than start.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a UDP server that can be used to send and receive UPD packets
func New ¶
func New(port Port, packetHandler PacketHandler) (*Server, error)
New creates a Server passing in ":0" for port will select any open port. It is also possible to specify a full IP address for port, as long as the address is local, but generally only a port is specified.
func RunNew ¶
func RunNew(port Port, packetHandler PacketHandler) (*Server, error)
RunNew is a wrapper around new that also calls Run in a Go routine if the server was created without error
func (*Server) IsOpen ¶
IsOpen returns true if the connection is open. If the server is closed, it can neither send nor receive
func (*Server) IsRunning ¶
IsRunning returns true if the server is running and can receive messages. Even if the server is not running, it can still send.
func (*Server) Run ¶
func (s *Server) Run()
Run is the servers listen loop. When it receives a message it will pass that message into the packetHandler
func (*Server) Send ¶
Send will send a single packe (byte slice) to an address just a wrapper around WriteToUDP