Seems to be working on windows and linux.

This commit is contained in:
Diego Nehab 2004-01-19 16:18:31 +00:00
parent 46d8f4eabe
commit fbb42b80cb
4 changed files with 38 additions and 19 deletions

View File

@ -124,12 +124,6 @@
<File <File
RelativePath=".\buffer.c"> RelativePath=".\buffer.c">
</File> </File>
<File
RelativePath=".\code.c">
</File>
<File
RelativePath=".\error.c">
</File>
<File <File
RelativePath=".\inet.c"> RelativePath=".\inet.c">
</File> </File>
@ -142,6 +136,9 @@
<File <File
RelativePath=".\luasocket.c"> RelativePath=".\luasocket.c">
</File> </File>
<File
RelativePath=".\mime.c">
</File>
<File <File
RelativePath=".\select.c"> RelativePath=".\select.c">
</File> </File>

View File

@ -21,6 +21,7 @@
#include <sys/times.h> #include <sys/times.h>
#include <unistd.h> #include <unistd.h>
#ifndef CLK_TCK #ifndef CLK_TCK
/* CLI_TCK is now obsolete in Linux */
#define CLK_TCK (sysconf(_SC_CLK_TCK)); #define CLK_TCK (sysconf(_SC_CLK_TCK));
#endif #endif
#endif #endif

View File

@ -53,33 +53,57 @@ void sock_shutdown(p_sock ps, int how)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Creates and sets up a socket * 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; int val = 1;
t_sock sock = socket(domain, type, protocol); t_sock sock = socket(domain, type, protocol);
if (sock == SOCK_INVALID) return sock_createstrerror(); if (sock == SOCK_INVALID) return IO_ERROR;
*ps = sock; *ps = sock;
sock_setnonblocking(ps); sock_setnonblocking(ps);
setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val)); setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val));
return NULL; return IO_DONE;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Connects or returns error message * 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(); t_sock sock = *ps;
else return NULL; 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 * 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(); if (bind(*ps, addr, addr_len) < 0) return IO_ERROR;
else return NULL; else return IO_DONE;
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\

View File

@ -425,7 +425,7 @@ test_closed()
test("accept with timeout (if it hangs, it failed:)") test("accept with timeout (if it hangs, it failed:)")
accept_timeout() accept_timeout()
test("accept with timeout (if it hangs, it failed:)") test("connect with timeout (if it hangs, it failed:)")
connect_timeout() connect_timeout()
test("mixed patterns") test("mixed patterns")
@ -499,8 +499,6 @@ test_raw(200)
test_raw(17) test_raw(17)
test_raw(1) test_raw(1)
a = [[
test("total timeout on send") test("total timeout on send")
test_totaltimeoutsend(800091, 1, 3) test_totaltimeoutsend(800091, 1, 3)
test_totaltimeoutsend(800091, 2, 3) test_totaltimeoutsend(800091, 2, 3)
@ -524,6 +522,5 @@ test_blockingtimeoutreceive(800091, 1, 3)
test_blockingtimeoutreceive(800091, 2, 3) test_blockingtimeoutreceive(800091, 2, 3)
test_blockingtimeoutreceive(800091, 3, 2) test_blockingtimeoutreceive(800091, 3, 2)
test_blockingtimeoutreceive(800091, 3, 1) test_blockingtimeoutreceive(800091, 3, 1)
]]
test(string.format("done in %.2fs", socket.time() - start)) test(string.format("done in %.2fs", socket.time() - start))