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
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition typedObject.h:88
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.