rnet: github.com/dist-ribut-us/rnet Index | Files

package rnet

import "github.com/dist-ribut-us/rnet"

Index

Package Files

inc.go rnet.go server.go

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

func GetLocalIPs Uses

func GetLocalIPs() []string

GetLocalIPs returns all local IP addresses that are not loopback addresses

type Addr Uses

type Addr struct {
    *net.UDPAddr
    Err error
}

Addr wraps net.UDPAddr and adds additional useful methods

func NewAddr Uses

func NewAddr(ip []byte, port int, zone string) *Addr

NewAddr creates an address from the primatives in net.UDPAddr

func ResolveAddr Uses

func ResolveAddr(addr string) (*Addr, error)

ResolveAddr takes a string and returns an Addr

func (*Addr) GetPort Uses

func (a *Addr) GetPort() Port

GetPort returns the port of an address

func (*Addr) Marshal Uses

func (a *Addr) Marshal() ([]byte, error)

Marshal an address to a byte slice

func (*Addr) String Uses

func (a *Addr) String() string

String returns the address as IP:Port

func (*Addr) Unmarshal Uses

func (a *Addr) Unmarshal(b []byte) error

Unmarshal address from byte slice

type PacketHandler Uses

type PacketHandler interface {
    Receive([]byte, *Addr)
}

PacketHandler is an interface for receiving packets from a UDP server

type Port Uses

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 Uses

func RandomPort() Port

RandomPort picks a random port number between 1000 and 65534 (inclusive)

func (Port) Addr Uses

func (p Port) Addr() *Addr

Addr returns the port as an *Addr

func (Port) GetPort Uses

func (p Port) GetPort() Port

GetPort fulfills the Porter interface

func (Port) Local Uses

func (p Port) Local() *Addr

Local returns the port as an *Addr on 127.0.0.1

func (Port) On Uses

func (p Port) On(ip string) *Addr

On returns a reference to the port on the given ip as an address

func (Port) RawStr Uses

func (p Port) RawStr() string

RawStr return the port as string

func (Port) String Uses

func (p Port) String() string

String return the port as string starting with :

type PortIncrementer Uses

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

PortIncrementer provides a threadsafe way to return an incrementing port value.

func NewPortIncrementer Uses

func NewPortIncrementer(start uint16) *PortIncrementer

NewPortIncrementer returns a PortIncrementer. The first value returned will be one greater than start.

func (*PortIncrementer) Next Uses

func (p *PortIncrementer) Next() Port

Next returnst the next port

type Porter Uses

type Porter interface {
    GetPort() Port
}

Porter is fulfilled by describing a port

type Server Uses

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 Uses

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 Uses

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) Close Uses

func (s *Server) Close() error

Close will close the connection, freeing the port

func (*Server) GetPort Uses

func (s *Server) GetPort() Port

GetPort returns the port the server is listening on

func (*Server) IsOpen Uses

func (s *Server) IsOpen() bool

IsOpen returns true if the connection is open. If the server is closed, it can neither send nor receive

func (*Server) IsRunning Uses

func (s *Server) IsRunning() bool

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 Uses

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 Uses

func (s *Server) Send(packet []byte, addr *Addr) error

Send will send a single packe (byte slice) to an address just a wrapper around WriteToUDP

func (*Server) SendAll Uses

func (s *Server) SendAll(packets [][]byte, addr *Addr) (errs []error)

SendAll sends a slice of packets (byte slices) to an address this will return the last error it encoutered, if it encountered any

func (*Server) Stop Uses

func (s *Server) Stop() error

Stop will stop the server

Package rnet imports 9 packages (graph) and is imported by 10 packages. Updated 2019-07-28. Refresh now. Tools for package owners.