aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-05-28 07:24:43 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-05-28 07:24:43 +0000
commitc98dc991998c724a3f6a1fdd90b5d1d8a80e3af3 (patch)
tree8d8b8aa856d8a3e822121d0915a63b8244f471bb
parent9297b074d53a00e1149250e0bbfa0871dcc5558f (diff)
downloadluasocket-c98dc991998c724a3f6a1fdd90b5d1d8a80e3af3.tar.gz
luasocket-c98dc991998c724a3f6a1fdd90b5d1d8a80e3af3.tar.bz2
luasocket-c98dc991998c724a3f6a1fdd90b5d1d8a80e3af3.zip
Bug feioso no UDP e possivelmente no TCP também.
-rw-r--r--etc/b64.lua4
-rw-r--r--etc/check-links.lua4
-rw-r--r--etc/dict.lua4
-rw-r--r--etc/qp.lua4
-rw-r--r--samples/cddb.lua3
-rw-r--r--samples/daytimeclnt.lua1
-rw-r--r--samples/echoclnt.lua15
-rw-r--r--samples/echosrvr.lua21
-rw-r--r--src/udp.c12
-rw-r--r--src/usocket.c8
-rw-r--r--src/wsocket.c21
-rw-r--r--test/mimetest.lua6
-rw-r--r--test/testclnt.lua2
-rw-r--r--test/testsrvr.lua4
14 files changed, 59 insertions, 50 deletions
diff --git a/etc/b64.lua b/etc/b64.lua
index 1993b01..b86b870 100644
--- a/etc/b64.lua
+++ b/etc/b64.lua
@@ -10,5 +10,5 @@ else
10 local wrap = mime.wrap() 10 local wrap = mime.wrap()
11 convert = ltn12.filter.chain(base64, wrap) 11 convert = ltn12.filter.chain(base64, wrap)
12end 12end
13source = ltn12.source.chain(source, convert) 13sink = ltn12.sink.chain(convert, sink)
14repeat until not ltn12.pump(source, sink) 14ltn12.pump.all(source, sink)
diff --git a/etc/check-links.lua b/etc/check-links.lua
index 03ca6de..c200dfb 100644
--- a/etc/check-links.lua
+++ b/etc/check-links.lua
@@ -4,6 +4,10 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7
8require"luasocket"
9require"http"
10
7socket.http.TIMEOUT = 10 11socket.http.TIMEOUT = 10
8 12
9cache = {} 13cache = {}
diff --git a/etc/dict.lua b/etc/dict.lua
index d11ac93..31359d9 100644
--- a/etc/dict.lua
+++ b/etc/dict.lua
@@ -4,6 +4,8 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7require"luasocket"
8
7function get_status(sock, valid) 9function get_status(sock, valid)
8 local line, err = sock:receive() 10 local line, err = sock:receive()
9 local code, par 11 local code, par
@@ -12,7 +14,7 @@ function get_status(sock, valid)
12 code = tonumber(code) 14 code = tonumber(code)
13 if code ~= valid then return code end 15 if code ~= valid then return code end
14 if code == 150 then 16 if code == 150 then
15 par = tonumber(socket.skip(2, string.find(line, "^%d%d%d (%d*)")) 17 par = tonumber(socket.skip(2, string.find(line, "^%d%d%d (%d*)")))
16 end 18 end
17 return nil, par 19 return nil, par
18end 20end
diff --git a/etc/qp.lua b/etc/qp.lua
index 08545db..5a00c4d 100644
--- a/etc/qp.lua
+++ b/etc/qp.lua
@@ -1,3 +1,5 @@
1require("ltn12")
2require("mime")
1local convert 3local convert
2arg = arg or {} 4arg = arg or {}
3local mode = arg and arg[1] or "-et" 5local mode = arg and arg[1] or "-et"
@@ -13,4 +15,4 @@ elseif mode == "-eb" then
13else convert = mime.decode("quoted-printable") end 15else convert = mime.decode("quoted-printable") end
14local source = ltn12.source.chain(ltn12.source.file(io.stdin), convert) 16local source = ltn12.source.chain(ltn12.source.file(io.stdin), convert)
15local sink = ltn12.sink.file(io.stdout) 17local sink = ltn12.sink.file(io.stdout)
16ltn12.pump(source, sink) 18ltn12.pump.all(source, sink)
diff --git a/samples/cddb.lua b/samples/cddb.lua
index 0ed7c71..09309e8 100644
--- a/samples/cddb.lua
+++ b/samples/cddb.lua
@@ -1,3 +1,6 @@
1require"luasocket"
2require"http"
3
1if not arg or not arg[1] or not arg[2] then 4if not arg or not arg[1] or not arg[2] then
2 print("luasocket cddb.lua <category> <disc-id> [<server>]") 5 print("luasocket cddb.lua <category> <disc-id> [<server>]")
3 os.exit(1) 6 os.exit(1)
diff --git a/samples/daytimeclnt.lua b/samples/daytimeclnt.lua
index 63f4017..ee7f652 100644
--- a/samples/daytimeclnt.lua
+++ b/samples/daytimeclnt.lua
@@ -4,6 +4,7 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7require"luasocket"
7host = host or "127.0.0.1" 8host = host or "127.0.0.1"
8port = port or 13 9port = port or 13
9if arg then 10if arg then
diff --git a/samples/echoclnt.lua b/samples/echoclnt.lua
index 56bd123..a3d75f3 100644
--- a/samples/echoclnt.lua
+++ b/samples/echoclnt.lua
@@ -4,6 +4,7 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7require"luasocket"
7host = host or "localhost" 8host = host or "localhost"
8port = port or 7 9port = port or 7
9if arg then 10if arg then
@@ -11,17 +12,13 @@ if arg then
11 port = arg[2] or port 12 port = arg[2] or port
12end 13end
13host = socket.dns.toip(host) 14host = socket.dns.toip(host)
14udp, err = socket.udp() 15udp = socket.try(socket.udp())
15assert(udp, err) 16socket.try(udp:setpeername(host, port))
16ret, err = udp:setpeername(host, port) 17print("Using remote host '" ..host.. "' and port " .. port .. "...")
17assert(ret, err)
18print("Using host '" ..host.. "' and port " .. port .. "...")
19while 1 do 18while 1 do
20 line = io.read() 19 line = io.read()
21 if not line then os.exit() end 20 if not line then os.exit() end
22 ret, err = udp:send(line) 21 socket.try(udp:send(line))
23 if not ret then print(err) os.exit() end 22 dgram = socket.try(udp:receive())
24 dgram, err = udp:receive()
25 if not dgram then print(err) os.exit() end
26 print(dgram) 23 print(dgram)
27end 24end
diff --git a/samples/echosrvr.lua b/samples/echosrvr.lua
index a7ed03c..9d99506 100644
--- a/samples/echosrvr.lua
+++ b/samples/echosrvr.lua
@@ -4,6 +4,7 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7require"luasocket"
7host = host or "127.0.0.1" 8host = host or "127.0.0.1"
8port = port or 7 9port = port or 7
9if arg then 10if arg then
@@ -11,19 +12,17 @@ if arg then
11 port = arg[2] or port 12 port = arg[2] or port
12end 13end
13print("Binding to host '" ..host.. "' and port " ..port.. "...") 14print("Binding to host '" ..host.. "' and port " ..port.. "...")
14udp, err = socket.udp() 15udp = socket.try(socket.udp())
15assert(udp, err) 16socket.try(udp:setsockname(host, port))
16ret, err = udp:setsockname(host, port) 17socket.try(udp:settimeout(5))
17assert(ret, err) 18ip, port = socket.try(udp:getsockname())
18udp:settimeout(5)
19ip, port = udp:getsockname()
20assert(ip, port)
21print("Waiting packets on " .. ip .. ":" .. port .. "...") 19print("Waiting packets on " .. ip .. ":" .. port .. "...")
22while 1 do 20while 1 do
23 dgram, ip, port = udp:receivefrom() 21 dgram, ip, port = udp:receivefrom()
24 if not dgram then print(ip) 22 if dgram then
25 else 23 print("Echoing '" .. dgram .. "' to " .. ip .. ":" .. port)
26 print("Echoing from " .. ip .. ":" .. port)
27 udp:sendto(dgram, ip, port) 24 udp:sendto(dgram, ip, port)
28 end 25 else
26 print(ip)
27 end
29end 28end
diff --git a/src/udp.c b/src/udp.c
index a2dff34..19cefe6 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -109,7 +109,8 @@ static int meth_send(lua_State *L)
109 int err; 109 int err;
110 const char *data = luaL_checklstring(L, 2, &count); 110 const char *data = luaL_checklstring(L, 2, &count);
111 tm_markstart(tm); 111 tm_markstart(tm);
112 err = sock_send(&udp->sock, data, count, &sent, tm_get(tm)); 112 do err = sock_send(&udp->sock, data, count, &sent, tm_getretry(tm));
113 while (err == IO_RETRY);
113 if (err == IO_DONE) lua_pushnumber(L, sent); 114 if (err == IO_DONE) lua_pushnumber(L, sent);
114 else lua_pushnil(L); 115 else lua_pushnil(L);
115 /* a 'closed' error on an unconnected means the target address was not 116 /* a 'closed' error on an unconnected means the target address was not
@@ -137,8 +138,9 @@ static int meth_sendto(lua_State *L)
137 addr.sin_family = AF_INET; 138 addr.sin_family = AF_INET;
138 addr.sin_port = htons(port); 139 addr.sin_port = htons(port);
139 tm_markstart(tm); 140 tm_markstart(tm);
140 err = sock_sendto(&udp->sock, data, count, &sent, 141 do err = sock_sendto(&udp->sock, data, count, &sent,
141 (SA *) &addr, sizeof(addr), tm_get(tm)); 142 (SA *) &addr, sizeof(addr), tm_get(tm));
143 while (err == IO_RETRY);
142 if (err == IO_DONE) lua_pushnumber(L, sent); 144 if (err == IO_DONE) lua_pushnumber(L, sent);
143 else lua_pushnil(L); 145 else lua_pushnil(L);
144 /* a 'closed' error on an unconnected means the target address was not 146 /* a 'closed' error on an unconnected means the target address was not
@@ -159,7 +161,8 @@ static int meth_receive(lua_State *L)
159 p_tm tm = &udp->tm; 161 p_tm tm = &udp->tm;
160 count = MIN(count, sizeof(buffer)); 162 count = MIN(count, sizeof(buffer));
161 tm_markstart(tm); 163 tm_markstart(tm);
162 err = sock_recv(&udp->sock, buffer, count, &got, tm_get(tm)); 164 do err = sock_recv(&udp->sock, buffer, count, &got, tm_get(tm));
165 while (err == IO_RETRY);
163 if (err == IO_DONE) lua_pushlstring(L, buffer, got); 166 if (err == IO_DONE) lua_pushlstring(L, buffer, got);
164 else lua_pushnil(L); 167 else lua_pushnil(L);
165 io_pusherror(L, err); 168 io_pusherror(L, err);
@@ -180,8 +183,9 @@ static int meth_receivefrom(lua_State *L)
180 p_tm tm = &udp->tm; 183 p_tm tm = &udp->tm;
181 tm_markstart(tm); 184 tm_markstart(tm);
182 count = MIN(count, sizeof(buffer)); 185 count = MIN(count, sizeof(buffer));
183 err = sock_recvfrom(&udp->sock, buffer, count, &got, 186 do err = sock_recvfrom(&udp->sock, buffer, count, &got,
184 (SA *) &addr, &addr_len, tm_get(tm)); 187 (SA *) &addr, &addr_len, tm_get(tm));
188 while (err == IO_RETRY);
185 if (err == IO_DONE) { 189 if (err == IO_DONE) {
186 lua_pushlstring(L, buffer, got); 190 lua_pushlstring(L, buffer, got);
187 lua_pushstring(L, inet_ntoa(addr.sin_addr)); 191 lua_pushstring(L, inet_ntoa(addr.sin_addr));
diff --git a/src/usocket.c b/src/usocket.c
index eb1a49a..9e6efd3 100644
--- a/src/usocket.c
+++ b/src/usocket.c
@@ -211,7 +211,7 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
211 /* here there was no data before timeout */ 211 /* here there was no data before timeout */
212 else return IO_TIMEOUT; 212 else return IO_TIMEOUT;
213 /* here we didn't send anything, but now we can */ 213 /* here we didn't send anything, but now we can */
214 } else return IO_DONE; 214 } else return IO_RETRY;
215 /* here we successfully sent something */ 215 /* here we successfully sent something */
216 } else { 216 } else {
217 *sent = put; 217 *sent = put;
@@ -239,7 +239,7 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
239 if (sock_select(sock+1, NULL, &fds, NULL, timeout) <= 0) { 239 if (sock_select(sock+1, NULL, &fds, NULL, timeout) <= 0) {
240 if (errno == EINTR) return IO_RETRY; 240 if (errno == EINTR) return IO_RETRY;
241 else return IO_TIMEOUT; 241 else return IO_TIMEOUT;
242 } else return IO_DONE; 242 } else return IO_RETRY;
243 } else { 243 } else {
244 *sent = put; 244 *sent = put;
245 return IO_DONE; 245 return IO_DONE;
@@ -266,7 +266,7 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
266 ret = sock_select(sock+1, &fds, NULL, NULL, timeout); 266 ret = sock_select(sock+1, &fds, NULL, NULL, timeout);
267 if (ret < 0 && errno == EINTR) return IO_RETRY; 267 if (ret < 0 && errno == EINTR) return IO_RETRY;
268 if (ret == 0) return IO_TIMEOUT; 268 if (ret == 0) return IO_TIMEOUT;
269 else return IO_DONE; 269 return IO_RETRY;
270 } else { 270 } else {
271 *got = taken; 271 *got = taken;
272 return IO_DONE; 272 return IO_DONE;
@@ -294,7 +294,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
294 ret = sock_select(sock+1, &fds, NULL, NULL, timeout); 294 ret = sock_select(sock+1, &fds, NULL, NULL, timeout);
295 if (ret < 0 && errno == EINTR) return IO_RETRY; 295 if (ret < 0 && errno == EINTR) return IO_RETRY;
296 if (ret == 0) return IO_TIMEOUT; 296 if (ret == 0) return IO_TIMEOUT;
297 else return IO_DONE; 297 return IO_RETRY;
298 } else { 298 } else {
299 *got = taken; 299 *got = taken;
300 return IO_DONE; 300 return IO_DONE;
diff --git a/src/wsocket.c b/src/wsocket.c
index af3f8d8..023f470 100644
--- a/src/wsocket.c
+++ b/src/wsocket.c
@@ -207,9 +207,9 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
207 FD_ZERO(&fds); 207 FD_ZERO(&fds);
208 FD_SET(sock, &fds); 208 FD_SET(sock, &fds);
209 ret = sock_select(0, NULL, &fds, NULL, timeout); 209 ret = sock_select(0, NULL, &fds, NULL, timeout);
210 /* tell the caller to call us again because there is more data */ 210 /* tell the caller to call us again because now we can send */
211 if (ret > 0) return IO_DONE; 211 if (ret > 0) return IO_RETRY;
212 /* tell the caller there was no data before timeout */ 212 /* tell the caller we can't send anything before timint out */
213 else return IO_TIMEOUT; 213 else return IO_TIMEOUT;
214 /* here we know the connection has been closed */ 214 /* here we know the connection has been closed */
215 } else return IO_CLOSED; 215 } else return IO_CLOSED;
@@ -229,27 +229,18 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
229 t_sock sock = *ps; 229 t_sock sock = *ps;
230 int put; 230 int put;
231 int ret; 231 int ret;
232 /* avoid making system calls on closed sockets */
233 if (sock == SOCK_INVALID) return IO_CLOSED; 232 if (sock == SOCK_INVALID) return IO_CLOSED;
234 /* try to send something */
235 put = sendto(sock, data, (int) count, 0, addr, addr_len); 233 put = sendto(sock, data, (int) count, 0, addr, addr_len);
236 /* deal with failure */
237 if (put <= 0) { 234 if (put <= 0) {
238 /* in any case, nothing has been sent */
239 *sent = 0; 235 *sent = 0;
240 /* run select to avoid busy wait */
241 if (WSAGetLastError() == WSAEWOULDBLOCK) { 236 if (WSAGetLastError() == WSAEWOULDBLOCK) {
242 fd_set fds; 237 fd_set fds;
243 FD_ZERO(&fds); 238 FD_ZERO(&fds);
244 FD_SET(sock, &fds); 239 FD_SET(sock, &fds);
245 ret = sock_select(0, NULL, &fds, NULL, timeout); 240 ret = sock_select(0, NULL, &fds, NULL, timeout);
246 /* tell the caller to call us again because there is more data */ 241 if (ret > 0) return IO_RETRY;
247 if (ret > 0) return IO_DONE;
248 /* tell the caller there was no data before timeout */
249 else return IO_TIMEOUT; 242 else return IO_TIMEOUT;
250 /* here we know the connection has been closed */
251 } else return IO_CLOSED; 243 } else return IO_CLOSED;
252 /* here we successfully sent something */
253 } else { 244 } else {
254 *sent = put; 245 *sent = put;
255 return IO_DONE; 246 return IO_DONE;
@@ -273,7 +264,7 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
273 FD_ZERO(&fds); 264 FD_ZERO(&fds);
274 FD_SET(sock, &fds); 265 FD_SET(sock, &fds);
275 ret = sock_select(0, &fds, NULL, NULL, timeout); 266 ret = sock_select(0, &fds, NULL, NULL, timeout);
276 if (ret > 0) return IO_DONE; 267 if (ret > 0) return IO_RETRY;
277 else return IO_TIMEOUT; 268 else return IO_TIMEOUT;
278 } else { 269 } else {
279 *got = taken; 270 *got = taken;
@@ -299,7 +290,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
299 FD_ZERO(&fds); 290 FD_ZERO(&fds);
300 FD_SET(sock, &fds); 291 FD_SET(sock, &fds);
301 ret = sock_select(0, &fds, NULL, NULL, timeout); 292 ret = sock_select(0, &fds, NULL, NULL, timeout);
302 if (ret > 0) return IO_DONE; 293 if (ret > 0) return IO_RETRY;
303 else return IO_TIMEOUT; 294 else return IO_TIMEOUT;
304 } else { 295 } else {
305 *got = taken; 296 *got = taken;
diff --git a/test/mimetest.lua b/test/mimetest.lua
index 3e57557..dea43d6 100644
--- a/test/mimetest.lua
+++ b/test/mimetest.lua
@@ -1,3 +1,7 @@
1require "luasocket"
2require "ltn12"
3require "mime"
4
1dofile("testsupport.lua") 5dofile("testsupport.lua")
2 6
3local qptest = "qptest.bin" 7local qptest = "qptest.bin"
@@ -92,7 +96,7 @@ local function transform(input, output, filter)
92 source = ltn12.source.chain(source, filter) 96 source = ltn12.source.chain(source, filter)
93 end 97 end
94 --what = not what 98 --what = not what
95 ltn12.pump(source, sink) 99 ltn12.pump.all(source, sink)
96end 100end
97 101
98local function encode_qptest(mode) 102local function encode_qptest(mode)
diff --git a/test/testclnt.lua b/test/testclnt.lua
index ecf419b..556e54d 100644
--- a/test/testclnt.lua
+++ b/test/testclnt.lua
@@ -1,3 +1,5 @@
1require "luasocket"
2
1host = host or "localhost" 3host = host or "localhost"
2port = port or "8080" 4port = port or "8080"
3 5
diff --git a/test/testsrvr.lua b/test/testsrvr.lua
index 5c05239..d172a9e 100644
--- a/test/testsrvr.lua
+++ b/test/testsrvr.lua
@@ -1,3 +1,5 @@
1require "luasocket"
2
1host = host or "localhost" 3host = host or "localhost"
2port = port or "8080" 4port = port or "8080"
3 5
@@ -22,8 +24,6 @@ while 1 do
22 print("server: closing connection...") 24 print("server: closing connection...")
23 break 25 break
24 end 26 end
25print(command);
26
27 (loadstring(command))() 27 (loadstring(command))()
28 end 28 end
29end 29end