8 Pacer::Pacer(Sender *sender, int max_rate_kbit_per_sec, int burst_num_packets)
9 : sender(sender), burst_num_packets(burst_num_packets), next_send_packet{0, 0}
11 const int max_rate_byte_per_sec = 1000 * max_rate_kbit_per_sec / 8;
12 seconds_per_byte = 1.0 / max_rate_byte_per_sec;
15 void Pacer::send_packet(uint16_t proto, const string &data, int incoming_seq)
17 waiting_packets.push_back(GREPacket{incoming_seq, proto, data, {0, 0}});
18 possibly_flush_packets();
21 void Pacer::possibly_adjust_tv(timeval *tv)
23 if (waiting_packets.empty()) {
28 gettimeofday(&now, NULL);
30 timeval tdiff = subtract_timeval_saturate(next_send_packet, now);
31 if (less_than(tdiff, *tv)) {
36 void Pacer::possibly_flush_packets()
38 if (waiting_packets.empty()) {
43 gettimeofday(&now, NULL);
44 if (less_than(now, next_send_packet)) {
49 for (int i = 0; i < burst_num_packets && !waiting_packets.empty(); ++i) {
50 const GREPacket &packet = waiting_packets.front();
51 sender->send_packet(packet.proto, packet.data, packet.seq);
52 bytes_sent += packet.data.size();
53 waiting_packets.pop_front();
56 next_send_packet = offset_timeval_seconds(now, seconds_per_byte * bytes_sent);