packet

package
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: May 25, 2022 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.MaxInt32
	NoPayloadSizeLimit = -1
)

Variables

View Source
var (
	ErrZeroData               = errors.New("zero data")
	ErrMaxPayloadSizeExceeded = errors.New("maximum payload size exceeded")
)

Functions

func Read

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

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

A maxPayloadSize of 0 specifies no limit. A maxPayloadSize of -1 (NoPayloadSizeLimit) specifies no limit.

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. Returns ErrMaxPayloadSizeExceeded if the payload size is exceeded.

func ReadDecode

func ReadDecode(conn net.Conn, value interface{}, codec codec.Codec, maxPayloadSize int) 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, maxPayloadSize int) 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 maxPayloadSize of 0 specifies no limit. A maxPayloadSize of -1 (NoPayloadSizeLimit) specifies no limit.

Returns ErrMaxPayloadSizeExceeded if the payload size is exceeded.

func WriteEncode

func WriteEncode(conn net.Conn, value interface{}, codec codec.Codec, maxPayloadSize int) 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