diff options
Diffstat (limited to 'src/sockutils.cc')
| -rw-r--r-- | src/sockutils.cc | 36 |
1 files changed, 36 insertions, 0 deletions
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 <fcntl.h> +#include <sys/time.h> +#include <unistd.h> #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 |
