CUGL 2.1
Cornell University Game Library
|
#include <CUNetworkSerializer.h>
Public Types | |
typedef std::variant< std::monostate, bool, float, double, Uint32, Uint64, Sint32, Sint64, std::string, std::shared_ptr< JsonValue >, std::vector< bool >, std::vector< float >, std::vector< double >, std::vector< Uint32 >, std::vector< Uint64 >, std::vector< Sint32 >, std::vector< Sint64 >, std::vector< std::string >, std::vector< std::shared_ptr< JsonValue > > > | Message |
Public Member Functions | |
NetworkDeserializer () | |
void | receive (const std::vector< uint8_t > &msg) |
Message | read () |
bool | available () const |
NetworkType | nextType () const |
bool | readBool () |
float | readFloat () |
double | readDouble () |
Uint32 | readUint32 () |
Sint32 | readSint32 () |
Uint64 | readUint64 () |
Sint64 | readSint64 () |
std::string | readString () |
std::shared_ptr< JsonValue > | readJson () |
std::vector< bool > | readBoolVector () |
std::vector< float > | readFloatVector () |
std::vector< double > | readDoubleVector () |
std::vector< Uint32 > | readUint32Vector () |
std::vector< Sint32 > | readSint32Vector () |
std::vector< Uint64 > | readUint64Vector () |
std::vector< Sint64 > | readSint64Vector () |
std::vector< std::string > | readStringVector () |
std::vector< std::shared_ptr< JsonValue > > | readJsonVector () |
void | reset () |
Static Public Member Functions | |
static std::shared_ptr< NetworkDeserializer > | alloc () |
A class to deserializes byte arrays back into the original complex data.
This class only handles messages serialized using NetworkSerializer. You should use NetworkType to guide your deserialization process.
typedef std::variant< std::monostate, bool, float, double, Uint32, Uint64, Sint32, Sint64, std::string, std::shared_ptr<JsonValue>, std::vector<bool>, std::vector<float>, std::vector<double>, std::vector<Uint32>, std::vector<Uint64>, std::vector<Sint32>, std::vector<Sint64>, std::vector<std::string>, std::vector<std::shared_ptr<JsonValue> > > cugl::NetworkDeserializer::Message |
Variant of possible messages to receive.
To understand how to use variants, see this tutorial:
https://riptutorial.com/cplusplus/example/18604/basic-std–variant-use
This type is to be used with the read() method. The variant monostate represents no more content.
|
inline |
Creates a new Network Deserializer on the stack.
Network deserializers do not have any nontrivial state and so it is unnecessary to use an init method. However, we do include a static alloc method for creating shared pointers.
|
inlinestatic |
Returns a newly created Network Deserializer.
This method is solely include for convenience purposes.
|
inline |
Returns true if there is any data left to be read
NetworkType cugl::NetworkDeserializer::nextType | ( | ) | const |
Returns the type of the next data value to be read.
This method returns NetworkType#InvalidType if the stream is exhausted (nothing left to be read) or corrupted.
Message cugl::NetworkDeserializer::read | ( | ) |
Reads the next unreturned value or vector from the currently loaded byte vector.
A byte vector should be loaded with the receive method. If nothing is loaded, this will return the monostate. This method also advances the read position. If the end of the vector is reached, this returns the monostate.
The return type is a variant. You can pattern match on the variant to handle different types. However, if you know what order the values were written in (which you really should), you can use std::get<T>(...) to just assert the next value should be of a certain type T and to extract that value directly. This avoids the overhead of a pattern match on every value. In addition, it is guaranteed to never corrupt the stream (unlike the other read methods)
bool cugl::NetworkDeserializer::readBool | ( | ) |
Returns a single boolean value.
This method is only defined if nextType has returned either BooleanTrue or BooleanFalse. Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return false.
std::vector<bool> cugl::NetworkDeserializer::readBoolVector | ( | ) |
Returns a vector of boolean values.
This method is only defined if nextType has returned (ArrayType+BooleanTrue). Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return false.
double cugl::NetworkDeserializer::readDouble | ( | ) |
Returns a single double value.
This method is only defined if nextType has returned DoubleType. Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
std::vector<double> cugl::NetworkDeserializer::readDoubleVector | ( | ) |
Returns a vector of double values.
This method is only defined if nextType has returned (ArrayType+DoubleType). Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
float cugl::NetworkDeserializer::readFloat | ( | ) |
Returns a single float value.
This method is only defined if nextType has returned FloatType. Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
std::vector<float> cugl::NetworkDeserializer::readFloatVector | ( | ) |
Returns a vector of float values.
This method is only defined if nextType has returned (ArrayType+FloatType). Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
std::shared_ptr<JsonValue> cugl::NetworkDeserializer::readJson | ( | ) |
Returns a single JsonValue object.
This method is only defined if nextType has returned JsonType. Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return nullptr.
std::vector<std::shared_ptr<JsonValue> > cugl::NetworkDeserializer::readJsonVector | ( | ) |
Returns a vector of JsonValue objects.
This method is only defined if nextType has returned (ArrayType+JsonType). Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return nullptr.
Sint32 cugl::NetworkDeserializer::readSint32 | ( | ) |
Returns a single signed (32 bit) int value.
This method is only defined if nextType has returned SInt32Type. Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
std::vector<Sint32> cugl::NetworkDeserializer::readSint32Vector | ( | ) |
Returns a vector of signed (32 bit) int values.
This method is only defined if nextType has returned (ArrayType+SInt32Type). Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
Sint64 cugl::NetworkDeserializer::readSint64 | ( | ) |
Returns a single signed (64 bit) int value.
This method is only defined if nextType has returned SInt64Type. Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
std::vector<Sint64> cugl::NetworkDeserializer::readSint64Vector | ( | ) |
Returns a vector of signed (64 bit) int values.
This method is only defined if nextType has returned (ArrayType+SInt32Type). Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
std::string cugl::NetworkDeserializer::readString | ( | ) |
Returns a single string.
This method is only defined if nextType has returned StringType. Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return the empty string.
std::vector<std::string> cugl::NetworkDeserializer::readStringVector | ( | ) |
Returns a vector of strings.
This method is only defined if nextType has returned (ArrayType+StringType). Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return the empty string.
Uint32 cugl::NetworkDeserializer::readUint32 | ( | ) |
Returns a single unsigned (32 bit) int value.
This method is only defined if nextType has returned UInt32Type. Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
std::vector<Uint32> cugl::NetworkDeserializer::readUint32Vector | ( | ) |
Returns a vector of unsigned (32 bit) int values.
This method is only defined if nextType has returned (ArrayType+UInt32Type). Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
Uint64 cugl::NetworkDeserializer::readUint64 | ( | ) |
Returns a single unsigned (64 bit) int value.
This method is only defined if nextType has returned UInt64Type. Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
std::vector<Uint64> cugl::NetworkDeserializer::readUint64Vector | ( | ) |
Returns a vector of unsigned (64 bit) int values.
This method is only defined if nextType has returned (ArrayType+UInt64Type). Otherwise, calling this method will potentially corrupt the stream.
The method advances the read position. If called when no more data is available, this method will return 0.
void cugl::NetworkDeserializer::receive | ( | const std::vector< uint8_t > & | msg | ) |
Loads a new message to be read.
Calling this method will discard any previously loaded messages. The message must be serialized by NetworkSerializer. Otherwise, the results are unspecified.
Once loaded, call read to get a single value (or vector of values). To read all of the data transmitted, call read until it returns the monostate. The values are guaranteed to be delievered in the same order they were written.
If you are uncomfortable with variants, there are type-specific methods for reading data. However, you will need to call nextType to determine the correct method to call.
msg | The byte vector serialized by NetworkSerializer |
void cugl::NetworkDeserializer::reset | ( | ) |
Clears the buffer and ignore any remaining data in it.