Panda3D
Loading...
Searching...
No Matches
pStatFrameData.cxx
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 pStatFrameData.cxx
10 * @author drose
11 * @date 2000-07-10
12 */
13
14#include "pStatFrameData.h"
15#include "pStatClientVersion.h"
16#include "config_pstatclient.h"
17
18#include "datagram.h"
19#include "datagramIterator.h"
20
21#include <algorithm>
22
23/**
24 * Ensures the frame data is in monotonically increasing order by time.
25 */
27sort_time() {
28 std::stable_sort(_time_data.begin(), _time_data.end());
29}
30
31/**
32 * Writes the definition of the FrameData to the datagram. Returns true on
33 * success, false on failure.
34 */
36write_datagram(Datagram &destination, PStatClient *client) const {
37 if (_time_data.size() >= 65536 || _level_data.size() >= 65536) {
38 pstats_cat.info()
39 << "Dropping frame with " << _time_data.size()
40 << " time measurements and " << _level_data.size()
41 << " level measurements.\n";
42 return false;
43 }
44
45#if !defined(WORDS_BIGENDIAN) || defined(__GNUC__)
46 // Hand-roll this, significantly more efficient for many data points
47 size_t size = (_time_data.size() + _level_data.size()) * 6 + 4;
48 PTA_uchar array = destination.modify_array();
49 size_t offset = array.size();
50 array.resize(offset + size);
51 unsigned char *data = &array[0] + offset;
52
53 uint16_t *ptr = (uint16_t *)data;
54
55#ifdef WORDS_BIGENDIAN
56 *ptr++ = __builtin_bswap16(_time_data.size());
57
58 for (const DataPoint &dp : _time_data) {
59 *ptr++ = __builtin_bswap16(dp._index);
60 PN_float32 v = (PN_float32)dp._value;
61 *(uint32_t *)ptr = __builtin_bswap32(reinterpret_cast<uint32_t &>(v));
62 ptr += 2;
63 }
64
65 *ptr++ = __builtin_bswap16(_level_data.size());
66 for (const DataPoint &dp : _level_data) {
67 *ptr++ = __builtin_bswap16(dp._index);
68 PN_float32 v = (PN_float32)dp._value;
69 *(uint32_t *)ptr = __builtin_bswap32(reinterpret_cast<uint32_t &>(v));
70 ptr += 2;
71 }
72#else
73 *ptr++ = _time_data.size();
74
75 for (const DataPoint &dp : _time_data) {
76 *ptr++ = dp._index;
77 *(PN_float32 *)ptr = dp._value;
78 ptr += 2;
79 }
80
81 *ptr++ = _level_data.size();
82 for (const DataPoint &dp : _level_data) {
83 *ptr++ = dp._index;
84 *(PN_float32 *)ptr = dp._value;
85 ptr += 2;
86 }
87#endif
88
89#else
90 destination.add_uint16(_time_data.size());
91 for (const DataPoint &dp : _time_data) {
92 destination.add_uint16(dp._index);
93 destination.add_float32(dp._value);
94 }
95 destination.add_uint16(_level_data.size());
96 for (const DataPoint &dp : _level_data) {
97 destination.add_uint16(dp._index);
98 destination.add_float32(dp._value);
99 }
100#endif
101
102 return true;
103}
104
105/**
106 * Extracts the FrameData definition from the datagram.
107 */
110 clear();
111
112 {
113 size_t time_size;
114 if (version->is_at_least(3, 2)) {
115 time_size = source.get_uint32();
116 } else {
117 time_size = source.get_uint16();
118 }
119 _time_data.resize(time_size);
120 for (DataPoint &dp : _time_data) {
121 nassertv(source.get_remaining_size() > 0);
122 dp._index = source.get_uint16();
123 dp._value = source.get_float32();
124 }
125 }
126
127 {
128 size_t level_size;
129 if (version->is_at_least(3, 2)) {
130 level_size = source.get_uint32();
131 } else {
132 level_size = source.get_uint16();
133 }
134 _level_data.resize(level_size);
135 for (DataPoint &dp : _level_data) {
136 nassertv(source.get_remaining_size() > 0);
137 dp._index = source.get_uint16();
138 dp._value = source.get_float32();
139 }
140 }
141
142 nassertv(source.get_remaining_size() == 0);
143}
A class to retrieve the individual data elements previously stored in a Datagram.
PN_float32 get_float32()
Extracts a 32-bit single-precision floating-point number.
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
size_t get_remaining_size() const
Return the bytes left in the datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
void add_float32(PN_float32 value)
Adds a 32-bit single-precision floating-point number to the datagram.
Definition datagram.I:114
PTA_uchar modify_array()
Returns a modifiable pointer to the actual data in the Datagram.
Definition datagram.I:372
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition datagram.I:85
Records the version number of a particular client.
bool is_at_least(int major_version, int minor_version) const
Returns true if the client version is at least the indicated major/minor version number,...
Manages the communications to report statistics via a network connection to a remote PStatServer.
void sort_time()
Ensures the frame data is in monotonically increasing order by time.
void read_datagram(DatagramIterator &source, PStatClientVersion *version)
Extracts the FrameData definition from the datagram.
bool write_datagram(Datagram &destination, PStatClient *client) const
Writes the definition of the FrameData to the datagram.
void clear()
Removes all the data points from the frame data, in preparation for building up a new frame's worth.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.