[xiph-commits] r7349 - icecast/trunk/net

karl at dactyl.lonelymoon.com karl
Mon Jul 26 09:03:55 PDT 2004


Author: karl
Date: Mon Jul 26 09:03:55 2004
New Revision: 7349

Modified:
icecast/trunk/net/sock.c
icecast/trunk/net/sock.h
Log:
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.


Modified: icecast/trunk/net/sock.c
===================================================================
--- icecast/trunk/net/sock.c	2004-07-26 13:35:02 UTC (rev 7348)
+++ icecast/trunk/net/sock.c	2004-07-26 16:03:52 UTC (rev 7349)
@@ -448,20 +448,22 @@
}
}

-/* 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_ERROR for failure
* return 0 for try again, interrupted
* return 1 for ok
*/
-int sock_connected (int sock, unsigned timeout)
+int sock_connected (int sock, int timeout)
{
fd_set wfds;
int val = SOCK_ERROR;
socklen_t size = sizeof val;
struct timeval tv, *timeval = NULL;

-    if (timeout)
+    /* make a timeout of <0 be indefinite */
+    if (timeout >= 0)
{
tv.tv_sec = timeout;
tv.tv_usec = 0;
@@ -531,8 +533,11 @@
return sock;
}

-
-sock_t sock_connect_wto(const char *hostname, const int port, const int timeout)
+/* issue a connect, but return after the timeout (seconds) is reached. If
+ * 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;
struct addrinfo *ai, *head, hints;
@@ -551,7 +556,7 @@
{
if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) >= 0)
{
-            if (timeout)
+            if (timeout > 0)
sock_set_blocking (sock, SOCK_NONBLOCK);

if (connect (sock, ai->ai_addr, ai->ai_addrlen) == 0)
@@ -562,13 +567,15 @@
{
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);
break;
}
-                    continue;
}
sock_close (sock);
sock = SOCK_ERROR;

Modified: icecast/trunk/net/sock.h
===================================================================
--- icecast/trunk/net/sock.h	2004-07-26 13:35:02 UTC (rev 7348)
+++ icecast/trunk/net/sock.h	2004-07-26 16:03:52 UTC (rev 7349)
@@ -114,7 +114,7 @@
/* Connection related socket functions */
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_connected(int sock, unsigned timeout);
+int sock_connected(int sock, int timeout);

/* Socket write functions */
int sock_write_bytes(sock_t sock, const void *buff, const size_t len);



More information about the commits mailing list