diff options
Diffstat (limited to 'src/sockutils.hh')
| -rw-r--r-- | src/sockutils.hh | 62 |
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 */ |
