transport

package
v1.9.25 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2021 License: Unlicense Imports: 17 Imported by: 3

Documentation

Overview

Package transport provides a listener and sender channel for unicast and multicast UDP IPv4 short message chat protocol with a pre shared key, forward error correction facilities with a nice friendly declaration syntax

Index

Constants

View Source
const (
	UDPMulticastAddress = "224.0.0.1"

	DefaultPort = 11049
)

Variables

View Source
var DefaultIP = net.IPv4(224, 0, 0, 1)
View Source
var F, E, W, I, D, T logg.LevelPrinter = logg.GetLogPrinterSet(subsystem)
View Source
var MulticastAddress = &net.UDPAddr{IP: DefaultIP, Port: DefaultPort}

Functions

func DecryptMessage added in v1.9.24

func DecryptMessage(creator string, ciph cipher.AEAD, data []byte) (msg []byte, e error)

DecryptMessage attempts to decode the received message

func EncryptMessage added in v1.9.24

func EncryptMessage(creator string, ciph cipher.AEAD, magic []byte, nonce, data []byte) (msg []byte, e error)

EncryptMessage encrypts a message, if the nonce is given it uses that otherwise it generates a new one. If there is no cipher this just returns a message with the given magic prepended.

func GetNonce added in v1.9.24

func GetNonce(ciph cipher.AEAD) (nonce []byte, e error)

GetNonce reads from a cryptographicallly secure random number source

func GetShards added in v1.9.24

func GetShards(data []byte) (shards [][]byte)

GetShards returns a buffer iterator to feed to Channel.SendMany containing fec encoded shards built from the provided buffer

func Handle added in v1.9.24

func Handle(
	address string, channel *Channel,
	handlers Handlers, maxDatagramSize int, quit qu.C,
)

Handle listens for messages, decodes them, aggregates them, recovers the data from the reed solomon fec shards received and invokes the handler provided matching the magic on the complete received messages

func Listen added in v1.9.24

func Listen(
	address string, channel *Channel, maxDatagramSize int, handlers Handlers,
	quit qu.C,
) (conn *net.UDPConn, e error)

Listen binds to the UDP Address and port given and writes packets received from that Address to a buffer which is passed to a handler

func ListenBroadcast added in v1.9.24

func ListenBroadcast(
	port int,
	channel *Channel,
	maxDatagramSize int,
	handlers Handlers,
	quit qu.C,
) (conn *net.UDPConn, e error)

ListenBroadcast binds to the UDP Address and port given and writes packets received from that Address to a buffer which is passed to a handler

func NewBroadcaster added in v1.9.24

func NewBroadcaster(port int, maxDatagramSize int) (conn *net.UDPConn, e error)

NewBroadcaster creates a new UDP multicast connection on which to broadcast

func NewSender added in v1.9.24

func NewSender(address string, maxDatagramSize int) (conn *net.UDPConn, e error)

NewSender creates a new UDP connection to a specified address

func PrevCallers added in v1.9.24

func PrevCallers() (out string)

Types

type Channel added in v1.9.24

type Channel struct {
	Ready qu.C

	Creator string

	MaxDatagramSize int

	Receiver *net.UDPConn

	Sender *net.UDPConn
	// contains filtered or unexported fields
}

func NewBroadcastChannel added in v1.9.24

func NewBroadcastChannel(
	creator string, ctx interface{}, key string, port int, maxDatagramSize int, handlers Handlers,
	quit qu.C,
) (channel *Channel, e error)

NewBroadcastChannel returns a broadcaster and listener with a given handler on a multicast address and specified port. The handlers define the messages that will be processed and any other messages are ignored

func NewUnicastChannel added in v1.9.24

func NewUnicastChannel(
	creator string, ctx interface{}, key, sender, receiver string, maxDatagramSize int,
	handlers Handlers, quit qu.C,
) (channel *Channel, e error)

NewUnicastChannel sets up a listener and sender for a specified destination

func (*Channel) Close added in v1.9.24

func (c *Channel) Close() (e error)

Close the multicast

func (*Channel) Send added in v1.9.24

func (c *Channel) Send(magic []byte, nonce []byte, data []byte) (n int, e error)

Send fires off some data through the configured multicast's outbound.

func (*Channel) SendMany added in v1.9.24

func (c *Channel) SendMany(magic []byte, b [][]byte) (e error)

SendMany sends a BufIter of shards as produced by GetShards

func (*Channel) SetDestination added in v1.9.24

func (c *Channel) SetDestination(dst string) (e error)

SetDestination changes the address the outbound connection of a multicast directs to

type Connection

type Connection struct {
	SendConn net.Conn
	// contains filtered or unexported fields
}

Connection is the state and working memory references for a simple reliable UDP lan transport, encrypted by a GCM AES cipher, with the simple protocol of sending out 9 packets containing encrypted FEC shards containing a slice of bytes.

This protocol probably won't work well outside of a multicast lan in adverse conditions but it is designed for local network control systems todo: it is if the updated fec segmenting code is put in

func (*Connection) CreateShards

func (c *Connection) CreateShards(b, magic []byte) (
	shards [][]byte,
	e error,
)

CreateShards takes a slice of bites and generates 3

func (*Connection) Listen

func (c *Connection) Listen(handlers HandleFunc, ifc interface{}, lastSent *time.Time, firstSender *string) (e error)

Listen runs a goroutine that collects and attempts to decode the FEC shards once it has enough intact pieces

func (*Connection) Send

func (c *Connection) Send(b, magic []byte) (e error)

func (*Connection) SendShards

func (c *Connection) SendShards(shards [][]byte) (e error)

func (*Connection) SendShardsTo

func (c *Connection) SendShardsTo(shards [][]byte, addr *net.UDPAddr) (e error)

func (*Connection) SendTo

func (c *Connection) SendTo(addr *net.UDPAddr, b, magic []byte) (e error)

func (*Connection) SetSendConn

func (c *Connection) SetSendConn(ad string) (e error)

SetSendConn sets up an outbound connection

type HandleFunc

type HandleFunc map[string]func(ctx interface{}) func(b []byte) (e error)

HandleFunc is a map of handlers for working on received, decoded packets

type HandlerFunc added in v1.9.24

type HandlerFunc func(ctx interface{}, src net.Addr, dst string, b []byte) (e error)

HandlerFunc is a function that is used to process a received message

type Handlers added in v1.9.24

type Handlers map[string]HandlerFunc

type MsgBuffer

type MsgBuffer struct {
	Buffers [][]byte
	First   time.Time
	Decoded bool
	Source  net.Addr
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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