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
- Variables
- func Read(conn net.Conn, buffer []byte, maxPayloadSize int) ([]byte, error)
- func ReadDecode(conn net.Conn, value interface{}, codec codec.Codec, maxPayloadSize int) error
- func Write(conn net.Conn, data []byte, maxPayloadSize int) error
- func WriteEncode(conn net.Conn, value interface{}, codec codec.Codec, maxPayloadSize int) error
Constants ¶
const ( MaxSize = math.MaxUint32 NoPayloadSizeLimit = -1 )
Variables ¶
var ( ErrZeroData = errors.New("zero data") ErrMaxPayloadSizeExceeded = errors.New("maximum payload size exceeded") )
Functions ¶
func Read ¶
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 ¶
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 ¶
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.
Types ¶
This section is empty.