Datagram

class Datagram

Bases: TypedObject

An ordered list of data elements, formatted in memory for transmission over a socket or writing to a data file.

Data elements should be added one at a time, in order, to the Datagram. The nature and contents of the data elements are totally up to the user. When a Datagram has been transmitted and received, its data elements may be extracted using a DatagramIterator; it is up to the caller to know the correct type of each data element in order.

A Datagram is itself headerless; it is simply a collection of data elements.

Inheritance diagram

Inheritance diagram of Datagram

Datagram(void) = default
Datagram(void const *data, std::size_t size)
explicit Datagram(vector_uchar data)
Datagram(Datagram const &copy) = default

Constructs a datagram from an existing block of data.

void add_be_float32(PN_float32 value)

Adds a 32-bit single-precision big-endian floating-point number to the datagram.

void add_be_float64(PN_float64 value)

Adds a 64-bit big-endian floating-point number to the datagram.

void add_be_int16(int16_t value)

These functions pack numbers big-endian, in case that’s desired.

Adds a signed 16-bit big-endian integer to the datagram.

void add_be_int32(int32_t value)

Adds a signed 32-bit big-endian integer to the datagram.

void add_be_int64(int64_t value)

Adds a signed 64-bit big-endian integer to the datagram.

void add_be_uint16(uint16_t value)

Adds an unsigned 16-bit big-endian integer to the datagram.

void add_be_uint32(uint32_t value)

Adds an unsigned 32-bit big-endian integer to the datagram.

void add_be_uint64(uint64_t value)

Adds an unsigned 64-bit big-endian integer to the datagram.

void add_blob(vector_uchar const&)

Adds a variable-length binary blob to the datagram. This actually adds a count followed by n bytes.

void add_blob32(vector_uchar const&)

Adds a variable-length binary blob to the datagram, using a 32-bit length field to allow very long blobs.

void add_bool(bool value)

Adds a boolean value to the datagram.

void add_fixed_string(std::string const &str, std::size_t size)

Adds a fixed-length string to the datagram. If the string given is less than the requested size, this will pad the string out with zeroes; if it is greater than the requested size, this will silently truncate the string.

void add_float32(PN_float32 value)

Adds a 32-bit single-precision floating-point number to the datagram. Since this kind of float is not necessarily portable across different architectures, special care is required.

void add_float64(PN_float64 value)

Adds a 64-bit floating-point number to the datagram.

void add_int16(int16_t value)

The default numeric packing is little-endian.

Adds a signed 16-bit integer to the datagram.

void add_int32(int32_t value)

Adds a signed 32-bit integer to the datagram.

void add_int64(int64_t value)

Adds a signed 64-bit integer to the datagram.

void add_int8(int8_t value)

Adds a signed 8-bit integer to the datagram.

void add_stdfloat(PN_stdfloat value)

Adds either a 32-bit or a 64-bit floating-point number, according to set_stdfloat_double().

void add_string(std::string const &str)

Adds a variable-length string to the datagram. This actually adds a count followed by n bytes.

void add_string32(std::string const &str)

Adds a variable-length string to the datagram, using a 32-bit length field to allow very long strings.

void add_uint16(uint16_t value)

Adds an unsigned 16-bit integer to the datagram.

void add_uint32(uint32_t value)

Adds an unsigned 32-bit integer to the datagram.

void add_uint64(uint64_t value)

Adds an unsigned 64-bit integer to the datagram.

void add_uint8(uint8_t value)

Adds an unsigned 8-bit integer to the datagram.

void add_wstring(std::wstring const &str)

Adds a variable-length wstring to the datagram.

void add_z_string(std::string const &str)

Adds a variable-length string to the datagram, as a NULL-terminated string.

void append_data(void const *data, std::size_t size)
void append_data(vector_uchar const &data)

Appends some more raw data to the end of the datagram.

virtual void clear(void)

Resets the datagram to empty, in preparation for building up a new datagram.

void copy_array(CPTA_uchar data)

Replaces the data in the Datagram with a copy of the data in the indicated CPTA_uchar. Unlike set_array(), a complete copy is made of the data; subsequent changes to the Datagram will not change the source CPTA_uchar.

void dump_hex(std::ostream &out, unsigned int indent = 0) const

Writes a representation of the entire datagram contents, as a sequence of hex (and ASCII) values.

CPTA_uchar get_array(void) const

Returns a const pointer to the actual data in the Datagram.

static TypeHandle get_class_type(void)
std::size_t get_length(void) const

Returns the number of bytes in the datagram.

PyObject *get_message(void) const

Returns the datagram’s data as a string.

bool get_stdfloat_double(void) const

Returns the stdfloat_double flag. See set_stdfloat_double().

PTA_uchar modify_array(void)

Returns a modifiable pointer to the actual data in the Datagram.

void output(std::ostream &out) const

Write a string representation of this instance to <out>.

void pad_bytes(std::size_t size)

Adds the indicated number of zero bytes to the datagram.

void set_array(PTA_uchar data)

Replaces the data in the Datagram with the data in the indicated PTA_uchar. This is assignment by reference: subsequent changes to the Datagram will also change the source PTA_uchar.

void set_stdfloat_double(bool stdfloat_double)

Changes the stdfloat_double flag, which defines the operation performed by add_stdfloat() and DatagramIterator::get_stdfloat(). When this is true, add_stdfloat() adds a 64-bit floating-point number; when it is false, it adds a 32-bit floating-point number. The default is based on the STDFLOAT_DOUBLE compilation flag.

void write(std::ostream &out, unsigned int indent = 0) const

Write a string representation of this instance to <out>.