Panda3D
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 {
39 PUBLISHED:
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 
97 PUBLISHED:
98  EXTENSION(INLINE PyObject *get_message() const);
99  EXTENSION(INLINE PyObject *__bytes__() const);
100 
101  INLINE size_t get_length() const;
102 
103  INLINE void set_array(PTA_uchar data);
104  INLINE void copy_array(CPTA_uchar data);
105  INLINE CPTA_uchar get_array() const;
106  INLINE PTA_uchar modify_array();
107 
108  INLINE void set_stdfloat_double(bool stdfloat_double);
109  INLINE bool get_stdfloat_double() const;
110 
111  INLINE bool operator == (const Datagram &other) const;
112  INLINE bool operator != (const Datagram &other) const;
113  INLINE bool operator < (const Datagram &other) const;
114 
115  void output(std::ostream &out) const;
116  void write(std::ostream &out, unsigned int indent=0) const;
117 
118 private:
119  PTA_uchar _data;
120 
121 #ifdef STDFLOAT_DOUBLE
122  bool _stdfloat_double = true;
123 #else
124  bool _stdfloat_double = false;
125 #endif
126 
127 public:
128 
129  static TypeHandle get_class_type() {
130  return _type_handle;
131  }
132  static void init_type() {
134  register_type(_type_handle, "Datagram",
135  TypedObject::get_class_type());
136  }
137  virtual TypeHandle get_type() const {
138  return get_class_type();
139  }
140  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
141 
142 
143 private:
144  static TypeHandle _type_handle;
145 };
146 
147 // These generic functions are primarily for writing a value to a datagram
148 // from within a template in which the actual type of the value is not known.
149 // If you do know the type, it's preferable to use the explicit add_*() method
150 // from above instead.
151 
152 INLINE void
153 generic_write_datagram(Datagram &dest, bool value);
154 INLINE void
155 generic_write_datagram(Datagram &dest, int value);
156 INLINE void
157 generic_write_datagram(Datagram &dest, float value);
158 INLINE void
159 generic_write_datagram(Datagram &dest, double value);
160 INLINE void
161 generic_write_datagram(Datagram &dest, const std::string &value);
162 INLINE void
163 generic_write_datagram(Datagram &dest, const std::wstring &value);
164 INLINE void
165 generic_write_datagram(Datagram &dest, const vector_uchar &value);
166 
167 
168 #include "datagram.I"
169 
170 #endif
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
indent
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
bigEndian.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pta_uchar.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
numeric_types.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
ConstPointerToArray< unsigned char >
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
datagram.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
typedObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypedObject
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:88
littleEndian.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.