From 8f01c9a51e32891b862489e4082d2c20aa1fc883 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Tue, 9 Jun 2015 00:21:45 +0200 Subject: Improve sender and sender_client 1) Add sockguard in sockutils.hh to help with closing sockets 2) Make sender fork in background 3) Make sender_client start a sender if needed (and sender_bin specified in config) --- src/sockutils.cc | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/sockutils.cc') diff --git a/src/sockutils.cc b/src/sockutils.cc index 53e3ef0..1bca300 100644 --- a/src/sockutils.cc +++ b/src/sockutils.cc @@ -1,6 +1,8 @@ #include "common.hh" #include +#include +#include #include "sockutils.hh" @@ -20,4 +22,38 @@ bool make_nonblocking(int sock) { return true; } +bool calc_timeout(const struct timeval* target, struct timeval* timeout) { + gettimeofday(timeout, nullptr); + if (target->tv_sec == timeout->tv_sec) { + timeout->tv_sec = 0; + if (target->tv_usec >= timeout->tv_usec) { + timeout->tv_usec = target->tv_usec - timeout->tv_usec; + } else { + return false; + } + } else if (target->tv_sec > timeout->tv_sec) { + timeout->tv_sec = target->tv_sec - timeout->tv_sec; + if (target->tv_usec >= timeout->tv_usec) { + timeout->tv_usec = target->tv_usec - timeout->tv_usec; + } else { + timeout->tv_sec--; + timeout->tv_usec = 1000000l + target->tv_usec - timeout->tv_usec; + } + } else { + return false; + } + return true; +} + +// static +void sockguard::close(int sock) { + ::close(sock); +} + } // namespace stuff + +namespace std { +void swap(stuff::sockguard& s1, stuff::sockguard& s2) noexcept { + s1.swap(s2); +} +} // namespace std -- cgit v1.2.3-70-g09d2