From 2ea5c13b47daf62bccdcaa2cb6f971383df54ad7 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 8 Feb 2015 23:29:28 +0100 Subject: [PATCH] Moved time functions into a shared file. --- Makefile | 4 ++-- pacer.cpp | 30 +++--------------------------- reorderer.cpp | 7 +------ timeutil.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ timeutil.h | 11 +++++++++++ 5 files changed, 65 insertions(+), 35 deletions(-) create mode 100644 timeutil.cpp create mode 100644 timeutil.h diff --git a/Makefile b/Makefile index b4634d8..d06a6f4 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ 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) diff --git a/pacer.cpp b/pacer.cpp index b0683f4..d049067 100644 --- a/pacer.cpp +++ b/pacer.cpp @@ -1,4 +1,5 @@ #include "pacer.h" +#include "timeutil.h" #include @@ -11,11 +12,6 @@ Pacer::Pacer(Sender *sender, int max_rate_kbit_per_sec, int burst_num_packets) 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}}); @@ -30,20 +26,8 @@ void Pacer::possibly_adjust_tv(timeval *tv) 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; } @@ -69,14 +53,6 @@ void Pacer::possibly_flush_packets() 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); } diff --git a/reorderer.cpp b/reorderer.cpp index 0512802..1fe3c08 100644 --- a/reorderer.cpp +++ b/reorderer.cpp @@ -2,18 +2,13 @@ #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) { diff --git a/timeutil.cpp b/timeutil.cpp new file mode 100644 index 0000000..b3d58a7 --- /dev/null +++ b/timeutil.cpp @@ -0,0 +1,48 @@ +#include + +#include + +#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; +} diff --git a/timeutil.h b/timeutil.h new file mode 100644 index 0000000..93d6973 --- /dev/null +++ b/timeutil.h @@ -0,0 +1,11 @@ +#ifndef _TIMEUTIL_H +#define _TIMEUTIL_H 1 + +#include + +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) -- 2.39.2