6 #include "include/types.h"
7 #include <sys/socket.h>
11 static const int nofile = -1;
20 flags_t(const file_t & f) : f_(f), flags_(fcntl(f_.fd_, F_GETFL, NULL)) { }
21 ~flags_t() { fcntl(f_.fd_, F_SETFL, flags_); }
22 operator int & () { return flags_; }
25 inline file_t(int fd=nofile) : fd_(fd) {}
26 inline file_t(const file_t &) = delete;
27 inline file_t(file_t && other) : fd_(nofile) { std::swap(fd_, other.fd_); }
28 inline ~file_t() { close(); }
29 static inline void pipe(file_t *ends) {
33 VERIFY(::pipe(fds) == 0);
37 inline operator int() const { if (fd_ == nofile) throw "no fd"; return fd_; }
38 inline flags_t flags() const { return {*this}; }
44 template <class T> inline typename std::enable_if<std::is_pod<T>::value, ssize_t>::type
45 read(T & t) const { return ::read(fd_, &t, sizeof(T)); }
46 inline ssize_t read(void * t, size_t n) const { return ::read(fd_, t, n); }
47 template <class T> inline typename std::enable_if<std::is_pod<T>::value, ssize_t>::type
48 write(const T & t) const { return ::write(fd_, &t, sizeof(T)); }
49 inline ssize_t write(const void * t, size_t n) const { return ::write(fd_, t, n); }
52 class socket_t : public file_t {
54 socket_t(int fd=nofile) : file_t(fd) {}
56 int setsockopt(int level, int option, T && value) {
57 return ::setsockopt(*this, level, option, &value, sizeof(T));
59 inline int shutdown(int how) { return ::shutdown(*this, how); }