26 SSReader(std::istream *stream) : _istream(stream) {
28 _tcp_header_size = tcp_header_size;
30 #ifdef SIMULATE_NETWORK_DELAY 31 _delay_active =
false;
33 _delay_variance = 0.0;
34 #endif // SIMULATE_NETWORK_DELAY 52 if (_tcp_header_size == 0) {
53 _data_expected = _data_so_far.
size();
55 if (_data_expected == 0) {
57 while ((
int)_data_so_far.size() < _tcp_header_size) {
58 int ch = _istream->get();
59 if (ch == EOF || _istream->fail()) {
63 _data_so_far.push_back((
unsigned char)ch);
68 if (_tcp_header_size == 2) {
69 _data_expected = di.get_uint16();
70 }
else if (_tcp_header_size == 4) {
71 _data_expected = di.get_uint32();
73 _data_so_far.erase(_data_so_far.begin(), _data_so_far.begin() + _tcp_header_size);
75 if (_data_expected == 0) {
84 static const size_t buffer_size = 1024;
85 char buffer[buffer_size];
87 size_t read_count = std::min(_data_expected - _data_so_far.size(), buffer_size);
88 _istream->read(buffer, read_count);
89 size_t count = _istream->gcount();
91 _data_so_far.insert(_data_so_far.end(), buffer, buffer + count);
93 read_count = std::min(_data_expected - _data_so_far.size(),
95 _istream->read(buffer, read_count);
96 count = _istream->gcount();
99 if (_data_so_far.size() < _data_expected) {
110 _data_so_far.clear();
115 #ifdef SIMULATE_NETWORK_DELAY 127 start_delay(
double min_delay,
double max_delay) {
128 _min_delay = min_delay;
129 _delay_variance = std::max(max_delay - min_delay, 0.0);
130 _delay_active =
true;
132 #endif // SIMULATE_NETWORK_DELAY 134 #ifdef SIMULATE_NETWORK_DELAY 142 _delay_active =
false;
144 #endif // SIMULATE_NETWORK_DELAY 146 #ifdef SIMULATE_NETWORK_DELAY 151 delay_datagram(
const Datagram &datagram) {
152 nassertv(_delay_active);
155 double reveal_time = now + _min_delay;
157 if (_delay_variance > 0.0) {
158 reveal_time += _delay_variance * ((double)rand() / (double)RAND_MAX);
160 _delayed.push_back(DelayedDatagram());
161 DelayedDatagram &dd = _delayed.back();
162 dd._reveal_time = reveal_time;
163 dd._datagram = datagram;
165 #endif // SIMULATE_NETWORK_DELAY 167 #ifdef SIMULATE_NETWORK_DELAY 174 if (_delayed.empty()) {
177 const DelayedDatagram &dd = _delayed.front();
180 if (dd._reveal_time > now) {
186 datagram = dd._datagram;
187 _delayed.pop_front();
191 #endif // SIMULATE_NETWORK_DELAY 197 SSWriter(std::ostream *stream) : _ostream(stream) {
198 _collect_tcp = collect_tcp;
199 _collect_tcp_interval = collect_tcp_interval;
200 _queued_data_start = 0.0;
201 _tcp_header_size = tcp_header_size;
219 if (_tcp_header_size == 2) {
221 downloader_cat.error()
222 <<
"Attempt to send TCP datagram of " << dg.
get_length()
223 <<
" bytes--too long!\n";
224 nassert_raise(
"Datagram too long");
229 }
else if (_tcp_header_size == 4) {
251 nassertv(_channel ==
nullptr);
254 #endif // HAVE_OPENSSL static TrueClock * get_global_ptr()
Returns a pointer to the one TrueClock object in the world.
void append_data(const void *data, size_t size)
Appends some more raw data to the end of the datagram.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void clear()
Resets the datagram to empty, in preparation for building up a new datagram.
size_t size() const
Returns the number of unique words in the variable.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the datagram.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A class to retrieve the individual data elements previously stored in a Datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
size_t get_length() const
Returns the number of bytes in the datagram.
const void * get_data() const
Returns a pointer to the beginning of the datagram's data.