packet

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2020 License: MIT Imports: 5 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 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

View Source
const (
	MaxSize = math.MaxUint32
)

Variables

View Source
var ErrZeroData = errors.New("zero data")

Functions

func Read

func Read(conn net.Conn, buffer []byte) ([]byte, error)

Read reads a packet from the connection and returns the raw bytes of it.

A timeout must be specified, where a timeout <= 0 means no timeout.

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. Returns ErrZeroData, if a packet with length 0 is received.

func ReadDecode

func ReadDecode(conn net.Conn, value interface{}, codec codec.Codec) error

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

func Write

func Write(conn net.Conn, data []byte) 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 timeout must be specified, where a timeout <= 0 means no timeout.

func WriteEncode

func WriteEncode(conn net.Conn, value interface{}, codec codec.Codec) error

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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