diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-01-19 16:18:31 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-01-19 16:18:31 +0000 |
| commit | fbb42b80cb0d299f38e0a4df9b0fa01228b39225 (patch) | |
| tree | 06483f7d02f1dfbc5fbbae2c2364b64264a13f3a | |
| parent | 46d8f4eabe5e96a248a17a2f1583d61d9307a7d2 (diff) | |
| download | luasocket-fbb42b80cb0d299f38e0a4df9b0fa01228b39225.tar.gz luasocket-fbb42b80cb0d299f38e0a4df9b0fa01228b39225.tar.bz2 luasocket-fbb42b80cb0d299f38e0a4df9b0fa01228b39225.zip | |
Seems to be working on windows and linux.
| -rw-r--r-- | luasocket.vcproj | 9 | ||||
| -rw-r--r-- | src/timeout.c | 1 | ||||
| -rw-r--r-- | src/wsocket.c | 42 | ||||
| -rw-r--r-- | 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 | |||
| @@ -125,12 +125,6 @@ | |||
| 125 | RelativePath=".\buffer.c"> | 125 | RelativePath=".\buffer.c"> |
| 126 | </File> | 126 | </File> |
| 127 | <File | 127 | <File |
| 128 | RelativePath=".\code.c"> | ||
| 129 | </File> | ||
| 130 | <File | ||
| 131 | RelativePath=".\error.c"> | ||
| 132 | </File> | ||
| 133 | <File | ||
| 134 | RelativePath=".\inet.c"> | 128 | RelativePath=".\inet.c"> |
| 135 | </File> | 129 | </File> |
| 136 | <File | 130 | <File |
| @@ -143,6 +137,9 @@ | |||
| 143 | RelativePath=".\luasocket.c"> | 137 | RelativePath=".\luasocket.c"> |
| 144 | </File> | 138 | </File> |
| 145 | <File | 139 | <File |
| 140 | RelativePath=".\mime.c"> | ||
| 141 | </File> | ||
| 142 | <File | ||
| 146 | RelativePath=".\select.c"> | 143 | RelativePath=".\select.c"> |
| 147 | </File> | 144 | </File> |
| 148 | <File | 145 | <File |
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 @@ | |||
| 21 | #include <sys/times.h> | 21 | #include <sys/times.h> |
| 22 | #include <unistd.h> | 22 | #include <unistd.h> |
| 23 | #ifndef CLK_TCK | 23 | #ifndef CLK_TCK |
| 24 | /* CLI_TCK is now obsolete in Linux */ | ||
| 24 | #define CLK_TCK (sysconf(_SC_CLK_TCK)); | 25 | #define CLK_TCK (sysconf(_SC_CLK_TCK)); |
| 25 | #endif | 26 | #endif |
| 26 | #endif | 27 | #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) | |||
| 53 | /*-------------------------------------------------------------------------*\ | 53 | /*-------------------------------------------------------------------------*\ |
| 54 | * Creates and sets up a socket | 54 | * Creates and sets up a socket |
| 55 | \*-------------------------------------------------------------------------*/ | 55 | \*-------------------------------------------------------------------------*/ |
| 56 | const char *sock_create(p_sock ps, int domain, int type, int protocol) | 56 | int sock_create(p_sock ps, int domain, int type, int protocol) |
| 57 | { | 57 | { |
| 58 | int val = 1; | 58 | int val = 1; |
| 59 | t_sock sock = socket(domain, type, protocol); | 59 | t_sock sock = socket(domain, type, protocol); |
| 60 | if (sock == SOCK_INVALID) return sock_createstrerror(); | 60 | if (sock == SOCK_INVALID) return IO_ERROR; |
| 61 | *ps = sock; | 61 | *ps = sock; |
| 62 | sock_setnonblocking(ps); | 62 | sock_setnonblocking(ps); |
| 63 | setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val)); | 63 | setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val)); |
| 64 | return NULL; | 64 | return IO_DONE; |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | /*-------------------------------------------------------------------------*\ | 67 | /*-------------------------------------------------------------------------*\ |
| 68 | * Connects or returns error message | 68 | * Connects or returns error message |
| 69 | \*-------------------------------------------------------------------------*/ | 69 | \*-------------------------------------------------------------------------*/ |
| 70 | const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len) | 70 | int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, int timeout) |
| 71 | { | 71 | { |
| 72 | if (connect(*ps, addr, addr_len) < 0) return sock_connectstrerror(); | 72 | t_sock sock = *ps; |
| 73 | else return NULL; | 73 | if (sock == SOCK_INVALID) return IO_CLOSED; |
| 74 | /* if connect fails, we have to find out why */ | ||
| 75 | if (connect(sock, addr, addr_len) < 0) { | ||
| 76 | int err; | ||
| 77 | struct timeval tv; | ||
| 78 | fd_set efds, wfds; | ||
| 79 | /* make sure the system is trying to connect */ | ||
| 80 | if (WSAGetLastError() != WSAEWOULDBLOCK) return IO_ERROR; | ||
| 81 | tv.tv_sec = timeout / 1000; | ||
| 82 | tv.tv_usec = (timeout % 1000) * 1000; | ||
| 83 | FD_ZERO(&wfds); FD_SET(sock, &wfds); | ||
| 84 | FD_ZERO(&efds); FD_SET(sock, &efds); | ||
| 85 | /* we run select to avoid busy waiting */ | ||
| 86 | err = select(0, NULL, &wfds, &efds, timeout >= 0? &tv: NULL); | ||
| 87 | /* if select returned due to an event */ | ||
| 88 | if (err > 0 ) { | ||
| 89 | /* the sets tell whether it was a sucess or failure */ | ||
| 90 | if (FD_ISSET(sock,&efds) || !FD_ISSET(sock,&wfds)) return IO_ERROR; | ||
| 91 | else return IO_DONE; | ||
| 92 | /* if nothing happened, we timed out */ | ||
| 93 | } else if (err == 0) return IO_TIMEOUT; | ||
| 94 | /* otherwise, I don't know what happened */ | ||
| 95 | else return IO_ERROR; | ||
| 96 | /* otherwise, it worked */ | ||
| 97 | } else return IO_DONE; | ||
| 74 | } | 98 | } |
| 75 | 99 | ||
| 76 | /*-------------------------------------------------------------------------*\ | 100 | /*-------------------------------------------------------------------------*\ |
| 77 | * Binds or returns error message | 101 | * Binds or returns error message |
| 78 | \*-------------------------------------------------------------------------*/ | 102 | \*-------------------------------------------------------------------------*/ |
| 79 | const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len) | 103 | int sock_bind(p_sock ps, SA *addr, socklen_t addr_len) |
| 80 | { | 104 | { |
| 81 | if (bind(*ps, addr, addr_len) < 0) return sock_bindstrerror(); | 105 | if (bind(*ps, addr, addr_len) < 0) return IO_ERROR; |
| 82 | else return NULL; | 106 | else return IO_DONE; |
| 83 | } | 107 | } |
| 84 | 108 | ||
| 85 | /*-------------------------------------------------------------------------*\ | 109 | /*-------------------------------------------------------------------------*\ |
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() | |||
| 425 | test("accept with timeout (if it hangs, it failed:)") | 425 | test("accept with timeout (if it hangs, it failed:)") |
| 426 | accept_timeout() | 426 | accept_timeout() |
| 427 | 427 | ||
| 428 | test("accept with timeout (if it hangs, it failed:)") | 428 | test("connect with timeout (if it hangs, it failed:)") |
| 429 | connect_timeout() | 429 | connect_timeout() |
| 430 | 430 | ||
| 431 | test("mixed patterns") | 431 | test("mixed patterns") |
| @@ -499,8 +499,6 @@ test_raw(200) | |||
| 499 | test_raw(17) | 499 | test_raw(17) |
| 500 | test_raw(1) | 500 | test_raw(1) |
| 501 | 501 | ||
| 502 | |||
| 503 | a = [[ | ||
| 504 | test("total timeout on send") | 502 | test("total timeout on send") |
| 505 | test_totaltimeoutsend(800091, 1, 3) | 503 | test_totaltimeoutsend(800091, 1, 3) |
| 506 | test_totaltimeoutsend(800091, 2, 3) | 504 | test_totaltimeoutsend(800091, 2, 3) |
| @@ -524,6 +522,5 @@ test_blockingtimeoutreceive(800091, 1, 3) | |||
| 524 | test_blockingtimeoutreceive(800091, 2, 3) | 522 | test_blockingtimeoutreceive(800091, 2, 3) |
| 525 | test_blockingtimeoutreceive(800091, 3, 2) | 523 | test_blockingtimeoutreceive(800091, 3, 2) |
| 526 | test_blockingtimeoutreceive(800091, 3, 1) | 524 | test_blockingtimeoutreceive(800091, 3, 1) |
| 527 | ]] | ||
| 528 | 525 | ||
| 529 | test(string.format("done in %.2fs", socket.time() - start)) | 526 | test(string.format("done in %.2fs", socket.time() - start)) |
