3es
0.7
|
A utility class for dealing with reading packets. More...
#include <3espacketreader.h>
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 > | |
PacketReader & | operator>> (T &val) |
![]() | |
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... | |
CrcType * | crcPtr () |
Fetch a pointer to the CRC bytes. More... | |
const CrcType * | crcPtr () 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 PacketHeader & | packet () 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 | |
![]() | |
enum | SeekPos |
Control values for seeking. | |
enum | Status |
Status bits. | |
typedef uint16_t | CrcType |
Defies the packet CRC type. | |
![]() | |
const PacketHeader & | _packet |
Packet header and buffer start address. | |
uint16_t | _status |
Status bits. | |
uint16_t | _payloadPosition |
Payload cursor. | |
A utility class for dealing with reading packets.
|
inline |
Returns the number of bytes available for writing in the payload.
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.
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.
bytes | Location to read into. |
elementSize | Size of a single array element to read. |
elementCount | The number of elements to attempt to read. |
Referenced by tes::ServerInfoMessage::read(), tes::ObjectAttributes::read(), and readElement().
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.
bytes | Location to read into. |
elementSize | Size of the data item being read at bytes . |
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().
|
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.
[out] | element | Set to the data read. |
sizeof(T)
on success, zero on failure. References readArray().
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.
bytes | Location to read into. byteCount Number of bytes to read. |
byteCount
if there are insufficient data available. Referenced by tes::CategoryNameMessage::read().