7 #include <sys/socket.h>
18 flags_t(const file_t & f) : f_(f), flags_(fcntl(f_.fd_, F_GETFL, NULL)) { }
19 ~flags_t() { fcntl(f_.fd_, F_SETFL, flags_); }
20 operator int & () { return flags_; }
23 inline file_t(int fd=-1) : fd_(fd) {}
24 inline file_t(const file_t &) = delete;
25 inline file_t(file_t && other) : fd_(-1) { std::swap(fd_, other.fd_); }
26 inline ~file_t() { if (fd_ != -1) ::close(fd_); }
27 static inline void pipe(file_t *ends) {
29 VERIFY(::pipe(fds) == 0);
33 inline operator int() const { if (fd_ == -1) throw "no fd"; return fd_; }
34 inline flags_t flags() const { return *this; }
40 inline ssize_t read(T & t) const { return ::read(fd_, &t, sizeof(T)); }
41 inline ssize_t read(void * t, size_t n) const { return ::read(fd_, t, n); }
43 inline ssize_t write(const T & t) const { return ::write(fd_, &t, sizeof(T)); }
44 inline ssize_t write(const void * t, size_t n) const { return ::write(fd_, t, n); }
47 class socket_t : public file_t {
49 socket_t(int fd=-1) : file_t(fd) {}
51 int setsockopt(int level, int option, T && value) {
52 return ::setsockopt(*this, level, option, &value, sizeof(T));