packet

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package packet provides convenience methods to read/write packets to a net.Conn. A selection of different functions are available to specify timeouts and to provide either raw bytes or custom values that are en-/decoded with custom codec.Codecs.

Packets

A packet is a very simple and efficient network data format used to transmit data over a network. It consists of two parts, a header and the payload.

The header must be present and has a fixed size of 4 bytes. It only contains the size of the payload.

The payload is of variable size (as indicated by the header) and is a stream of bytes. The maximum possible size is restricted by the available bytes for the header, and is therefore currently 2^32 - 1.

In case an empty payload should be transmitted, the packet consists of solely a header with value 0.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Returned, if the size of a payload exceeds the maxPayloadSize.
	ErrMaxPayloadSizeExceeded = errors.New("max payload size exceeded")
)

Functions

func Read

func Read(
	conn io.Reader,
	buffer []byte,
	maxPayloadSize int,
) ([]byte, error)

Read reads a packet from the connection, without setting a timeout, and returns the raw bytes of it.

A maximum size for the packet must be specified. If the packet's size exceeds it, an ErrMaxPayloadSizeExceeded is returned. A negative maxPayloadSize causes a panic.

If buffer is set and is big enough to fit the packet, then the buffer is used. Otherwise a new buffer is allocated. Returns an empty byte slice when no data was send.

func ReadDecode

func ReadDecode(
	conn net.Conn,
	value interface{},
	codec codec.Codec,
	maxPayloadSize int,
	timeout time.Duration,
) (err error)

ReadDecode reads the packet from the connection using ReadTimeout() and decodes it into the value, using the provided codec. Ensure to pass a pointer value.

func ReadTimeout

func ReadTimeout(
	conn net.Conn,
	buffer []byte,
	maxPayloadSize int,
	timeout time.Duration,
) ([]byte, error)

ReadTimeout performs the same task as Read(), but allows to specify a timeout for reading from the connection. If the deadline is not met, the read is terminated and a net.Error with Timeout() == true is returned.

func Write

func Write(
	conn io.Writer,
	data []byte,
	maxPayloadSize int,
) (err error)

Write writes the packet data to the connection, without setting a timeout.

If data is empty, an empty packet is sent that consists of a header with payload size 0 and no payload.

A maximum size for the packet must be specified. If the packet's size exceeds it, an ErrMaxPayloadSizeExceeded is returned. A negative maxPayloadSize causes a panic.

func WriteEncode

func WriteEncode(
	conn net.Conn,
	value interface{},
	codec codec.Codec,
	maxPayloadSize int,
	timeout time.Duration,
) (err error)

WriteEncode encodes the value using the provided codec and writes it to the connection using WriteTimeout().

func WriteTimeout

func WriteTimeout(
	conn net.Conn,
	data []byte,
	maxPayloadSize int,
	timeout time.Duration,
) (err error)

WriteTimeout performs the same task as Write(), but allows to specify a timeout for reading from the connection. If the deadline is not met, the read is terminated and a net.Error with Timeout() == true is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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