3es  0.7
tes::PacketReader Class Reference

A utility class for dealing with reading packets. More...

#include <3espacketreader.h>

Inheritance diagram for tes::PacketReader:
tes::PacketStream< const PacketHeader >

Public Member Functions

 PacketReader (const PacketHeader &packet)
 Creates a new packet reader for the given packet and its CRC.
 
bool checkCrc ()
 Calculates the CRC value, returning true if it matches. More...
 
CrcType calculateCrc () const
 Caluclates the CRC for the packet.
 
uint16_t bytesAvailable () const
 Returns the number of bytes available for writing in the payload. More...
 
size_t readElement (uint8_t *bytes, size_t elementSize)
 Reads a single data element from the current position. More...
 
size_t readArray (uint8_t *bytes, size_t elementSize, size_t elementCount)
 Reads an array of data items from the current position. More...
 
size_t readRaw (uint8_t *bytes, size_t byteCount)
 Reads raw bytes from the packet at the current position up to byteCount. More...
 
template<typename T >
size_t readElement (T &element)
 Reads a single data item from the packet. More...
 
template<typename T >
size_t readArray (T *elements, size_t elementCount)
 
template<typename T >
PacketReaderoperator>> (T &val)
 
- Public Member Functions inherited from tes::PacketStream< const PacketHeader >
 PacketStream (const PacketHeader &packet)
 Create a stream to read from beginning at packet. More...
 
uint32_t marker () const
 Fetch the marker bytes in local endian. More...
 
uint16_t versionMajor () const
 Fetch the major version bytes in local endian. More...
 
uint16_t versionMinor () const
 Fetch the minor version bytes in local endian. More...
 
uint16_t payloadSize () const
 Fetch the payload size bytes in local endian. More...
 
uint16_t packetSize () const
 Returns the size of the packet plus payload, giving the full data packet size including the CRC. More...
 
uint16_t routingId () const
 Fetch the routing ID bytes in local endian. More...
 
uint16_t messageId () const
 Fetch the message ID bytes in local endian. More...
 
CrcType crc () const
 Fetch the CRC bytes in local endian. More...
 
CrcTypecrcPtr ()
 Fetch a pointer to the CRC bytes. More...
 
const CrcTypecrcPtr () const
 
uint16_t status () const
 Report the Status bits. More...
 
bool isEop () const
 At end of packet/stream? More...
 
bool isOk () const
 Status OK? More...
 
bool isFail () const
 Fail bit set? More...
 
bool isReadOnly () const
 Read only stream? More...
 
bool isCrcValid () const
 CRC validated? More...
 
const PacketHeaderpacket () const
 Access the head of the packet buffer, for direct PacketHeader access. More...
 
uint16_t tell () const
 Tell the current stream position. More...
 
bool seek (int offset, SeekPos pos=Begin)
 Seek to the indicated position. More...
 
const uint8_t * payload () const
 Direct payload pointer access. More...
 

Additional Inherited Members

- Public Types inherited from tes::PacketStream< const PacketHeader >
enum  SeekPos
 Control values for seeking.
 
enum  Status
 Status bits.
 
typedef uint16_t CrcType
 Defies the packet CRC type.
 
- Protected Attributes inherited from tes::PacketStream< const PacketHeader >
const PacketHeader_packet
 Packet header and buffer start address.
 
uint16_t _status
 Status bits.
 
uint16_t _payloadPosition
 Payload cursor.
 

Detailed Description

A utility class for dealing with reading packets.

Bug:
Use the payloadOffset in various calculations herein. It was added after this class was written, but is currently only supported as being zero, so it's not an issue yet.

Member Function Documentation

◆ bytesAvailable()

uint16_t tes::PacketReader::bytesAvailable ( ) const
inline

Returns the number of bytes available for writing in the payload.

Returns
The number of bytes available for writing.

◆ checkCrc()

bool tes::PacketReader::checkCrc ( )

Calculates the CRC value, returning true if it matches.

This also sets isCrcValid() on success.

Returns true immediately when isCrcValid() is already set.

Returns
True if the CRC is valid.

◆ readArray()

size_t tes::PacketReader::readArray ( uint8_t *  bytes,
size_t  elementSize,
size_t  elementCount 
)

Reads an array of data items from the current position.

This makes the same assumptions as readElement() and performs an endian swap per array element. Elements in the array are assumed to be contiguous in both source and destination locations.

Up to elementCount elements will be read depending on availability. Less may be read, but on success the number of bytes read will be a multiple of elementSize.

The reader position is advanced by the number of bytes read. Does not set the Fail bit on failure.

Parameters
bytesLocation to read into.
elementSizeSize of a single array element to read.
elementCountThe number of elements to attempt to read.
Returns
On success returns the number of whole elements read.

Referenced by tes::ServerInfoMessage::read(), tes::ObjectAttributes::read(), and readElement().

◆ readElement() [1/2]

size_t tes::PacketReader::readElement ( uint8_t *  bytes,
size_t  elementSize 
)

Reads a single data element from the current position.

This assumes that a single data element of size elementSize is being read and may require an endian swap to the current platform endian.

The reader position is advanced by elementSize. Does not set the Fail bit on failure.

Parameters
bytesLocation to read into.
elementSizeSize of the data item being read at bytes.
Returns
elementSize on success, 0 otherwise.

Referenced by tes::MeshCreateMessage::read(), tes::ServerInfoMessage::read(), tes::MeshDestroyMessage::read(), tes::ControlMessage::read(), tes::MeshComponentMessage::read(), tes::CategoryNameMessage::read(), tes::Material::read(), tes::MeshFinaliseMessage::read(), tes::CollatedPacketMessage::read(), tes::ObjectAttributes::read(), tes::CreateMessage::read(), tes::DataMessage::read(), tes::UpdateMessage::read(), and tes::DestroyMessage::read().

◆ readElement() [2/2]

template<typename T >
size_t tes::PacketReader::readElement ( T &  element)
inline

Reads a single data item from the packet.

This reads a number of bytes equal to sizeof(T) performing an endian swap if necessary.

Parameters
[out]elementSet to the data read.
Returns
sizeof(T) on success, zero on failure.

References readArray().

◆ readRaw()

size_t tes::PacketReader::readRaw ( uint8_t *  bytes,
size_t  byteCount 
)

Reads raw bytes from the packet at the current position up to byteCount.

No endian swap is performed on the data read.

The reader position is advanced by byteCount. Does not set the Fail bit on failure.

Parameters
bytesLocation to read into. byteCount Number of bytes to read.
Returns
The number of bytes read. This may be less than byteCount if there are insufficient data available.

Referenced by tes::CategoryNameMessage::read().


The documentation for this class was generated from the following file: