mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-27 04:48:21 +01:00
Trying to get rid of EINTR problems...
This commit is contained in:
parent
076451c753
commit
02ef4e7daa
@ -181,7 +181,7 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp)
|
|||||||
* Tries to connect to remote address (address, port)
|
* Tries to connect to remote address (address, port)
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
const char *inet_tryconnect(p_sock ps, const char *address,
|
const char *inet_tryconnect(p_sock ps, const char *address,
|
||||||
unsigned short port)
|
unsigned short port, int timeout)
|
||||||
{
|
{
|
||||||
struct sockaddr_in remote;
|
struct sockaddr_in remote;
|
||||||
const char *err;
|
const char *err;
|
||||||
@ -197,14 +197,12 @@ const char *inet_tryconnect(p_sock ps, const char *address,
|
|||||||
memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
|
memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
|
||||||
}
|
}
|
||||||
} else remote.sin_family = AF_UNSPEC;
|
} else remote.sin_family = AF_UNSPEC;
|
||||||
sock_setblocking(ps);
|
err = sock_connect(ps, (SA *) &remote, sizeof(remote), timeout);
|
||||||
err = sock_connect(ps, (SA *) &remote, sizeof(remote));
|
|
||||||
if (err) {
|
if (err) {
|
||||||
sock_destroy(ps);
|
sock_destroy(ps);
|
||||||
*ps = SOCK_INVALID;
|
*ps = SOCK_INVALID;
|
||||||
return err;
|
return err;
|
||||||
} else {
|
} else {
|
||||||
sock_setnonblocking(ps);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
|
|
||||||
void inet_open(lua_State *L);
|
void inet_open(lua_State *L);
|
||||||
const char *inet_tryconnect(p_sock ps, const char *address,
|
const char *inet_tryconnect(p_sock ps, const char *address,
|
||||||
unsigned short port);
|
unsigned short port, int timeout);
|
||||||
const char *inet_trybind(p_sock ps, const char *address,
|
const char *inet_trybind(p_sock ps, const char *address,
|
||||||
unsigned short port, int backlog);
|
unsigned short port, int backlog);
|
||||||
const char *inet_trycreate(p_sock ps, int type);
|
const char *inet_trycreate(p_sock ps, int type);
|
||||||
|
1
src/io.h
1
src/io.h
@ -24,6 +24,7 @@ enum {
|
|||||||
IO_CLOSED, /* the connection has been closed */
|
IO_CLOSED, /* the connection has been closed */
|
||||||
IO_ERROR, /* something wrong... */
|
IO_ERROR, /* something wrong... */
|
||||||
IO_REFUSED, /* transfer has been refused */
|
IO_REFUSED, /* transfer has been refused */
|
||||||
|
IO_RETRY, /* please try again */
|
||||||
IO_LIMITED /* maximum number of bytes reached */
|
IO_LIMITED /* maximum number of bytes reached */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ const char *sock_create(p_sock ps, int domain, int type, int protocol);
|
|||||||
void sock_destroy(p_sock ps);
|
void sock_destroy(p_sock ps);
|
||||||
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len,
|
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len,
|
||||||
int timeout);
|
int timeout);
|
||||||
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len);
|
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, int timeout);
|
||||||
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len);
|
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len);
|
||||||
void sock_listen(p_sock ps, int backlog);
|
void sock_listen(p_sock ps, int backlog);
|
||||||
void sock_shutdown(p_sock ps, int how);
|
void sock_shutdown(p_sock ps, int how);
|
||||||
|
@ -257,7 +257,10 @@ static int meth_connect(lua_State *L)
|
|||||||
p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1);
|
p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1);
|
||||||
const char *address = luaL_checkstring(L, 2);
|
const char *address = luaL_checkstring(L, 2);
|
||||||
unsigned short port = (unsigned short) luaL_checknumber(L, 3);
|
unsigned short port = (unsigned short) luaL_checknumber(L, 3);
|
||||||
const char *err = inet_tryconnect(&tcp->sock, address, port);
|
p_tm tm = &tcp->tm;
|
||||||
|
const char *err;
|
||||||
|
tm_markstart(tm);
|
||||||
|
err = inet_tryconnect(&tcp->sock, address, port, tm_getfailure(tm));
|
||||||
if (err) {
|
if (err) {
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
lua_pushstring(L, err);
|
lua_pushstring(L, err);
|
||||||
@ -326,7 +329,7 @@ static int meth_getsockname(lua_State *L)
|
|||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
static int meth_settimeout(lua_State *L)
|
static int meth_settimeout(lua_State *L)
|
||||||
{
|
{
|
||||||
p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1);
|
p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1);
|
||||||
return tm_meth_settimeout(L, &tcp->tm);
|
return tm_meth_settimeout(L, &tcp->tm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -335,12 +335,14 @@ static int meth_settimeout(lua_State *L)
|
|||||||
static int meth_setpeername(lua_State *L)
|
static int meth_setpeername(lua_State *L)
|
||||||
{
|
{
|
||||||
p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1);
|
p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1);
|
||||||
|
p_tm tm = &udp->tm;
|
||||||
const char *address = luaL_checkstring(L, 2);
|
const char *address = luaL_checkstring(L, 2);
|
||||||
int connecting = strcmp(address, "*");
|
int connecting = strcmp(address, "*");
|
||||||
unsigned short port = connecting ?
|
unsigned short port = connecting ?
|
||||||
(unsigned short) luaL_checknumber(L, 3) :
|
(unsigned short) luaL_checknumber(L, 3) :
|
||||||
(unsigned short) luaL_optnumber(L, 3, 0);
|
(unsigned short) luaL_optnumber(L, 3, 0);
|
||||||
const char *err = inet_tryconnect(&udp->sock, address, port);
|
const char *err;
|
||||||
|
err = inet_tryconnect(&udp->sock, address, port, tm_getfailure(tm));
|
||||||
if (err) {
|
if (err) {
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
lua_pushstring(L, err);
|
lua_pushstring(L, err);
|
||||||
|
@ -2,6 +2,17 @@
|
|||||||
* Socket compatibilization module for Unix
|
* Socket compatibilization module for Unix
|
||||||
* LuaSocket toolkit
|
* LuaSocket toolkit
|
||||||
*
|
*
|
||||||
|
* We are now treating EINTRs, but if an interrupt happens in the middle of
|
||||||
|
* a select function call, we don't guarantee values timeouts anymore.
|
||||||
|
* It's not a big deal, since we are not real-time anyways.
|
||||||
|
*
|
||||||
|
* We also exchanged the order of the calls to send/recv and select.
|
||||||
|
* The idea is that the outer loop (whoever is calling sock_send/recv)
|
||||||
|
* will call the function again if we didn't time out, so we can
|
||||||
|
* call write and then select only if it fails. This moves the penalty
|
||||||
|
* to when data is not available, maximizing the bandwidth if data is
|
||||||
|
* always available.
|
||||||
|
*
|
||||||
* RCS ID: $Id$
|
* RCS ID: $Id$
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -49,10 +60,27 @@ const char *sock_create(p_sock ps, int domain, int type, int protocol)
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Connects or returns error message
|
* Connects or returns error message
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len)
|
const char *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 "closed";
|
||||||
|
if (connect(sock, addr, addr_len) < 0) {
|
||||||
|
struct timeval tv;
|
||||||
|
fd_set wfds, efds;
|
||||||
|
int err;
|
||||||
|
tv.tv_sec = timeout / 1000;
|
||||||
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
|
FD_ZERO(&wfds); FD_ZERO(&efds);
|
||||||
|
FD_SET(sock, &wfds); FD_SET(sock, &efds);
|
||||||
|
do err = select(sock+1, NULL, &wfds, &efds, timeout >= 0 ? &tv : NULL);
|
||||||
|
while (err < 0 && errno == EINTR);
|
||||||
|
if (err <= 0) return "timeout";
|
||||||
|
if (FD_ISSET(sock, &efds)) {
|
||||||
|
char dummy;
|
||||||
|
recv(sock, &dummy, 0, 0);
|
||||||
|
return sock_connectstrerror();
|
||||||
|
} else return NULL;
|
||||||
|
} else return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
@ -91,14 +119,16 @@ int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len,
|
|||||||
SA dummy_addr;
|
SA dummy_addr;
|
||||||
socklen_t dummy_len;
|
socklen_t dummy_len;
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
|
int err;
|
||||||
if (sock == SOCK_INVALID) return IO_CLOSED;
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
||||||
tv.tv_sec = timeout / 1000;
|
tv.tv_sec = timeout / 1000;
|
||||||
tv.tv_usec = (timeout % 1000) * 1000;
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(sock, &fds);
|
FD_SET(sock, &fds);
|
||||||
*pa = SOCK_INVALID;
|
*pa = SOCK_INVALID;
|
||||||
if (select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL) <= 0)
|
do err = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
|
||||||
return IO_TIMEOUT;
|
while (err < 0 && errno == EINTR);
|
||||||
|
if (err <= 0) return IO_TIMEOUT;
|
||||||
if (!addr) addr = &dummy_addr;
|
if (!addr) addr = &dummy_addr;
|
||||||
if (!addr_len) addr_len = &dummy_len;
|
if (!addr_len) addr_len = &dummy_len;
|
||||||
*pa = accept(sock, addr, addr_len);
|
*pa = accept(sock, addr, addr_len);
|
||||||
@ -108,12 +138,6 @@ int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len,
|
|||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Send with timeout
|
* Send with timeout
|
||||||
* Here we exchanged the order of the calls to write and select
|
|
||||||
* The idea is that the outer loop (whoever is calling sock_send)
|
|
||||||
* will call the function again if we didn't time out, so we can
|
|
||||||
* call write and then select only if it fails.
|
|
||||||
* Should speed things up!
|
|
||||||
* We are also treating EINTR and EPIPE errors.
|
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
|
int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
|
||||||
int timeout)
|
int timeout)
|
||||||
@ -138,7 +162,8 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
|
|||||||
tv.tv_usec = (timeout % 1000) * 1000;
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(sock, &fds);
|
FD_SET(sock, &fds);
|
||||||
ret = select(sock+1, NULL, &fds, NULL, timeout >= 0 ? &tv : NULL);
|
do ret = select(sock+1, NULL, &fds, NULL, timeout >= 0 ?&tv : NULL);
|
||||||
|
while (ret < 0 && errno == EINTR);
|
||||||
/* tell the caller to call us again because there is more data */
|
/* tell the caller to call us again because there is more data */
|
||||||
if (ret > 0) return IO_DONE;
|
if (ret > 0) return IO_DONE;
|
||||||
/* tell the caller there was no data before timeout */
|
/* tell the caller there was no data before timeout */
|
||||||
@ -178,7 +203,8 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
|
|||||||
tv.tv_usec = (timeout % 1000) * 1000;
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(sock, &fds);
|
FD_SET(sock, &fds);
|
||||||
ret = select(sock+1, NULL, &fds, NULL, timeout >= 0 ? &tv : NULL);
|
do ret = select(sock+1, NULL, &fds, NULL, timeout >= 0? &tv: NULL);
|
||||||
|
while (ret < 0 && errno == EINTR);
|
||||||
/* tell the caller to call us again because there is more data */
|
/* tell the caller to call us again because there is more data */
|
||||||
if (ret > 0) return IO_DONE;
|
if (ret > 0) return IO_DONE;
|
||||||
/* tell the caller there was no data before timeout */
|
/* tell the caller there was no data before timeout */
|
||||||
@ -199,7 +225,6 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
|
|||||||
* will call the function again if we didn't time out, so we can
|
* will call the function again if we didn't time out, so we can
|
||||||
* call write and then select only if it fails.
|
* call write and then select only if it fails.
|
||||||
* Should speed things up!
|
* Should speed things up!
|
||||||
* We are also treating EINTR errors.
|
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
|
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
|
||||||
{
|
{
|
||||||
@ -218,7 +243,8 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
|
|||||||
tv.tv_usec = (timeout % 1000) * 1000;
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(sock, &fds);
|
FD_SET(sock, &fds);
|
||||||
ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
|
do ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
|
||||||
|
while (ret < 0 && errno == EINTR);
|
||||||
if (ret > 0) return IO_DONE;
|
if (ret > 0) return IO_DONE;
|
||||||
else return IO_TIMEOUT;
|
else return IO_TIMEOUT;
|
||||||
} else {
|
} else {
|
||||||
@ -248,7 +274,8 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
|
|||||||
tv.tv_usec = (timeout % 1000) * 1000;
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(sock, &fds);
|
FD_SET(sock, &fds);
|
||||||
ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
|
do ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
|
||||||
|
while (ret < 0 && errno == EINTR);
|
||||||
if (ret > 0) return IO_DONE;
|
if (ret > 0) return IO_DONE;
|
||||||
else return IO_TIMEOUT;
|
else return IO_TIMEOUT;
|
||||||
} else {
|
} else {
|
||||||
|
@ -2,6 +2,13 @@
|
|||||||
* Socket compatibilization module for Win32
|
* Socket compatibilization module for Win32
|
||||||
* LuaSocket toolkit
|
* LuaSocket toolkit
|
||||||
*
|
*
|
||||||
|
* We also exchanged the order of the calls to send/recv and select.
|
||||||
|
* The idea is that the outer loop (whoever is calling sock_send/recv)
|
||||||
|
* will call the function again if we didn't time out, so we can
|
||||||
|
* call write and then select only if it fails. This moves the penalty
|
||||||
|
* to when data is not available, maximizing the bandwidth if data is
|
||||||
|
* always available.
|
||||||
|
*
|
||||||
* RCS ID: $Id$
|
* RCS ID: $Id$
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
Loading…
Reference in New Issue
Block a user