rnet

package module
v0.0.0-...-02346bf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2019 License: BSD-3-Clause Imports: 9 Imported by: 9

README

Dist-ribut-us Networking Utilities

Package to help with networking. Focused on UDP.

GoDoc

Documentation

Index

Constants

View Source
const ErrNilAddr = errors.String("Addr is nil")

ErrNilAddr is returned when trying to marshal a nil address.

View Source
const ErrWriteToNilUDP = errors.String("Cannot write to nil UDP connection")

ErrWriteToNilUDP is returned when trying to write a server after calling Close.

View Source
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

type Addr struct {
	*net.UDPAddr
	Err error
}

Addr wraps net.UDPAddr and adds additional useful methods

func NewAddr

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

NewAddr creates an address from the primatives in net.UDPAddr

func ResolveAddr

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

ResolveAddr takes a string and returns an Addr

func (*Addr) GetPort

func (a *Addr) GetPort() Port

GetPort returns the port of an address

func (*Addr) Marshal

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

Marshal an address to a byte slice

func (*Addr) String

func (a *Addr) String() string

String returns the address as IP:Port

func (*Addr) Unmarshal

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

Unmarshal address from byte slice

type PacketHandler

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

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)

func (Port) Addr

func (p Port) Addr() *Addr

Addr returns the port as an *Addr

func (Port) GetPort

func (p Port) GetPort() Port

GetPort fulfills the Porter interface

func (Port) Local

func (p Port) Local() *Addr

Local returns the port as an *Addr on 127.0.0.1

func (Port) On

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

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

func (Port) RawStr

func (p Port) RawStr() string

RawStr return the port as string

func (Port) String

func (p Port) String() string

String return the port as string starting with :

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.

func (*PortIncrementer) Next

func (p *PortIncrementer) Next() Port

Next returnst the next port

type Porter

type Porter interface {
	GetPort() Port
}

Porter is fulfilled by describing a port

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

func (s *Server) Close() error

Close will close the connection, freeing the port

func (*Server) GetPort

func (s *Server) GetPort() Port

GetPort returns the port the server is listening on

func (*Server) IsOpen

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

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

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

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

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

func (s *Server) Stop() error

Stop will stop the server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL