From f3448f1797b50e3d45bfbd66adcd10db1e0576fd Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sat, 20 Feb 2016 10:51:35 -0800 Subject: [PATCH] Use timercmp/sub and std::chrono to subtract timevals --- include/tins/packet_sender.h | 1 - src/packet_sender.cpp | 63 +++++++++++++++++------------------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/include/tins/packet_sender.h b/include/tins/packet_sender.h index 9315b88..337beed 100644 --- a/include/tins/packet_sender.h +++ b/include/tins/packet_sender.h @@ -384,7 +384,6 @@ private: PacketSender(const PacketSender&); PacketSender& operator=(const PacketSender&); int find_type(SocketType type); - int timeval_subtract (struct timeval* result, struct timeval* x, struct timeval* y); #ifndef _WIN32 bool ether_socket_initialized(const NetworkInterface& iface = NetworkInterface()) const; int get_ether_socket(const NetworkInterface& iface = NetworkInterface()); diff --git a/src/packet_sender.cpp b/src/packet_sender.cpp index e390c34..dac8521 100644 --- a/src/packet_sender.cpp +++ b/src/packet_sender.cpp @@ -65,6 +65,10 @@ #include "radiotap.h" #include "ieee802_3.h" #include "internals.h" +#include "cxxstd.h" +#if TINS_IS_CXX11 + #include +#endif // TINS_IS_CXX11 using std::string; using std::ostringstream; @@ -472,44 +476,37 @@ PDU* PacketSender::recv_match_loop(const vector& sockets, } } } - struct timeval this_time, diff; - #ifdef _WIN32 - // fixme + #if TINS_IS_CXX11 + using namespace std::chrono; + microseconds end = seconds(end_time.tv_sec) + microseconds(end_time.tv_usec); + microseconds now = duration_cast(system_clock::now().time_since_epoch()); + if (now > end) { + return 0; + } + microseconds diff = end - now; + timeout.tv_sec = duration_cast(diff).count(); + timeout.tv_usec = (diff - seconds(timeout.tv_sec)).count(); #else - gettimeofday(&this_time, 0); - #endif // _WIN32 - if (timeval_subtract(&diff, &end_time, &this_time)) { - return 0; - } - timeout.tv_sec = diff.tv_sec; - timeout.tv_usec = diff.tv_usec; + #ifdef _WIN32 + // Can't do much + return 0; + #else + struct timeval now; + gettimeofday(&now, 0); + // If now > end_time + if (timercmp(&now, &end_time, >)) { + return 0; + } + struct timeval diff; + timersub(&end_time, &now, &diff); + timeout.tv_sec = diff.tv_sec; + timeout.tv_usec = diff.tv_usec; + #endif // _WIN32 + #endif // TINS_IS_CXX11 } return 0; } -int PacketSender::timeval_subtract (struct timeval* result, struct timeval* x, struct timeval* y) { - /* Perform the carry for the later subtraction by updating y. */ - if (x->tv_usec < y->tv_usec) { - int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; - y->tv_usec -= 1000000 * nsec; - y->tv_sec += nsec; - } - - if (x->tv_usec - y->tv_usec > 1000000) { - int nsec = (x->tv_usec - y->tv_usec) / 1000000; - y->tv_usec += 1000000 * nsec; - y->tv_sec -= nsec; - } - - /* Compute the time remaining to wait. - tv_usec is certainly positive. */ - result->tv_sec = x->tv_sec - y->tv_sec; - result->tv_usec = x->tv_usec - y->tv_usec; - - /* Return 1 if result is negative. */ - return x->tv_sec < y->tv_sec; -} - int PacketSender::find_type(SocketType type) { SocketTypeMap::iterator it = types_.find(type); if (it == types_.end()) {