forked from ravinet/mahimahi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelay_queue.cc
37 lines (29 loc) · 854 Bytes
/
delay_queue.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* -*-mode:c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include <limits>
#include "delay_queue.hh"
#include "timestamp.hh"
using namespace std;
void DelayQueue::read_packet( const string & contents )
{
packet_queue_.emplace( timestamp() + delay_ms_, contents );
}
void DelayQueue::write_packets( FileDescriptor & fd )
{
while ( (!packet_queue_.empty())
&& (packet_queue_.front().first <= timestamp()) ) {
fd.write( packet_queue_.front().second );
packet_queue_.pop();
}
}
unsigned int DelayQueue::wait_time( void ) const
{
if ( packet_queue_.empty() ) {
return numeric_limits<uint16_t>::max();
}
const auto now = timestamp();
if ( packet_queue_.front().first <= now ) {
return 0;
} else {
return packet_queue_.front().first - now;
}
}