summaryrefslogtreecommitdiff
path: root/src/sockutils.hh
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.hh
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.hh')
-rw-r--r--src/sockutils.hh62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/sockutils.hh b/src/sockutils.hh
index 2c47daa..9ad85ab 100644
--- a/src/sockutils.hh
+++ b/src/sockutils.hh
@@ -5,6 +5,68 @@ namespace stuff {
bool make_nonblocking(int sock);
+bool calc_timeout(const struct timeval* target, struct timeval* timeout);
+
+class sockguard {
+public:
+ sockguard()
+ : sock_(-1) {
+ }
+ explicit sockguard(int sock)
+ : sock_(sock) {
+ }
+ sockguard(sockguard&& sock)
+ : sock_(sock.sock_) {
+ }
+ ~sockguard() {
+ reset();
+ }
+ sockguard& operator=(sockguard&& sock) {
+ reset(sock.sock_);
+ return *this;
+ }
+ void reset() {
+ if (sock_ != -1) {
+ close(sock_);
+ sock_ = -1;
+ }
+ }
+ void reset(int sock) {
+ if (sock_ != -1 && sock_ != sock) {
+ close(sock_);
+ }
+ sock_ = sock;
+ }
+ void swap(sockguard& sock) {
+ auto tmp = sock.sock_;
+ sock.sock_ = sock_;
+ sock_ = tmp;
+ }
+ operator bool() const {
+ return sock_ != -1;
+ }
+ int get() const {
+ return sock_;
+ }
+ int release() {
+ auto ret = sock_;
+ sock_ = -1;
+ return ret;
+ }
+
+protected:
+ sockguard(const sockguard&) = delete;
+ sockguard& operator=(const sockguard&) = delete;
+ static void close(int sock);
+
+private:
+ int sock_;
+};
+
} // namespace stuff
+namespace std {
+void swap(stuff::sockguard& s1, stuff::sockguard& s2) noexcept;
+} // namespace std
+
#endif /* SOCKUTILS_HH */