1
0
mirror of https://gitlab.xiph.org/xiph/icecast-common.git synced 2024-12-04 14:46:31 -05:00

make sure sock_connected does not block with timeout 0, use -1

for that, also handle the sock_connected return values properly
in the case of timeout.

svn path=/icecast/trunk/net/; revision=7349
This commit is contained in:
Karl Heyes 2004-07-26 16:03:52 +00:00
parent 0d5a600afc
commit 265752a303
2 changed files with 17 additions and 10 deletions

View File

@ -448,20 +448,22 @@ int sock_read_line(sock_t sock, char *buff, const int len)
} }
} }
/* see if a connection has been established /* see if a connection has been established. If timeout is < 0 then wait
* indefinitely, else wait for the stated number of seconds.
* return SOCK_TIMEOUT for timeout * return SOCK_TIMEOUT for timeout
* return SOCK_ERROR for failure * return SOCK_ERROR for failure
* return 0 for try again, interrupted * return 0 for try again, interrupted
* return 1 for ok * return 1 for ok
*/ */
int sock_connected (int sock, unsigned timeout) int sock_connected (int sock, int timeout)
{ {
fd_set wfds; fd_set wfds;
int val = SOCK_ERROR; int val = SOCK_ERROR;
socklen_t size = sizeof val; socklen_t size = sizeof val;
struct timeval tv, *timeval = NULL; struct timeval tv, *timeval = NULL;
if (timeout) /* make a timeout of <0 be indefinite */
if (timeout >= 0)
{ {
tv.tv_sec = timeout; tv.tv_sec = timeout;
tv.tv_usec = 0; tv.tv_usec = 0;
@ -531,8 +533,11 @@ int sock_connect_non_blocking (const char *hostname, const unsigned port)
return sock; return sock;
} }
/* issue a connect, but return after the timeout (seconds) is reached. If
sock_t sock_connect_wto(const char *hostname, const int port, const int timeout) * timeout is 0 or less then we will wait until the OS gives up on the connect
* The socket is returned
*/
sock_t sock_connect_wto(const char *hostname, int port, int timeout)
{ {
int sock = SOCK_ERROR; int sock = SOCK_ERROR;
struct addrinfo *ai, *head, hints; struct addrinfo *ai, *head, hints;
@ -551,7 +556,7 @@ sock_t sock_connect_wto(const char *hostname, const int port, const int timeout)
{ {
if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) >= 0) if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) >= 0)
{ {
if (timeout) if (timeout > 0)
sock_set_blocking (sock, SOCK_NONBLOCK); sock_set_blocking (sock, SOCK_NONBLOCK);
if (connect (sock, ai->ai_addr, ai->ai_addrlen) == 0) if (connect (sock, ai->ai_addr, ai->ai_addrlen) == 0)
@ -562,13 +567,15 @@ sock_t sock_connect_wto(const char *hostname, const int port, const int timeout)
{ {
if (sock_recoverable (sock_error())) if (sock_recoverable (sock_error()))
{ {
if (sock_connected (sock, timeout) > 0) int connected = sock_connected (sock, timeout);
if (connected == 0) /* try again, interrupted */
continue;
if (connected == 1) /* connected */
{ {
if (timeout) /* really wants to reset to what it was before */ if (timeout >= 0)
sock_set_blocking(sock, SOCK_BLOCK); sock_set_blocking(sock, SOCK_BLOCK);
break; break;
} }
continue;
} }
sock_close (sock); sock_close (sock);
sock = SOCK_ERROR; sock = SOCK_ERROR;

View File

@ -114,7 +114,7 @@ int sock_close(sock_t sock);
/* Connection related socket functions */ /* Connection related socket functions */
sock_t sock_connect_wto(const char *hostname, const int port, const int timeout); sock_t sock_connect_wto(const char *hostname, const int port, const int timeout);
int sock_connect_non_blocking(const char *host, const unsigned port); int sock_connect_non_blocking(const char *host, const unsigned port);
int sock_connected(int sock, unsigned timeout); int sock_connected(int sock, int timeout);
/* Socket write functions */ /* Socket write functions */
int sock_write_bytes(sock_t sock, const void *buff, const size_t len); int sock_write_bytes(sock_t sock, const void *buff, const size_t len);