Trying to get connect-with-timeout to work. Darwin works...

This commit is contained in:
Diego Nehab 2004-01-18 00:04:20 +00:00
parent 02ef4e7daa
commit c8d58798f0
14 changed files with 232 additions and 198 deletions

View file

@ -46,50 +46,59 @@ void sock_destroy(p_sock ps)
/*-------------------------------------------------------------------------*\
* Creates and sets up a socket
\*-------------------------------------------------------------------------*/
const char *sock_create(p_sock ps, int domain, int type, int protocol)
int sock_create(p_sock ps, int domain, int type, int protocol)
{
int val = 1;
t_sock sock = socket(domain, type, protocol);
if (sock == SOCK_INVALID) return sock_createstrerror();
if (sock == SOCK_INVALID) return IO_ERROR;
*ps = sock;
sock_setnonblocking(ps);
setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val));
return NULL;
return IO_DONE;
}
/*-------------------------------------------------------------------------*\
* Connects or returns error message
\*-------------------------------------------------------------------------*/
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, int timeout)
int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, int timeout)
{
t_sock sock = *ps;
if (sock == SOCK_INVALID) return "closed";
if (sock == SOCK_INVALID) return IO_CLOSED;
/* if connect fails, we have to find out why */
if (connect(sock, addr, addr_len) < 0) {
struct timeval tv;
fd_set wfds, efds;
fd_set rfds, efds, wfds;
int err;
/* make sure the system is trying to connect */
if (errno != EINPROGRESS) return IO_ERROR;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&wfds); FD_ZERO(&efds);
FD_SET(sock, &wfds); FD_SET(sock, &efds);
do err = select(sock+1, NULL, &wfds, &efds, timeout >= 0 ? &tv : NULL);
while (err < 0 && errno == EINTR);
if (err <= 0) return "timeout";
if (FD_ISSET(sock, &efds)) {
FD_ZERO(&rfds); FD_SET(sock, &rfds);
FD_ZERO(&wfds); FD_SET(sock, &wfds);
FD_ZERO(&efds); FD_SET(sock, &efds);
/* we run select to avoid busy waiting */
err = select(sock+1, &rfds, &wfds, &efds, timeout >= 0? &tv: NULL);
/* if select was interrupted, ask the user to retry */
if (err < 0 && errno == EINTR) return IO_RETRY;
/* if selects readable, try reading */
if (err > 0) {
char dummy;
recv(sock, &dummy, 0, 0);
return sock_connectstrerror();
} else return NULL;
} else return NULL;
/* try reading so that errno is set */
if (recv(sock, &dummy, 0, 0) < 0) return IO_ERROR;
return IO_DONE;
/* if no event happened, there was a timeout */
} else return IO_TIMEOUT;
/* otherwise connection succeeded */
} else return IO_DONE;
}
/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len)
int sock_bind(p_sock ps, SA *addr, socklen_t addr_len)
{
if (bind(*ps, addr, addr_len) < 0) return sock_bindstrerror();
else return NULL;
if (bind(*ps, addr, addr_len) < 0) return IO_ERROR;
else return IO_DONE;
}
/*-------------------------------------------------------------------------*\
@ -115,25 +124,24 @@ int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len,
int timeout)
{
t_sock sock = *ps;
struct timeval tv;
SA dummy_addr;
socklen_t dummy_len;
fd_set fds;
int err;
if (sock == SOCK_INVALID) return IO_CLOSED;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
*pa = SOCK_INVALID;
do err = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
while (err < 0 && errno == EINTR);
if (err <= 0) return IO_TIMEOUT;
if (!addr) addr = &dummy_addr;
if (!addr_len) addr_len = &dummy_len;
*pa = accept(sock, addr, addr_len);
if (*pa == SOCK_INVALID) return IO_ERROR;
else return IO_DONE;
if (*pa == SOCK_INVALID) {
struct timeval tv;
fd_set fds;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
/* just call select to avoid busy-wait. doesn't really matter
* what happens. the caller will choose to retry or not */
select(sock+1, &fds, NULL, NULL, timeout >= 0? &tv: NULL);
return IO_RETRY;
} else return IO_DONE;
}
/*-------------------------------------------------------------------------*\
@ -144,32 +152,31 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
{
t_sock sock = *ps;
ssize_t put;
int ret;
/* avoid making system calls on closed sockets */
if (sock == SOCK_INVALID) return IO_CLOSED;
/* make sure we repeat in case the call was interrupted */
do put = write(sock, data, count);
while (put <= 0 && errno == EINTR);
do put = send(sock, data, count, 0);
while (put < 0 && errno == EINTR);
/* deal with failure */
if (put <= 0) {
struct timeval tv;
fd_set fds;
/* in any case, nothing has been sent */
*sent = 0;
/* run select to avoid busy wait */
if (errno != EPIPE) {
struct timeval tv;
fd_set fds;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
do ret = select(sock+1, NULL, &fds, NULL, timeout >= 0 ?&tv : NULL);
while (ret < 0 && errno == EINTR);
/* tell the caller to call us again because there is more data */
if (ret > 0) return IO_DONE;
/* tell the caller there was no data before timeout */
else return IO_TIMEOUT;
/* here we know the connection has been closed */
} else return IO_CLOSED;
if (errno == EPIPE) return IO_CLOSED;
/* run select to avoid busy wait */
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
if (select(sock+1, NULL, &fds, NULL, timeout >= 0? &tv: NULL) <= 0) {
/* here the call was interrupted. calling again might work */
if (errno == EINTR) return IO_RETRY;
/* here there was no data before timeout */
else return IO_TIMEOUT;
/* here we didn't send anything, but now we can */
} else return IO_DONE;
/* here we successfully sent something */
} else {
*sent = put;
@ -185,33 +192,22 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
{
t_sock sock = *ps;
ssize_t put;
int ret;
/* avoid making system calls on closed sockets */
if (sock == SOCK_INVALID) return IO_CLOSED;
/* make sure we repeat in case the call was interrupted */
do put = sendto(sock, data, count, 0, addr, addr_len);
while (put <= 0 && errno == EINTR);
/* deal with failure */
while (put < 0 && errno == EINTR);
if (put <= 0) {
/* in any case, nothing has been sent */
struct timeval tv;
fd_set fds;
*sent = 0;
/* run select to avoid busy wait */
if (errno != EPIPE) {
struct timeval tv;
fd_set fds;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
do ret = select(sock+1, NULL, &fds, NULL, timeout >= 0? &tv: NULL);
while (ret < 0 && errno == EINTR);
/* tell the caller to call us again because there is more data */
if (ret > 0) return IO_DONE;
/* tell the caller there was no data before timeout */
if (errno == EPIPE) return IO_CLOSED;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
if (select(sock+1, NULL, &fds, NULL, timeout >= 0? &tv: NULL) <= 0) {
if (errno == EINTR) return IO_RETRY;
else return IO_TIMEOUT;
/* here we know the connection has been closed */
} else return IO_CLOSED;
/* here we successfully sent something */
} else return IO_DONE;
} else {
*sent = put;
return IO_DONE;
@ -220,11 +216,6 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
/*-------------------------------------------------------------------------*\
* Receive with timeout
* Here we exchanged the order of the calls to write and select
* The idea is that the outer loop (whoever is calling sock_send)
* will call the function again if we didn't time out, so we can
* call write and then select only if it fails.
* Should speed things up!
\*-------------------------------------------------------------------------*/
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
{
@ -232,7 +223,7 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
ssize_t taken;
if (sock == SOCK_INVALID) return IO_CLOSED;
do taken = read(sock, data, count);
while (taken <= 0 && errno == EINTR);
while (taken < 0 && errno == EINTR);
if (taken <= 0) {
struct timeval tv;
fd_set fds;
@ -243,10 +234,10 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
do ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
while (ret < 0 && errno == EINTR);
if (ret > 0) return IO_DONE;
else return IO_TIMEOUT;
ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
if (ret < 0 && errno == EINTR) return IO_RETRY;
if (ret == 0) return IO_TIMEOUT;
else return IO_DONE;
} else {
*got = taken;
return IO_DONE;
@ -263,7 +254,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
ssize_t taken;
if (sock == SOCK_INVALID) return IO_CLOSED;
do taken = recvfrom(sock, data, count, 0, addr, addr_len);
while (taken <= 0 && errno == EINTR);
while (taken < 0 && errno == EINTR);
if (taken <= 0) {
struct timeval tv;
fd_set fds;
@ -274,10 +265,10 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
do ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
while (ret < 0 && errno == EINTR);
if (ret > 0) return IO_DONE;
else return IO_TIMEOUT;
ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
if (ret < 0 && errno == EINTR) return IO_RETRY;
if (ret == 0) return IO_TIMEOUT;
else return IO_DONE;
} else {
*got = taken;
return IO_DONE;