CXXFLAGS=-std=gnu++11 -O2 -g
LDLIBS=-lfecpp
-TUNGRE_OBJS=tungre.o greprotocol.o reorderer.o tunprotocol.o rsdecoder.o rsencoder.o pacer.o
-GREPROXY_OBJS=greproxy.o greprotocol.o reorderer.o tunprotocol.o rsdecoder.o rsencoder.o pacer.o
+TUNGRE_OBJS=tungre.o greprotocol.o reorderer.o tunprotocol.o rsdecoder.o rsencoder.o pacer.o timeutil.o
+GREPROXY_OBJS=greproxy.o greprotocol.o reorderer.o tunprotocol.o rsdecoder.o rsencoder.o pacer.o timeutil.o
all: tungre greproxy
tungre: $(TUNGRE_OBJS)
#include "pacer.h"
+#include "timeutil.h"
#include <algorithm>
seconds_per_byte = 1.0 / max_rate_byte_per_sec;
}
-bool less_than(const timeval &a, const timeval &b)
-{
- return make_pair(a.tv_sec, a.tv_usec) < make_pair(b.tv_sec, b.tv_usec);
-}
-
void Pacer::send_packet(uint16_t proto, const string &data, int incoming_seq)
{
waiting_packets.push_back(GREPacket{incoming_seq, proto, data, {0, 0}});
timeval now;
gettimeofday(&now, NULL);
- if (less_than(next_send_packet, now)) {
- // Should send packets immediately.
- tv->tv_sec = tv->tv_usec = 0;
- return;
- }
-
- timeval tdiff;
- tdiff.tv_sec = next_send_packet.tv_sec - now.tv_sec;
- tdiff.tv_usec = next_send_packet.tv_usec - now.tv_usec;
- if (tdiff.tv_usec < 0) {
- tdiff.tv_usec += 1000000;
- --tdiff.tv_sec;
- }
+ timeval tdiff = subtract_timeval_saturate(next_send_packet, now);
if (less_than(tdiff, *tv)) {
*tv = tdiff;
}
waiting_packets.pop_front();
}
- int usec_to_add = lrint(1e6 * seconds_per_byte * bytes_sent);
- int sec_to_add = usec_to_add / 1000000;
- usec_to_add %= 1000000;
-
- next_send_packet = now;
- next_send_packet.tv_usec += usec_to_add;
- next_send_packet.tv_sec += sec_to_add;
- next_send_packet.tv_sec += next_send_packet.tv_usec / 1000000;
- next_send_packet.tv_usec %= 1000000;
+ next_send_packet = offset_timeval_seconds(now, seconds_per_byte * bytes_sent);
}
#include "reorderer.h"
#include "protocol.h"
+#include "timeutil.h"
#define PACKET_BUFFER_SIZE 1000
#define TIMEOUT_SEC 1.000
using namespace std;
-double tdiff(const timeval& a, const timeval& b)
-{
- return b.tv_sec - a.tv_sec +
- 1e-6 * (b.tv_usec - a.tv_usec);
-}
-
Reorderer::Reorderer(Sender* sender)
: sender(sender), last_seq(-1)
{
--- /dev/null
+#include <sys/time.h>
+
+#include <algorithm>
+
+#include "timeutil.h"
+
+using namespace std;
+
+double tdiff(const timeval& a, const timeval& b)
+{
+ return b.tv_sec - a.tv_sec +
+ 1e-6 * (b.tv_usec - a.tv_usec);
+}
+
+bool less_than(const timeval &a, const timeval &b)
+{
+ return make_pair(a.tv_sec, a.tv_usec) < make_pair(b.tv_sec, b.tv_usec);
+}
+
+timeval subtract_timeval_saturate(const timeval &a, const timeval &b)
+{
+ timeval ret;
+ if (less_than(a, b)) {
+ ret.tv_sec = ret.tv_usec = 0;
+ return ret;
+ }
+ ret.tv_sec = b.tv_sec - a.tv_sec;
+ ret.tv_usec = b.tv_usec - a.tv_usec;
+ if (ret.tv_usec < 0) {
+ ret.tv_usec += 1000000;
+ --ret.tv_sec;
+ }
+ return ret;
+}
+
+timeval offset_timeval_seconds(const timeval &a, double s)
+{
+ int usec_to_add = lrint(1e6 * s);
+ int sec_to_add = usec_to_add / 1000000;
+ usec_to_add %= 1000000;
+
+ timeval ret = a;
+ ret.tv_usec += usec_to_add;
+ ret.tv_sec += sec_to_add;
+ ret.tv_sec += ret.tv_usec / 1000000;
+ ret.tv_usec %= 1000000;
+ return ret;
+}
--- /dev/null
+#ifndef _TIMEUTIL_H
+#define _TIMEUTIL_H 1
+
+#include <sys/time.h>
+
+double tdiff(const timeval& a, const timeval& b);
+bool less_than(const timeval &a, const timeval &b);
+timeval subtract_timeval_saturate(const timeval &a, const timeval &b);
+timeval offset_timeval_seconds(const timeval &a, double s);
+
+#endif // !defined(_TIMEUTIL_H)