3 #ifndef _XDRPP_SOCKET_H_HEADER_INCLUDED_ 4 #define _XDRPP_SOCKET_H_HEADER_INCLUDED_ 1 9 #include <system_error> 19 #include <sys/types.h> 20 #include <sys/socket.h> 22 #include <netinet/in.h> 31 static constexpr type invalid = INVALID_SOCKET;
34 static constexpr type invalid = -1;
38 constexpr
sock_t() : fd_(invalid) {}
39 constexpr sock_t(type fd) : fd_(fd) {}
41 bool operator==(sock_t s)
const {
return fd_ == s.fd_; }
42 bool operator!=(sock_t s)
const {
return fd_ != s.fd_; }
43 bool operator<(sock_t s)
const {
return fd_ < s.fd_; }
44 explicit operator bool()
const {
return fd_ != invalid; }
45 type fd()
const {
return fd_; }
48 template<>
struct hash<
xdr::sock_t> {
50 using result_type = size_t;
52 size_t operator()(
const xdr::sock_t s)
const {
return s.fd(); }
56 constexpr sock_t invalid_sock{};
68 ssize_t read(sock_t s,
void *buf, std::size_t count);
69 ssize_t write(sock_t s,
const void *buf, std::size_t count);
70 ssize_t readv(sock_t s,
const struct iovec *iov,
int iovcnt);
71 ssize_t writev(sock_t s,
const struct iovec *iov,
int iovcnt);
102 void operator()(addrinfo *ai)
const { freeaddrinfo(ai); }
111 int socktype = SOCK_STREAM,
112 const char *service =
nullptr,
113 int family = AF_UNSPEC);
116 void get_numinfo(
const sockaddr *sa, socklen_t salen,
117 std::string *host, std::string *serv);
140 explicit operator bool()
const {
return s_ != invalid_sock; }
150 if (s_ != invalid_sock) {
155 void reset(
sock_t s) { clear(); s_ = s; }
166 return tcp_connect(ai.get());
168 unique_sock tcp_connect(
const char *host,
const char *service,
169 int family = AF_UNSPEC);
173 int family = AF_UNSPEC,
178 #endif // !_XDRPP_SOCKET_H_HEADER_INCLUDED_ unique_sock tcp_connect(const addrinfo *ai)
Try connecting to every addrinfo in a list until one succeeds.
void get_numinfo(const sockaddr *sa, socklen_t salen, std::string *host, std::string *serv)
Return printable versions of numeric host and port number.
void throw_sockerr(const char *)
Throw a system_error exception for the last socket error.
unique_sock tcp_connect1(const addrinfo *ai, bool ndelay)
Try connecting to the first addrinfo in a linked list.
Most of the xdrpp library is encapsulated in the xdr namespace.
A deleter to use std::unique_ptr with addrinfo structures (which must be freed recursively).
std::enable_if< xdr_traits< T >::is_struct &&xdr_traits< T >::xdr_defined, bool >::type operator==(const T &a, const T &b)
Equality for XDR structures.
std::unique_ptr< addrinfo, delete_addrinfo > unique_addrinfo
Automatically garbage-collected addrinfo pointer.
bool sock_eagain()
Returns true if the most recent (socket) error is a temporary error, such as EAGAIN, EWOULDBLOCK, or EINTR.
void set_nonblock(sock_t s)
Set the O_NONBLOCK flag on a socket.
sock_t release()
Return the file descriptor number, relinquishing ownership of it.
std::enable_if< xdr_traits< T >::is_struct &&xdr_traits< T >::xdr_defined, bool >::type operator<(const T &a, const T &b)
Ordering of XDR structures. See note at xdr::operator==.
sock_t accept(sock_t s, sockaddr *addr, socklen_t *addrlen)
Wrapper around accept for sock_t.
unique_addrinfo get_addrinfo(const char *host, int socktype, const char *service, int family)
Wrapper around getaddrinfo that returns a garbage-collected xdr::unique_addrinfo. ...
const std::error_category & gai_category()
Category for system errors dealing with DNS (getaddrinfo, etc.).
const char * sock_errmsg()
Last socket error message (strerror(errno) on POSIX).
unique_sock tcp_listen(const char *service, int family, int backlog)
Create bind a listening TCP socket.
Abstract away the type of a socket (for windows).
void create_selfpipe(sock_t ss[2])
Create a socket (or pipe on unix, where both are file descriptors) that is connected to itself...
void set_close_on_exec(sock_t s)
Set the close-on-exec flag of a file descriptor.