CUGL 2.5
Cornell University Game Library
|
#include <CUNetcodeSerializer.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 | |
NetcodeDeserializer () | |
void | receive (const std::vector< std::byte > &msg) |
Message | read () |
bool | available () const |
NetcodeType | 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< NetcodeDeserializer > | alloc () |
A class to deserializes byte arrays back into the original complex data.
This class only handles messages serialized using NetcodeSerializer
. You should use NetcodeType
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::net::NetcodeDeserializer::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 Netcode Deserializer on the stack.
Netcode 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 Netcode ,l Deserializer.
This method is solely include for convenience purposes.
|
inline |
Returns true if there is any data left to be read
NetcodeType cugl::net::NetcodeDeserializer::nextType | ( | ) | const |
Returns the type of the next data value to be read.
This method returns NetcodeType#InvalidType
if the stream is exhausted (nothing left to be read) or corrupted.
Message cugl::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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.
JsonValue
object. std::vector< std::shared_ptr< JsonValue > > cugl::net::NetcodeDeserializer::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.
JsonValue
objects. Sint32 cugl::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::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::net::NetcodeDeserializer::receive | ( | const std::vector< std::byte > & | msg | ) |
Loads a new message to be read.
Calling this method will discard any previously loaded messages. The message must be serialized by NetcodeSerializer
. 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 NetcodeSerializer |
void cugl::net::NetcodeDeserializer::reset | ( | ) |
Clears the buffer and ignore any remaining data in it.