Panda3D

Loading...
Searching...
No Matches
datagram.h
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file datagram.h
10 * @author drose
11 * @date 2000-06-06
12 */
13
14#ifndef DATAGRAM_H
15#define DATAGRAM_H
16
17#include "pandabase.h"
18
19#include "numeric_types.h"
20#include "typedObject.h"
21#include "littleEndian.h"
22#include "bigEndian.h"
23#include "pta_uchar.h"
24
25/**
26 * An ordered list of data elements, formatted in memory for transmission over
27 * a socket or writing to a data file.
28 *
29 * Data elements should be added one at a time, in order, to the Datagram.
30 * The nature and contents of the data elements are totally up to the user.
31 * When a Datagram has been transmitted and received, its data elements may be
32 * extracted using a DatagramIterator; it is up to the caller to know the
33 * correct type of each data element in order.
34 *
35 * A Datagram is itself headerless; it is simply a collection of data
36 * elements.
37 */
38 class EXPCL_PANDA_EXPRESS Datagram : public TypedObject {
39PUBLISHED:
40 INLINE Datagram() = default;
41 INLINE Datagram(const void *data, size_t size);
42 INLINE explicit Datagram(vector_uchar data);
43 Datagram(const Datagram &copy) = default;
44 Datagram(Datagram &&from) noexcept = default;
45 virtual ~Datagram();
46
47 Datagram &operator = (const Datagram &copy) = default;
48 Datagram &operator = (Datagram &&from) noexcept = default;
49
50 virtual void clear();
51 void dump_hex(std::ostream &out, unsigned int indent=0) const;
52
53 INLINE void add_bool(bool value);
54 INLINE void add_int8(int8_t value);
55 INLINE void add_uint8(uint8_t value);
56
57 // The default numeric packing is little-endian.
58 INLINE void add_int16(int16_t value);
59 INLINE void add_int32(int32_t value);
60 INLINE void add_int64(int64_t value);
61 INLINE void add_uint16(uint16_t value);
62 INLINE void add_uint32(uint32_t value);
63 INLINE void add_uint64(uint64_t value);
64 INLINE void add_float32(PN_float32 value);
65 INLINE void add_float64(PN_float64 value);
66 INLINE void add_stdfloat(PN_stdfloat value);
67
68 // These functions pack numbers big-endian, in case that's desired.
69 INLINE void add_be_int16(int16_t value);
70 INLINE void add_be_int32(int32_t value);
71 INLINE void add_be_int64(int64_t value);
72 INLINE void add_be_uint16(uint16_t value);
73 INLINE void add_be_uint32(uint32_t value);
74 INLINE void add_be_uint64(uint64_t value);
75 INLINE void add_be_float32(PN_float32 value);
76 INLINE void add_be_float64(PN_float64 value);
77
78 INLINE void add_string(const std::string &str);
79 INLINE void add_string32(const std::string &str);
80 INLINE void add_z_string(const std::string &str);
81 INLINE void add_fixed_string(const std::string &str, size_t size);
82 void add_wstring(const std::wstring &str);
83
84 INLINE void add_blob(const vector_uchar &);
85 INLINE void add_blob32(const vector_uchar &);
86
87 void pad_bytes(size_t size);
88 void append_data(const void *data, size_t size);
89 INLINE void append_data(const vector_uchar &data);
90
91 public:
92 void assign(const void *data, size_t size);
93
94 INLINE std::string get_message() const;
95 INLINE const void *get_data() const;
96
97PUBLISHED:
98 EXTENSION(INLINE PyObject *get_message() const);
99 EXTENSION(INLINE PyObject *__bytes__() const);
100 EXTENSION(PyObject *__reduce__() const);
101
102 INLINE size_t get_length() const;
103
104 INLINE void set_array(PTA_uchar data);
105 INLINE void copy_array(CPTA_uchar data);
106 INLINE CPTA_uchar get_array() const;
107 INLINE PTA_uchar modify_array();
108
109 INLINE void set_stdfloat_double(bool stdfloat_double);
110 INLINE bool get_stdfloat_double() const;
111
112 INLINE bool operator == (const Datagram &other) const;
113 INLINE bool operator != (const Datagram &other) const;
114 INLINE bool operator < (const Datagram &other) const;
115
116 void output(std::ostream &out) const;
117 void write(std::ostream &out, unsigned int indent=0) const;
118
119 private:
120 PTA_uchar _data;
121
122#ifdef STDFLOAT_DOUBLE
123 bool _stdfloat_double = true;
124#else
125 bool _stdfloat_double = false;
126#endif
127
128 public:
129
130 static TypeHandle get_class_type() {
131 return _type_handle;
132 }
133 static void init_type() {
134 TypedObject::init_type();
135 register_type(_type_handle, "Datagram",
136 TypedObject::get_class_type());
137 }
138 virtual TypeHandle get_type() const {
139 return get_class_type();
140 }
141 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
142
143
144 private:
145 static TypeHandle _type_handle;
146};
147
148// These generic functions are primarily for writing a value to a datagram
149// from within a template in which the actual type of the value is not known.
150// If you do know the type, it's preferable to use the explicit add_*() method
151// from above instead.
152
153INLINE void
154generic_write_datagram(Datagram &dest, bool value);
155INLINE void
156generic_write_datagram(Datagram &dest, int value);
157INLINE void
158generic_write_datagram(Datagram &dest, float value);
159INLINE void
160generic_write_datagram(Datagram &dest, double value);
161INLINE void
162generic_write_datagram(Datagram &dest, const std::string &value);
163INLINE void
164generic_write_datagram(Datagram &dest, const std::wstring &value);
165INLINE void
166generic_write_datagram(Datagram &dest, const vector_uchar &value);
167
168
169#include "datagram.I"
170
171#endif
bigEndian.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
Datagram::add_be_uint64
void add_be_uint64(uint64_t value)
Adds an unsigned 64-bit big-endian integer to the datagram.
Definition datagram.I:190
Datagram::add_uint32
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the datagram.
Definition datagram.I:94
Datagram::add_be_uint32
void add_be_uint32(uint32_t value)
Adds an unsigned 32-bit big-endian integer to the datagram.
Definition datagram.I:181
Datagram::add_be_int64
void add_be_int64(int64_t value)
Adds a signed 64-bit big-endian integer to the datagram.
Definition datagram.I:163
Datagram::output
void output(std::ostream &out) const
Write a string representation of this instance to <out>.
Definition datagram.cxx:172
Datagram::write
void write(std::ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
Definition datagram.cxx:182
Datagram::set_stdfloat_double
void set_stdfloat_double(bool stdfloat_double)
Changes the stdfloat_double flag, which defines the operation performed by add_stdfloat() and Datagra...
Definition datagram.I:395
Datagram::add_z_string
void add_z_string(const std::string &str)
Adds a variable-length string to the datagram, as a NULL-terminated string.
Definition datagram.I:247
Datagram::add_be_uint16
void add_be_uint16(uint16_t value)
Adds an unsigned 16-bit big-endian integer to the datagram.
Definition datagram.I:172
Datagram::get_length
size_t get_length() const
Returns the number of bytes in the datagram.
Definition datagram.I:335
Datagram::get_stdfloat_double
bool get_stdfloat_double() const
Returns the stdfloat_double flag.
Definition datagram.I:403
Datagram::add_be_int32
void add_be_int32(int32_t value)
Adds a signed 32-bit big-endian integer to the datagram.
Definition datagram.I:154
Datagram::add_wstring
void add_wstring(const std::wstring &str)
Adds a variable-length wstring to the datagram.
Definition datagram.cxx:83
Datagram::assign
void assign(const void *data, size_t size)
Replaces the datagram's data with the indicated block.
Definition datagram.cxx:160
Datagram::add_be_float32
void add_be_float32(PN_float32 value)
Adds a 32-bit single-precision big-endian floating-point number to the datagram.
Definition datagram.I:200
Datagram::add_int16
void add_int16(int16_t value)
Adds a signed 16-bit integer to the datagram.
Definition datagram.I:58
Datagram::add_fixed_string
void add_fixed_string(const std::string &str, size_t size)
Adds a fixed-length string to the datagram.
Definition datagram.I:263
Datagram::add_float32
void add_float32(PN_float32 value)
Adds a 32-bit single-precision floating-point number to the datagram.
Definition datagram.I:114
Datagram::get_array
CPTA_uchar get_array() const
Returns a const pointer to the actual data in the Datagram.
Definition datagram.I:364
Datagram::add_int32
void add_int32(int32_t value)
Adds a signed 32-bit integer to the datagram.
Definition datagram.I:67
Datagram::add_uint8
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the datagram.
Definition datagram.I:50
Datagram::add_string32
void add_string32(const std::string &str)
Adds a variable-length string to the datagram, using a 32-bit length field to allow very long strings...
Definition datagram.I:235
Datagram::add_stdfloat
void add_stdfloat(PN_stdfloat value)
Adds either a 32-bit or a 64-bit floating-point number, according to set_stdfloat_double().
Definition datagram.I:133
Datagram::add_be_int16
void add_be_int16(int16_t value)
Adds a signed 16-bit big-endian integer to the datagram.
Definition datagram.I:145
Datagram::get_message
std::string get_message() const
Returns the datagram's data as a string.
Definition datagram.I:314
Datagram::add_be_float64
void add_be_float64(PN_float64 value)
Adds a 64-bit big-endian floating-point number to the datagram.
Definition datagram.I:209
Datagram::copy_array
void copy_array(CPTA_uchar data)
Replaces the data in the Datagram with a copy of the data in the indicated CPTA_uchar.
Definition datagram.I:355
Datagram::dump_hex
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.
Definition datagram.cxx:44
Datagram::add_bool
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition datagram.I:34
Datagram::clear
virtual void clear()
Resets the datagram to empty, in preparation for building up a new datagram.
Definition datagram.cxx:35
Datagram::append_data
void append_data(const void *data, size_t size)
Appends some more raw data to the end of the datagram.
Definition datagram.cxx:129
Datagram::set_array
void set_array(PTA_uchar data)
Replaces the data in the Datagram with the data in the indicated PTA_uchar.
Definition datagram.I:345
Datagram::add_blob
void add_blob(const vector_uchar &)
Adds a variable-length binary blob to the datagram.
Definition datagram.I:278
Datagram::pad_bytes
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the datagram.
Definition datagram.cxx:99
Datagram::modify_array
PTA_uchar modify_array()
Returns a modifiable pointer to the actual data in the Datagram.
Definition datagram.I:372
Datagram::add_uint64
void add_uint64(uint64_t value)
Adds an unsigned 64-bit integer to the datagram.
Definition datagram.I:103
Datagram::add_int64
void add_int64(int64_t value)
Adds a signed 64-bit integer to the datagram.
Definition datagram.I:76
Datagram::add_uint16
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition datagram.I:85
Datagram::add_string
void add_string(const std::string &str)
Adds a variable-length string to the datagram.
Definition datagram.I:219
Datagram::add_blob32
void add_blob32(const vector_uchar &)
Adds a variable-length binary blob to the datagram, using a 32-bit length field to allow very long bl...
Definition datagram.I:294
Datagram::add_float64
void add_float64(PN_float64 value)
Adds a 64-bit floating-point number to the datagram.
Definition datagram.I:123
Datagram::get_data
const void * get_data() const
Returns a pointer to the beginning of the datagram's data.
Definition datagram.I:327
Datagram::add_int8
void add_int8(int8_t value)
Adds a signed 8-bit integer to the datagram.
Definition datagram.I:42
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
TypedObject::init_type
static void init_type()
This function is declared non-inline to work around a compiler bug in g++ 2.96.
Definition typedObject.cxx:44
datagram.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
indent
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
littleEndian.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
numeric_types.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pta_uchar.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
register_type
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition register_type.I:22
typedObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.