15 #include "queuedConnectionReader.h" 16 #include "config_net.h" 17 #include "trueClock.h" 18 #include "lightMutexHolder.h" 25 QueuedConnectionReader::
29 #ifdef SIMULATE_NETWORK_DELAY 30 _delay_active =
false;
32 _delay_variance = 0.0;
33 #endif // SIMULATE_NETWORK_DELAY 41 QueuedConnectionReader::
42 ~QueuedConnectionReader() {
57 #ifdef SIMULATE_NETWORK_DELAY 59 #endif // SIMULATE_NETWORK_DELAY 60 return thing_available();
78 return get_thing(result);
109 void QueuedConnectionReader::
119 #ifdef SIMULATE_NETWORK_DELAY 120 delay_datagram(datagram);
122 #else // SIMULATE_NETWORK_DELAY 123 if (!enqueue_thing(datagram)) {
125 <<
"QueuedConnectionReader queue full!\n";
127 #endif // SIMULATE_NETWORK_DELAY 131 #ifdef SIMULATE_NETWORK_DELAY 142 void QueuedConnectionReader::
143 start_delay(
double min_delay,
double max_delay) {
145 _min_delay = min_delay;
146 _delay_variance = max(max_delay - min_delay, 0.0);
147 _delay_active =
true;
157 void QueuedConnectionReader::
160 _delay_active =
false;
163 while (!_delayed.empty()) {
164 const DelayedDatagram &dd = _delayed.front();
165 if (!enqueue_thing(dd._datagram)) {
167 <<
"QueuedConnectionReader queue full!\n";
169 _delayed.pop_front();
180 void QueuedConnectionReader::
185 while (!_delayed.empty()) {
186 const DelayedDatagram &dd = _delayed.front();
187 if (dd._reveal_time > now) {
191 if (!enqueue_thing(dd._datagram)) {
193 <<
"QueuedConnectionReader queue full!\n";
195 _delayed.pop_front();
206 void QueuedConnectionReader::
208 if (!_delay_active) {
209 if (!enqueue_thing(datagram)) {
211 <<
"QueuedConnectionReader queue full!\n";
217 if (!_delay_active) {
218 if (!enqueue_thing(datagram)) {
220 <<
"QueuedConnectionReader queue full!\n";
225 double reveal_time = now + _min_delay;
227 if (_delay_variance > 0.0) {
228 reveal_time += _delay_variance * ((double)rand() / (double)RAND_MAX);
230 _delayed.push_back(DelayedDatagram());
231 DelayedDatagram &dd = _delayed.back();
232 dd._reveal_time = reveal_time;
233 dd._datagram = datagram;
238 #endif // SIMULATE_NETWORK_DELAY static TrueClock * get_global_ptr()
Returns a pointer to the one TrueClock object in the world.
A specific kind of Datagram, especially for sending across or receiving from a network.
The primary interface to the low-level networking layer in this package.
void shutdown()
Terminates all threads cleanly.
This is an abstract base class for a family of classes that listen for activity on a socket and respo...
void poll()
Explicitly polls the available sockets to see if any of them have any noise.
bool data_available()
Returns true if a datagram is available on the queue; call get_data() to extract the datagram...
Similar to MutexHolder, but for a light mutex.
bool get_data(NetDatagram &result)
If a previous call to data_available() returned true, this function will return the datagram that has...
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...