aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/except.c2
-rw-r--r--src/ftp.lua2
-rw-r--r--src/tcp.c2
-rw-r--r--src/timeout.c26
-rw-r--r--src/usocket.c11
-rw-r--r--src/wsocket.c5
6 files changed, 28 insertions, 20 deletions
diff --git a/src/except.c b/src/except.c
index 68abf70..ad03817 100644
--- a/src/except.c
+++ b/src/except.c
@@ -29,7 +29,7 @@ static luaL_reg func[] = {
29* Try factory 29* Try factory
30\*-------------------------------------------------------------------------*/ 30\*-------------------------------------------------------------------------*/
31static int finalize(lua_State *L) { 31static int finalize(lua_State *L) {
32 if (lua_isnil(L, 1) || (lua_isboolean(L, 1) && !lua_toboolean(L, 1))) { 32 if (!lua_toboolean(L, 1)) {
33 lua_pushvalue(L, lua_upvalueindex(1)); 33 lua_pushvalue(L, lua_upvalueindex(1));
34 lua_pcall(L, 0, 0, 0); 34 lua_pcall(L, 0, 0, 0);
35 lua_settop(L, 2); 35 lua_settop(L, 2);
diff --git a/src/ftp.lua b/src/ftp.lua
index 1c7ea71..4e2bb62 100644
--- a/src/ftp.lua
+++ b/src/ftp.lua
@@ -32,7 +32,7 @@ local metat = { __index = {} }
32 32
33function open(server, port) 33function open(server, port)
34 local tp = socket.try(tp.connect(server, port or PORT, TIMEOUT)) 34 local tp = socket.try(tp.connect(server, port or PORT, TIMEOUT))
35 local f = setmetat({ tp = tp }, metat) 35 local f = setmetatable({ tp = tp }, metat)
36 -- make sure everything gets closed in an exception 36 -- make sure everything gets closed in an exception
37 f.try = socket.newtry(function() f:close() end) 37 f.try = socket.newtry(function() f:close() end)
38 return f 38 return f
diff --git a/src/tcp.c b/src/tcp.c
index 845e0a3..adc2585 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -283,7 +283,7 @@ static int meth_getpeername(lua_State *L)
283 283
284static int meth_getsockname(lua_State *L) 284static int meth_getsockname(lua_State *L)
285{ 285{
286 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); 286 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1);
287 return inet_meth_getsockname(L, &tcp->sock); 287 return inet_meth_getsockname(L, &tcp->sock);
288} 288}
289 289
diff --git a/src/timeout.c b/src/timeout.c
index 3472ca7..e089051 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -18,12 +18,6 @@
18#else 18#else
19#include <time.h> 19#include <time.h>
20#include <sys/time.h> 20#include <sys/time.h>
21#include <sys/times.h>
22#include <unistd.h>
23#ifndef CLK_TCK
24/* CLI_TCK is now obsolete in Linux */
25#define CLK_TCK (sysconf(_SC_CLK_TCK));
26#endif
27#endif 21#endif
28 22
29/* min and max macros */ 23/* min and max macros */
@@ -37,11 +31,11 @@
37/*=========================================================================*\ 31/*=========================================================================*\
38* Internal function prototypes 32* Internal function prototypes
39\*=========================================================================*/ 33\*=========================================================================*/
40static int tm_lua_time(lua_State *L); 34static int tm_lua_gettime(lua_State *L);
41static int tm_lua_sleep(lua_State *L); 35static int tm_lua_sleep(lua_State *L);
42 36
43static luaL_reg func[] = { 37static luaL_reg func[] = {
44 { "time", tm_lua_time }, 38 { "gettime", tm_lua_gettime },
45 { "sleep", tm_lua_sleep }, 39 { "sleep", tm_lua_sleep },
46 { NULL, NULL } 40 { NULL, NULL }
47}; 41};
@@ -141,8 +135,10 @@ int tm_gettime(void)
141#else 135#else
142int tm_gettime(void) 136int tm_gettime(void)
143{ 137{
144 struct tms t; 138 struct timeval v;
145 return (times(&t)*1000)/CLK_TCK; 139 struct timezone z = {0, 0};
140 gettimeofday(&v, &z);
141 return v.tv_sec * 1000 + v.tv_usec/1000;
146} 142}
147#endif 143#endif
148 144
@@ -186,7 +182,7 @@ int tm_meth_settimeout(lua_State *L, p_tm tm)
186/*-------------------------------------------------------------------------*\ 182/*-------------------------------------------------------------------------*\
187* Returns the time the system has been up, in secconds. 183* Returns the time the system has been up, in secconds.
188\*-------------------------------------------------------------------------*/ 184\*-------------------------------------------------------------------------*/
189static int tm_lua_time(lua_State *L) 185static int tm_lua_gettime(lua_State *L)
190{ 186{
191 lua_pushnumber(L, tm_gettime()/1000.0); 187 lua_pushnumber(L, tm_gettime()/1000.0);
192 return 1; 188 return 1;
@@ -199,9 +195,13 @@ int tm_lua_sleep(lua_State *L)
199{ 195{
200 double n = luaL_checknumber(L, 1); 196 double n = luaL_checknumber(L, 1);
201#ifdef _WIN32 197#ifdef _WIN32
202 Sleep((int)n*1000); 198 Sleep((int)(n*1000));
203#else 199#else
204 sleep((int)n); 200 struct timespec t, r;
201 t.tv_sec = (int) n;
202 n -= t.tv_sec;
203 t.tv_nsec = (int) (n * 1000000000) % 1000000000;
204 nanosleep(&t, &r);
205#endif 205#endif
206 return 0; 206 return 0;
207} 207}
diff --git a/src/usocket.c b/src/usocket.c
index 6b4182b..ea0f172 100644
--- a/src/usocket.c
+++ b/src/usocket.c
@@ -95,7 +95,8 @@ const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm)
95 /* don't call on closed socket */ 95 /* don't call on closed socket */
96 if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED); 96 if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
97 /* ask system to connect */ 97 /* ask system to connect */
98 err = connect(sock, addr, addr_len); 98 do err = connect(sock, addr, addr_len);
99 while (err < 0 && errno == EINTR);
99 /* if no error, we're done */ 100 /* if no error, we're done */
100 if (err == 0) return NULL; 101 if (err == 0) return NULL;
101 /* make sure the system is trying to connect */ 102 /* make sure the system is trying to connect */
@@ -174,9 +175,13 @@ const char *sock_accept(p_sock ps, p_sock pa, SA *addr,
174 int err; 175 int err;
175 fd_set fds; 176 fd_set fds;
176 /* try to accept */ 177 /* try to accept */
177 *pa = accept(sock, addr, addr_len); 178 do *pa = accept(sock, addr, addr_len);
179 while (*pa < 0 && errno == EINTR);
178 /* if result is valid, we are done */ 180 /* if result is valid, we are done */
179 if (*pa != SOCK_INVALID) return NULL; 181 if (*pa != SOCK_INVALID) {
182 sock_setnonblocking(pa);
183 return NULL;
184 }
180 /* find out if we failed for a fatal reason */ 185 /* find out if we failed for a fatal reason */
181 if (errno != EWOULDBLOCK && errno != ECONNABORTED) 186 if (errno != EWOULDBLOCK && errno != ECONNABORTED)
182 return sock_acceptstrerror(errno); 187 return sock_acceptstrerror(errno);
diff --git a/src/wsocket.c b/src/wsocket.c
index 08c1046..84a49dc 100644
--- a/src/wsocket.c
+++ b/src/wsocket.c
@@ -177,7 +177,10 @@ const char *sock_accept(p_sock ps, p_sock pa, SA *addr,
177 /* try to get client socket */ 177 /* try to get client socket */
178 *pa = accept(sock, addr, addr_len); 178 *pa = accept(sock, addr, addr_len);
179 /* if return is valid, we are done */ 179 /* if return is valid, we are done */
180 if (*pa != SOCK_INVALID) return NULL; 180 if (*pa != SOCK_INVALID) {
181 sock_setnonblocking(pa);
182 return NULL;
183 }
181 /* optimization */ 184 /* optimization */
182 if (timeout == 0) return io_strerror(IO_TIMEOUT); 185 if (timeout == 0) return io_strerror(IO_TIMEOUT);
183 /* otherwise find out why we failed */ 186 /* otherwise find out why we failed */