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
RelativePath=".\buffer.c">
</File>
<File
RelativePath=".\code.c">
</File>
<File
RelativePath=".\error.c">
</File>
<File
RelativePath=".\inet.c">
</File>
@ -142,6 +136,9 @@
<File
RelativePath=".\luasocket.c">
</File>
<File
RelativePath=".\mime.c">
</File>
<File
RelativePath=".\select.c">
</File>

View File

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

View File

@ -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;
}
/*-------------------------------------------------------------------------*\

View File

@ -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))