From fbb42b80cb0d299f38e0a4df9b0fa01228b39225 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Mon, 19 Jan 2004 16:18:31 +0000 Subject: [PATCH] Seems to be working on windows and linux. --- luasocket.vcproj | 9 +++------ src/timeout.c | 1 + src/wsocket.c | 42 +++++++++++++++++++++++++++++++++--------- test/testclnt.lua | 5 +---- 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/luasocket.vcproj b/luasocket.vcproj index b5c4d53..5a74880 100644 --- a/luasocket.vcproj +++ b/luasocket.vcproj @@ -124,12 +124,6 @@ - - - - @@ -142,6 +136,9 @@ + + diff --git a/src/timeout.c b/src/timeout.c index df199a0..09cb53d 100644 --- a/src/timeout.c +++ b/src/timeout.c @@ -21,6 +21,7 @@ #include #include #ifndef CLK_TCK +/* CLI_TCK is now obsolete in Linux */ #define CLK_TCK (sysconf(_SC_CLK_TCK)); #endif #endif diff --git a/src/wsocket.c b/src/wsocket.c index bf92a90..c0e28d9 100644 --- a/src/wsocket.c +++ b/src/wsocket.c @@ -53,33 +53,57 @@ void sock_shutdown(p_sock ps, int how) /*-------------------------------------------------------------------------*\ * 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 sock_connect(p_sock ps, SA *addr, socklen_t addr_len, int timeout) { - if (connect(*ps, addr, addr_len) < 0) return sock_connectstrerror(); - else return NULL; + t_sock sock = *ps; + if (sock == SOCK_INVALID) return IO_CLOSED; + /* if connect fails, we have to find out why */ + if (connect(sock, addr, addr_len) < 0) { + int err; + struct timeval tv; + fd_set efds, wfds; + /* make sure the system is trying to connect */ + if (WSAGetLastError() != WSAEWOULDBLOCK) return IO_ERROR; + tv.tv_sec = timeout / 1000; + tv.tv_usec = (timeout % 1000) * 1000; + FD_ZERO(&wfds); FD_SET(sock, &wfds); + FD_ZERO(&efds); FD_SET(sock, &efds); + /* we run select to avoid busy waiting */ + err = select(0, NULL, &wfds, &efds, timeout >= 0? &tv: NULL); + /* if select returned due to an event */ + if (err > 0 ) { + /* the sets tell whether it was a sucess or failure */ + if (FD_ISSET(sock,&efds) || !FD_ISSET(sock,&wfds)) return IO_ERROR; + else return IO_DONE; + /* if nothing happened, we timed out */ + } else if (err == 0) return IO_TIMEOUT; + /* otherwise, I don't know what happened */ + else return IO_ERROR; + /* otherwise, it worked */ + } 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; } /*-------------------------------------------------------------------------*\ diff --git a/test/testclnt.lua b/test/testclnt.lua index 270891b..5a001b3 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua @@ -425,7 +425,7 @@ test_closed() test("accept with timeout (if it hangs, it failed:)") accept_timeout() -test("accept with timeout (if it hangs, it failed:)") +test("connect with timeout (if it hangs, it failed:)") connect_timeout() test("mixed patterns") @@ -499,8 +499,6 @@ test_raw(200) test_raw(17) test_raw(1) - -a = [[ test("total timeout on send") test_totaltimeoutsend(800091, 1, 3) test_totaltimeoutsend(800091, 2, 3) @@ -524,6 +522,5 @@ test_blockingtimeoutreceive(800091, 1, 3) test_blockingtimeoutreceive(800091, 2, 3) test_blockingtimeoutreceive(800091, 3, 2) test_blockingtimeoutreceive(800091, 3, 1) -]] test(string.format("done in %.2fs", socket.time() - start))