CUGL 2.1
Cornell University Game Library
Public Member Functions | Static Public Member Functions | List of all members
cugl::NetworkSerializer Class Reference

#include <CUNetworkSerializer.h>

Public Member Functions

 NetworkSerializer ()
 
void writeBool (bool b)
 
void writeFloat (float f)
 
void writeDouble (double d)
 
void writeUint32 (Uint32 i)
 
void writeUint64 (Uint64 i)
 
void writeSint32 (Sint32 i)
 
void writeSint64 (Sint64 i)
 
void writeString (std::string s)
 
void writeChars (char *s)
 
void writeJson (const std::shared_ptr< JsonValue > &j)
 
void writeBoolVector (std::vector< bool > b)
 
void writeFloatVector (std::vector< float > f)
 
void writeDoubleVector (std::vector< double > d)
 
void writeUint32Vector (std::vector< Uint32 > i)
 
void writeUint64Vector (std::vector< Uint64 > i)
 
void writeSint32Vector (std::vector< Sint32 > i)
 
void writeSint64Vector (std::vector< Sint64 > i)
 
void writeStringVector (std::vector< std::string > s)
 
void writeCharsVector (std::vector< char * > s)
 
void writeJsonVector (std::vector< std::shared_ptr< JsonValue >> j)
 
const std::vector< uint8_t > & serialize ()
 
void reset ()
 

Static Public Member Functions

static std::shared_ptr< NetworkSerializeralloc ()
 

Detailed Description

A class to serialize complex data into a byte array.

The class NetworkConnection is only capable of transmitting byte arrays. You should use this class to construct a byte array for a single message so that you can transmit it.

This class is capable of serializing the following data types:

You should deserialize all of these with the NetworkDeserializer.

Note that if a char* (not a C++ string) is written, it will be deserialized as a std::string. The same applies to vectors of char*.

Constructor & Destructor Documentation

◆ NetworkSerializer()

cugl::NetworkSerializer::NetworkSerializer ( )
inline

Creates a new Network Serializer on the stack.

Network serializers 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.

Member Function Documentation

◆ alloc()

static std::shared_ptr<NetworkSerializer> cugl::NetworkSerializer::alloc ( )
inlinestatic

Returns a newly created Network Serializer.

This method is solely include for convenience purposes.

Returns
a newly created Network Serializer.

◆ reset()

void cugl::NetworkSerializer::reset ( )

Clears the input buffer.

◆ serialize()

const std::vector<uint8_t>& cugl::NetworkSerializer::serialize ( )

Returns a byte vector of all written values suitable for network transit.

This method should be called after the appropriate write methods have been called. This provides a vector for network transit. It will need to be deserialized on the other end by NetworkDeserializer.

You MUST call reset() after this method to clear the input buffer. Otherwise, the next call to this method will still contain all the contents written in this call.

The contents of the returned vector should be treated as opaque. You should only read the output via the use of NetworkDeserializer.

Returns
a byte vector of all written values suitable for network transit.

◆ writeBool()

void cugl::NetworkSerializer::writeBool ( bool  b)

Writes a single boolean value.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
bThe value to write

◆ writeBoolVector()

void cugl::NetworkSerializer::writeBoolVector ( std::vector< bool >  b)

Writes a vector of boolean values.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
bThe vector to write

◆ writeChars()

void cugl::NetworkSerializer::writeChars ( char *  s)

Writes a single string value.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Note that this value will be deserialized by NetworkSerializer as a std::string object.

Parameters
sThe value to write

◆ writeCharsVector()

void cugl::NetworkSerializer::writeCharsVector ( std::vector< char * >  s)

Writes a vector of string values.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Note that the vector contents will be deserialized by NetworkSerializer as std::string objects.

Parameters
sThe vector to write

◆ writeDouble()

void cugl::NetworkSerializer::writeDouble ( double  d)

Writes a single double value.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
dThe value to write

◆ writeDoubleVector()

void cugl::NetworkSerializer::writeDoubleVector ( std::vector< double >  d)

Writes a vector of double values.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
dThe vector to write

◆ writeFloat()

void cugl::NetworkSerializer::writeFloat ( float  f)

Writes a single float value.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
fThe value to write

◆ writeFloatVector()

void cugl::NetworkSerializer::writeFloatVector ( std::vector< float >  f)

Writes a vector of float values.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
fThe vector to write

◆ writeJson()

void cugl::NetworkSerializer::writeJson ( const std::shared_ptr< JsonValue > &  j)

Writes a single JsonValue.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
jThe value to write

◆ writeJsonVector()

void cugl::NetworkSerializer::writeJsonVector ( std::vector< std::shared_ptr< JsonValue >>  j)

Write a vector of JsonValue objects.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
jThe vector to write

◆ writeSint32()

void cugl::NetworkSerializer::writeSint32 ( Sint32  i)

Writes a single signed (32 bit) int value.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
iThe value to write

◆ writeSint32Vector()

void cugl::NetworkSerializer::writeSint32Vector ( std::vector< Sint32 >  i)

Writes a vector of signed (32 bit) int values.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
iThe vector to write

◆ writeSint64()

void cugl::NetworkSerializer::writeSint64 ( Sint64  i)

Writes a single signed (64 bit) int value.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
iThe value to write

◆ writeSint64Vector()

void cugl::NetworkSerializer::writeSint64Vector ( std::vector< Sint64 >  i)

Writes a vector of signed (64 bit) int values.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
iThe vector to write

◆ writeString()

void cugl::NetworkSerializer::writeString ( std::string  s)

Writes a single string value.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
sThe value to write

◆ writeStringVector()

void cugl::NetworkSerializer::writeStringVector ( std::vector< std::string >  s)

Writes a vector of string values.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
sThe vector to write

◆ writeUint32()

void cugl::NetworkSerializer::writeUint32 ( Uint32  i)

Writes a single unsigned (32 bit) int value.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
iThe value to write

◆ writeUint32Vector()

void cugl::NetworkSerializer::writeUint32Vector ( std::vector< Uint32 >  i)

Writes a vector of unsigned (32 bit) int values.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
iThe vector to write

◆ writeUint64()

void cugl::NetworkSerializer::writeUint64 ( Uint64  i)

Writes a single unsigned (64 bit) int value.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
iThe value to write

◆ writeUint64Vector()

void cugl::NetworkSerializer::writeUint64Vector ( std::vector< Uint64 >  i)

Writes a vector of unsigned (64 bit) int values.

Values will be deserialized on other machines in the same order they were written in. Pass the result of serialize to the NetworkConnection to send all values buffered up to this point.

Parameters
iThe vector to write

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