42 # include <netinet/in.h>
43 # include <netinet/tcp.h>
46 #include <string_view>
52 double timeout_connect,
bool tcp_nodelay,
53 const string& context)
56 int connect_errno = 0;
57 for (
auto&& r :
Resolver(hostname, port)) {
64 socktype |= SOCK_NONBLOCK;
66 int fd = socket(r.ai_family, socktype, r.ai_protocol);
70 #if !defined __WIN32__ && defined F_SETFD && defined FD_CLOEXEC
75 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
82 return ioctlsocket(fd, FIONBIO, &on);
84 #define FLAG_NAME "FIONBIO"
85 #elif defined O_NONBLOCK
86 int rc = fcntl(fd, F_SETFL, O_NONBLOCK);
87 #define FLAG_NAME "O_NONBLOCK"
89 int rc = fcntl(fd, F_SETFL, O_NDELAY);
90 #define FLAG_NAME "O_NDELAY"
107 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
108 reinterpret_cast<char*
>(&on),
118 int retval = connect(fd, r.ai_addr, r.ai_addrlen);
129 err == WSAEWOULDBLOCK
137 fds.events = POLLOUT;
139 retval = poll(&fds, 1,
int(timeout_connect * 1000));
140 }
while (retval < 0 && (errno == EINTR || errno == EAGAIN));
141 # define FUNC_NAME "poll()"
149 retval = select(fd + 1, 0, &fdset, 0, &tv);
150 }
while (retval < 0 && (errno == EINTR || errno == EAGAIN));
151 # define FUNC_NAME "select()"
155 int saved_errno = errno;
175 retval = getsockopt(fd, SOL_SOCKET, SO_ERROR,
176 reinterpret_cast<char*
>(&err), &len);
195 if (connect_errno == 0)
210 fcntl(socketfd, F_SETFL, 0);
213 ioctlsocket(socketfd, FIONBIO, &off);
Indicates a problem communicating with a remote database.
Indicates a timeout expired while communicating with a remote database.
Hierarchy of classes which Xapian can throw as exceptions.
void to_timeval(double t, struct timeval *tv)
Fill in struct timeval from number of seconds in a double.
int open_socket(std::string_view hostname, int port, double timeout_connect, bool tcp_nodelay, const std::string &context)
Attempt to open a TCP/IP socket connection to a server.
Functions for handling a time or time interval in a double.
Resolve hostnames and ip addresses.
include <fcntl.h>, but working around broken platforms.
include <sys/select.h> with portability workarounds.
include <sys/socket.h> with portability workarounds.
Socket handling utilities.
Open a TCP connection to a server.