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 */
38class 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
91public:
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
119private:
120 PTA_uchar _data;
121
122#ifdef STDFLOAT_DOUBLE
123 bool _stdfloat_double = true;
124#else
125 bool _stdfloat_double = false;
126#endif
127
128public:
129
130 static TypeHandle get_class_type() {
131 return _type_handle;
132 }
133 static void 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
144private:
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
void add_be_uint64(uint64_t value)
Adds an unsigned 64-bit big-endian integer to the datagram.
Definition datagram.I:190
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the datagram.
Definition datagram.I:94
void add_be_uint32(uint32_t value)
Adds an unsigned 32-bit big-endian integer to the datagram.
Definition datagram.I:181
void add_be_int64(int64_t value)
Adds a signed 64-bit big-endian integer to the datagram.
Definition datagram.I:163
void output(std::ostream &out) const
Write a string representation of this instance to <out>.
Definition datagram.cxx:172
void write(std::ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
Definition datagram.cxx:182
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
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
void add_be_uint16(uint16_t value)
Adds an unsigned 16-bit big-endian integer to the datagram.
Definition datagram.I:172
size_t get_length() const
Returns the number of bytes in the datagram.
Definition datagram.I:335
bool get_stdfloat_double() const
Returns the stdfloat_double flag.
Definition datagram.I:403
void add_be_int32(int32_t value)
Adds a signed 32-bit big-endian integer to the datagram.
Definition datagram.I:154
void add_wstring(const std::wstring &str)
Adds a variable-length wstring to the datagram.
Definition datagram.cxx:83
void assign(const void *data, size_t size)
Replaces the datagram's data with the indicated block.
Definition datagram.cxx:160
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
void add_int16(int16_t value)
Adds a signed 16-bit integer to the datagram.
Definition datagram.I:58
void add_fixed_string(const std::string &str, size_t size)
Adds a fixed-length string to the datagram.
Definition datagram.I:263
void add_float32(PN_float32 value)
Adds a 32-bit single-precision floating-point number to the datagram.
Definition datagram.I:114
CPTA_uchar get_array() const
Returns a const pointer to the actual data in the Datagram.
Definition datagram.I:364
void add_int32(int32_t value)
Adds a signed 32-bit integer to the datagram.
Definition datagram.I:67
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the datagram.
Definition datagram.I:50
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
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
void add_be_int16(int16_t value)
Adds a signed 16-bit big-endian integer to the datagram.
Definition datagram.I:145
std::string get_message() const
Returns the datagram's data as a string.
Definition datagram.I:314
void add_be_float64(PN_float64 value)
Adds a 64-bit big-endian floating-point number to the datagram.
Definition datagram.I:209
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
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
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition datagram.I:34
virtual void clear()
Resets the datagram to empty, in preparation for building up a new datagram.
Definition datagram.cxx:35
void append_data(const void *data, size_t size)
Appends some more raw data to the end of the datagram.
Definition datagram.cxx:129
void set_array(PTA_uchar data)
Replaces the data in the Datagram with the data in the indicated PTA_uchar.
Definition datagram.I:345
void add_blob(const vector_uchar &)
Adds a variable-length binary blob to the datagram.
Definition datagram.I:278
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the datagram.
Definition datagram.cxx:99
PTA_uchar modify_array()
Returns a modifiable pointer to the actual data in the Datagram.
Definition datagram.I:372
void add_uint64(uint64_t value)
Adds an unsigned 64-bit integer to the datagram.
Definition datagram.I:103
void add_int64(int64_t value)
Adds a signed 64-bit integer to the datagram.
Definition datagram.I:76
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition datagram.I:85
void add_string(const std::string &str)
Adds a variable-length string to the datagram.
Definition datagram.I:219
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
void add_float64(PN_float64 value)
Adds a 64-bit floating-point number to the datagram.
Definition datagram.I:123
const void * get_data() const
Returns a pointer to the beginning of the datagram's data.
Definition datagram.I:327
void add_int8(int8_t value)
Adds a signed 8-bit integer to the datagram.
Definition datagram.I:42
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
static void init_type()
This function is declared non-inline to work around a compiler bug in g++ 2.96.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.