Fixing bugs...

This commit is contained in:
Diego Nehab 2004-06-20 22:19:54 +00:00
parent 5dc5c3ebe8
commit f7579db9e8
24 changed files with 127 additions and 94 deletions

View file

@ -29,7 +29,7 @@ static luaL_reg func[] = {
* Try factory
\*-------------------------------------------------------------------------*/
static int finalize(lua_State *L) {
if (lua_isnil(L, 1) || (lua_isboolean(L, 1) && !lua_toboolean(L, 1))) {
if (!lua_toboolean(L, 1)) {
lua_pushvalue(L, lua_upvalueindex(1));
lua_pcall(L, 0, 0, 0);
lua_settop(L, 2);

View file

@ -32,7 +32,7 @@ local metat = { __index = {} }
function open(server, port)
local tp = socket.try(tp.connect(server, port or PORT, TIMEOUT))
local f = setmetat({ tp = tp }, metat)
local f = setmetatable({ tp = tp }, metat)
-- make sure everything gets closed in an exception
f.try = socket.newtry(function() f:close() end)
return f

View file

@ -283,7 +283,7 @@ static int meth_getpeername(lua_State *L)
static int meth_getsockname(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 inet_meth_getsockname(L, &tcp->sock);
}

View file

@ -18,12 +18,6 @@
#else
#include <time.h>
#include <sys/time.h>
#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
/* min and max macros */
@ -37,11 +31,11 @@
/*=========================================================================*\
* Internal function prototypes
\*=========================================================================*/
static int tm_lua_time(lua_State *L);
static int tm_lua_gettime(lua_State *L);
static int tm_lua_sleep(lua_State *L);
static luaL_reg func[] = {
{ "time", tm_lua_time },
{ "gettime", tm_lua_gettime },
{ "sleep", tm_lua_sleep },
{ NULL, NULL }
};
@ -141,8 +135,10 @@ int tm_gettime(void)
#else
int tm_gettime(void)
{
struct tms t;
return (times(&t)*1000)/CLK_TCK;
struct timeval v;
struct timezone z = {0, 0};
gettimeofday(&v, &z);
return v.tv_sec * 1000 + v.tv_usec/1000;
}
#endif
@ -186,7 +182,7 @@ int tm_meth_settimeout(lua_State *L, p_tm tm)
/*-------------------------------------------------------------------------*\
* Returns the time the system has been up, in secconds.
\*-------------------------------------------------------------------------*/
static int tm_lua_time(lua_State *L)
static int tm_lua_gettime(lua_State *L)
{
lua_pushnumber(L, tm_gettime()/1000.0);
return 1;
@ -199,9 +195,13 @@ int tm_lua_sleep(lua_State *L)
{
double n = luaL_checknumber(L, 1);
#ifdef _WIN32
Sleep((int)n*1000);
Sleep((int)(n*1000));
#else
sleep((int)n);
struct timespec t, r;
t.tv_sec = (int) n;
n -= t.tv_sec;
t.tv_nsec = (int) (n * 1000000000) % 1000000000;
nanosleep(&t, &r);
#endif
return 0;
}

View file

@ -95,7 +95,8 @@ const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm)
/* don't call on closed socket */
if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
/* ask system to connect */
err = connect(sock, addr, addr_len);
do err = connect(sock, addr, addr_len);
while (err < 0 && errno == EINTR);
/* if no error, we're done */
if (err == 0) return NULL;
/* make sure the system is trying to connect */
@ -174,9 +175,13 @@ const char *sock_accept(p_sock ps, p_sock pa, SA *addr,
int err;
fd_set fds;
/* try to accept */
*pa = accept(sock, addr, addr_len);
do *pa = accept(sock, addr, addr_len);
while (*pa < 0 && errno == EINTR);
/* if result is valid, we are done */
if (*pa != SOCK_INVALID) return NULL;
if (*pa != SOCK_INVALID) {
sock_setnonblocking(pa);
return NULL;
}
/* find out if we failed for a fatal reason */
if (errno != EWOULDBLOCK && errno != ECONNABORTED)
return sock_acceptstrerror(errno);

View file

@ -177,7 +177,10 @@ const char *sock_accept(p_sock ps, p_sock pa, SA *addr,
/* try to get client socket */
*pa = accept(sock, addr, addr_len);
/* if return is valid, we are done */
if (*pa != SOCK_INVALID) return NULL;
if (*pa != SOCK_INVALID) {
sock_setnonblocking(pa);
return NULL;
}
/* optimization */
if (timeout == 0) return io_strerror(IO_TIMEOUT);
/* otherwise find out why we failed */