Public Member Functions

kNet::DataDeserializer Class Reference

DataDeserializer is an utility class that walks through and deserializes data in a stream of raw bytes. More...

#include <DataDeserializer.h>

List of all members.

Public Member Functions

 DataDeserializer (const char *data, size_t size)
 Constructs a DataDeserializer that reads its data from the given buffer.
 DataDeserializer (const char *data, size_t size, const SerializedMessageDesc *msgTemplate)
 Constructs a DataDeserializer that reads its data from the given buffer.
void ResetTraversal ()
 Moves the bit offset position counter to the beginning of the data buffer.
template<typename T >
Read ()
 Deserializes a single value of type T off the stream and advances the internal read offset.
template<typename VLEType >
u32 ReadVLE ()
 Reads a variable-length encoded integer off the stream and advances the internal read offset.
template<typename T >
void ReadArray (T *dst, size_t numElems)
 Deserializes an array of values of type T off the stream and advances the internal read offset.
std::string ReadString ()
 Reads an ASCII string from the stream.
u32 ReadBits (int numBits)
 Reads the given amount of bits and packs them into a u32, which is returned.
u32 GetDynamicElemCount ()
 Need to have a message template to use this function.
u32 BytesLeft () const
u32 BitsLeft () const
u32 BytePos () const
u32 BitPos () const
size_t BitsReadTotal () const
const char * CurrentData () const
void SkipBits (size_t numBits)
 Advances the read pointer with the given amount of bits. Can only be used in nontemplate read mode.
void SkipBytes (size_t numBytes)
 Advances the read pointer with the given amount of bytes. Can only be used in nontemplate read mode.

Detailed Description

DataDeserializer is an utility class that walks through and deserializes data in a stream of raw bytes.

The stream itself does not contain information about what types of data is contained within, but the user of DataDeserializer must know the contents of the data. DataDeserializer never copies the data it is given to read into an internal memory buffer, but instead it reads the given existing memory buffers. DataDeserializer maintains an internal bit offset position to keep track the position that is currently being read.


Constructor & Destructor Documentation

kNet::DataDeserializer::DataDeserializer ( const char *  data,
size_t  size 
)

Constructs a DataDeserializer that reads its data from the given buffer.

DataDeserializer will not copy the contents of the buffer to its own memory area, so be sure to keep the data alive and unmoved for the duration DataDeserializer exists.

References ResetTraversal().

kNet::DataDeserializer::DataDeserializer ( const char *  data,
size_t  size,
const SerializedMessageDesc msgTemplate 
)

Constructs a DataDeserializer that reads its data from the given buffer.

DataDeserializer will not copy the contents of the buffer to its own memory area, so be sure to keep the data alive and unmoved for the duration DataDeserializer exists.

Parameters:
msgTemplate A pointer to an existing message template structure, which is used to validate that deserialization of the data proceeds in the defined order. DataDeserializer does not make a copy of this description, but dereferences it directly. Be sure to keep it alive for the duration that DataDeserializer exists. Do not pass in a zero pointer here.

References ResetTraversal().


Member Function Documentation

template<typename T >
void kNet::DataDeserializer::ReadArray ( T *  dst,
size_t  numElems 
)

Deserializes an array of values of type T off the stream and advances the internal read offset.

Parameters:
dst [out] Pointer to an array to receive the read data.
numElems The number of elements to read. The array dst must be able to hold that many elements.
std::string kNet::DataDeserializer::ReadString (  ) 

Reads an ASCII string from the stream.

If we are using a template, the template tells how the string is stored. Otherwise, we are assuming the string was stored length-prepended, using a single byte to denote the length (no null padding). Use the 's8' data type with dynamicCount set in the message template description for a string variable. The returned string will only contain ascii values in the range [32, 253], 0x0D, 0x0A, 0x09. Other values will be replaced with a space bar character (0x20). Because of this string validation method, do not use this function to extract binary data of any kind (base64-encoded is fine).

References BitsLeft(), and GetDynamicElemCount().

u32 kNet::DataDeserializer::ReadBits ( int  numBits  ) 

Reads the given amount of bits and packs them into a u32, which is returned.

Parameters:
numBits the number of bits to read, [1, 32].

Referenced by ReadVLE().

u32 kNet::DataDeserializer::BytesLeft (  )  const [inline]
Returns:
The number of bytes left in the stream to read.

Referenced by kNet::MessageConnection::HandleInboundMessage().

u32 kNet::DataDeserializer::BitsLeft (  )  const [inline]
Returns:
The number of bits left in the stream to read.

Referenced by GetDynamicElemCount(), ReadString(), and ReadVLE().

u32 kNet::DataDeserializer::BytePos (  )  const [inline]
Returns:
The current byte of the stream that will be read next.

Referenced by kNet::MessageConnection::HandleInboundMessage().

u32 kNet::DataDeserializer::BitPos (  )  const [inline]
Returns:
The bit index, [0, 7] of the byte that will be read next. The reading will proceed from LSBit to MSBit, i.e. 0 -> 7.

Referenced by kNet::MessageConnection::HandleInboundMessage().

size_t kNet::DataDeserializer::BitsReadTotal (  )  const [inline]
Returns:
The number of bits read so far in total.
const char* kNet::DataDeserializer::CurrentData (  )  const [inline]
Returns:
A pointer in the byte stream at the current read position.

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines