summaryrefslogtreecommitdiff
path: root/src/sockutils.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2015-06-09 00:21:45 +0200
committerJoel Klinghed <the_jk@yahoo.com>2015-06-09 00:21:45 +0200
commit8f01c9a51e32891b862489e4082d2c20aa1fc883 (patch)
treec8f21cb9768c0853da6f68decae316c1f9c7a2f5 /src/sockutils.cc
parent913cfd1c7ef7a145036a8416d4ea815cb5cdb601 (diff)
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)
Diffstat (limited to 'src/sockutils.cc')
-rw-r--r--src/sockutils.cc36
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