Bug feioso no UDP e possivelmente no TCP também.

This commit is contained in:
Diego Nehab 2004-05-28 07:24:43 +00:00
parent 9297b074d5
commit c98dc99199
14 changed files with 59 additions and 50 deletions

View File

@ -10,5 +10,5 @@ else
local wrap = mime.wrap() local wrap = mime.wrap()
convert = ltn12.filter.chain(base64, wrap) convert = ltn12.filter.chain(base64, wrap)
end end
source = ltn12.source.chain(source, convert) sink = ltn12.sink.chain(convert, sink)
repeat until not ltn12.pump(source, sink) ltn12.pump.all(source, sink)

View File

@ -4,6 +4,10 @@
-- Author: Diego Nehab -- Author: Diego Nehab
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
require"luasocket"
require"http"
socket.http.TIMEOUT = 10 socket.http.TIMEOUT = 10
cache = {} cache = {}

View File

@ -4,6 +4,8 @@
-- Author: Diego Nehab -- Author: Diego Nehab
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
require"luasocket"
function get_status(sock, valid) function get_status(sock, valid)
local line, err = sock:receive() local line, err = sock:receive()
local code, par local code, par
@ -12,7 +14,7 @@ function get_status(sock, valid)
code = tonumber(code) code = tonumber(code)
if code ~= valid then return code end if code ~= valid then return code end
if code == 150 then if code == 150 then
par = tonumber(socket.skip(2, string.find(line, "^%d%d%d (%d*)")) par = tonumber(socket.skip(2, string.find(line, "^%d%d%d (%d*)")))
end end
return nil, par return nil, par
end end

View File

@ -1,3 +1,5 @@
require("ltn12")
require("mime")
local convert local convert
arg = arg or {} arg = arg or {}
local mode = arg and arg[1] or "-et" local mode = arg and arg[1] or "-et"
@ -13,4 +15,4 @@ elseif mode == "-eb" then
else convert = mime.decode("quoted-printable") end else convert = mime.decode("quoted-printable") end
local source = ltn12.source.chain(ltn12.source.file(io.stdin), convert) local source = ltn12.source.chain(ltn12.source.file(io.stdin), convert)
local sink = ltn12.sink.file(io.stdout) local sink = ltn12.sink.file(io.stdout)
ltn12.pump(source, sink) ltn12.pump.all(source, sink)

View File

@ -1,3 +1,6 @@
require"luasocket"
require"http"
if not arg or not arg[1] or not arg[2] then if not arg or not arg[1] or not arg[2] then
print("luasocket cddb.lua <category> <disc-id> [<server>]") print("luasocket cddb.lua <category> <disc-id> [<server>]")
os.exit(1) os.exit(1)

View File

@ -4,6 +4,7 @@
-- Author: Diego Nehab -- Author: Diego Nehab
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
require"luasocket"
host = host or "127.0.0.1" host = host or "127.0.0.1"
port = port or 13 port = port or 13
if arg then if arg then

View File

@ -4,6 +4,7 @@
-- Author: Diego Nehab -- Author: Diego Nehab
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
require"luasocket"
host = host or "localhost" host = host or "localhost"
port = port or 7 port = port or 7
if arg then if arg then
@ -11,17 +12,13 @@ if arg then
port = arg[2] or port port = arg[2] or port
end end
host = socket.dns.toip(host) host = socket.dns.toip(host)
udp, err = socket.udp() udp = socket.try(socket.udp())
assert(udp, err) socket.try(udp:setpeername(host, port))
ret, err = udp:setpeername(host, port) print("Using remote host '" ..host.. "' and port " .. port .. "...")
assert(ret, err)
print("Using host '" ..host.. "' and port " .. port .. "...")
while 1 do while 1 do
line = io.read() line = io.read()
if not line then os.exit() end if not line then os.exit() end
ret, err = udp:send(line) socket.try(udp:send(line))
if not ret then print(err) os.exit() end dgram = socket.try(udp:receive())
dgram, err = udp:receive()
if not dgram then print(err) os.exit() end
print(dgram) print(dgram)
end end

View File

@ -4,6 +4,7 @@
-- Author: Diego Nehab -- Author: Diego Nehab
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
require"luasocket"
host = host or "127.0.0.1" host = host or "127.0.0.1"
port = port or 7 port = port or 7
if arg then if arg then
@ -11,19 +12,17 @@ if arg then
port = arg[2] or port port = arg[2] or port
end end
print("Binding to host '" ..host.. "' and port " ..port.. "...") print("Binding to host '" ..host.. "' and port " ..port.. "...")
udp, err = socket.udp() udp = socket.try(socket.udp())
assert(udp, err) socket.try(udp:setsockname(host, port))
ret, err = udp:setsockname(host, port) socket.try(udp:settimeout(5))
assert(ret, err) ip, port = socket.try(udp:getsockname())
udp:settimeout(5)
ip, port = udp:getsockname()
assert(ip, port)
print("Waiting packets on " .. ip .. ":" .. port .. "...") print("Waiting packets on " .. ip .. ":" .. port .. "...")
while 1 do while 1 do
dgram, ip, port = udp:receivefrom() dgram, ip, port = udp:receivefrom()
if not dgram then print(ip) if dgram then
else print("Echoing '" .. dgram .. "' to " .. ip .. ":" .. port)
print("Echoing from " .. ip .. ":" .. port)
udp:sendto(dgram, ip, port) udp:sendto(dgram, ip, port)
else
print(ip)
end end
end end

View File

@ -109,7 +109,8 @@ static int meth_send(lua_State *L)
int err; int err;
const char *data = luaL_checklstring(L, 2, &count); const char *data = luaL_checklstring(L, 2, &count);
tm_markstart(tm); tm_markstart(tm);
err = sock_send(&udp->sock, data, count, &sent, tm_get(tm)); do err = sock_send(&udp->sock, data, count, &sent, tm_getretry(tm));
while (err == IO_RETRY);
if (err == IO_DONE) lua_pushnumber(L, sent); if (err == IO_DONE) lua_pushnumber(L, sent);
else lua_pushnil(L); else lua_pushnil(L);
/* a 'closed' error on an unconnected means the target address was not /* a 'closed' error on an unconnected means the target address was not
@ -137,8 +138,9 @@ static int meth_sendto(lua_State *L)
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(port); addr.sin_port = htons(port);
tm_markstart(tm); tm_markstart(tm);
err = sock_sendto(&udp->sock, data, count, &sent, do err = sock_sendto(&udp->sock, data, count, &sent,
(SA *) &addr, sizeof(addr), tm_get(tm)); (SA *) &addr, sizeof(addr), tm_get(tm));
while (err == IO_RETRY);
if (err == IO_DONE) lua_pushnumber(L, sent); if (err == IO_DONE) lua_pushnumber(L, sent);
else lua_pushnil(L); else lua_pushnil(L);
/* a 'closed' error on an unconnected means the target address was not /* a 'closed' error on an unconnected means the target address was not
@ -159,7 +161,8 @@ static int meth_receive(lua_State *L)
p_tm tm = &udp->tm; p_tm tm = &udp->tm;
count = MIN(count, sizeof(buffer)); count = MIN(count, sizeof(buffer));
tm_markstart(tm); tm_markstart(tm);
err = sock_recv(&udp->sock, buffer, count, &got, tm_get(tm)); do err = sock_recv(&udp->sock, buffer, count, &got, tm_get(tm));
while (err == IO_RETRY);
if (err == IO_DONE) lua_pushlstring(L, buffer, got); if (err == IO_DONE) lua_pushlstring(L, buffer, got);
else lua_pushnil(L); else lua_pushnil(L);
io_pusherror(L, err); io_pusherror(L, err);
@ -180,8 +183,9 @@ static int meth_receivefrom(lua_State *L)
p_tm tm = &udp->tm; p_tm tm = &udp->tm;
tm_markstart(tm); tm_markstart(tm);
count = MIN(count, sizeof(buffer)); count = MIN(count, sizeof(buffer));
err = sock_recvfrom(&udp->sock, buffer, count, &got, do err = sock_recvfrom(&udp->sock, buffer, count, &got,
(SA *) &addr, &addr_len, tm_get(tm)); (SA *) &addr, &addr_len, tm_get(tm));
while (err == IO_RETRY);
if (err == IO_DONE) { if (err == IO_DONE) {
lua_pushlstring(L, buffer, got); lua_pushlstring(L, buffer, got);
lua_pushstring(L, inet_ntoa(addr.sin_addr)); lua_pushstring(L, inet_ntoa(addr.sin_addr));

View File

@ -211,7 +211,7 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
/* here there was no data before timeout */ /* here there was no data before timeout */
else return IO_TIMEOUT; else return IO_TIMEOUT;
/* here we didn't send anything, but now we can */ /* here we didn't send anything, but now we can */
} else return IO_DONE; } else return IO_RETRY;
/* here we successfully sent something */ /* here we successfully sent something */
} else { } else {
*sent = put; *sent = put;
@ -239,7 +239,7 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
if (sock_select(sock+1, NULL, &fds, NULL, timeout) <= 0) { if (sock_select(sock+1, NULL, &fds, NULL, timeout) <= 0) {
if (errno == EINTR) return IO_RETRY; if (errno == EINTR) return IO_RETRY;
else return IO_TIMEOUT; else return IO_TIMEOUT;
} else return IO_DONE; } else return IO_RETRY;
} else { } else {
*sent = put; *sent = put;
return IO_DONE; return IO_DONE;
@ -266,7 +266,7 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
ret = sock_select(sock+1, &fds, NULL, NULL, timeout); ret = sock_select(sock+1, &fds, NULL, NULL, timeout);
if (ret < 0 && errno == EINTR) return IO_RETRY; if (ret < 0 && errno == EINTR) return IO_RETRY;
if (ret == 0) return IO_TIMEOUT; if (ret == 0) return IO_TIMEOUT;
else return IO_DONE; return IO_RETRY;
} else { } else {
*got = taken; *got = taken;
return IO_DONE; return IO_DONE;
@ -294,7 +294,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
ret = sock_select(sock+1, &fds, NULL, NULL, timeout); ret = sock_select(sock+1, &fds, NULL, NULL, timeout);
if (ret < 0 && errno == EINTR) return IO_RETRY; if (ret < 0 && errno == EINTR) return IO_RETRY;
if (ret == 0) return IO_TIMEOUT; if (ret == 0) return IO_TIMEOUT;
else return IO_DONE; return IO_RETRY;
} else { } else {
*got = taken; *got = taken;
return IO_DONE; return IO_DONE;

View File

@ -207,9 +207,9 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
ret = sock_select(0, NULL, &fds, NULL, timeout); ret = sock_select(0, NULL, &fds, NULL, timeout);
/* tell the caller to call us again because there is more data */ /* tell the caller to call us again because now we can send */
if (ret > 0) return IO_DONE; if (ret > 0) return IO_RETRY;
/* tell the caller there was no data before timeout */ /* tell the caller we can't send anything before timint out */
else return IO_TIMEOUT; else return IO_TIMEOUT;
/* here we know the connection has been closed */ /* here we know the connection has been closed */
} else return IO_CLOSED; } else return IO_CLOSED;
@ -229,27 +229,18 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
t_sock sock = *ps; t_sock sock = *ps;
int put; int put;
int ret; int ret;
/* avoid making system calls on closed sockets */
if (sock == SOCK_INVALID) return IO_CLOSED; if (sock == SOCK_INVALID) return IO_CLOSED;
/* try to send something */
put = sendto(sock, data, (int) count, 0, addr, addr_len); put = sendto(sock, data, (int) count, 0, addr, addr_len);
/* deal with failure */
if (put <= 0) { if (put <= 0) {
/* in any case, nothing has been sent */
*sent = 0; *sent = 0;
/* run select to avoid busy wait */
if (WSAGetLastError() == WSAEWOULDBLOCK) { if (WSAGetLastError() == WSAEWOULDBLOCK) {
fd_set fds; fd_set fds;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
ret = sock_select(0, NULL, &fds, NULL, timeout); ret = sock_select(0, NULL, &fds, NULL, timeout);
/* tell the caller to call us again because there is more data */ if (ret > 0) return IO_RETRY;
if (ret > 0) return IO_DONE;
/* tell the caller there was no data before timeout */
else return IO_TIMEOUT; else return IO_TIMEOUT;
/* here we know the connection has been closed */
} else return IO_CLOSED; } else return IO_CLOSED;
/* here we successfully sent something */
} else { } else {
*sent = put; *sent = put;
return IO_DONE; return IO_DONE;
@ -273,7 +264,7 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
ret = sock_select(0, &fds, NULL, NULL, timeout); ret = sock_select(0, &fds, NULL, NULL, timeout);
if (ret > 0) return IO_DONE; if (ret > 0) return IO_RETRY;
else return IO_TIMEOUT; else return IO_TIMEOUT;
} else { } else {
*got = taken; *got = taken;
@ -299,7 +290,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
ret = sock_select(0, &fds, NULL, NULL, timeout); ret = sock_select(0, &fds, NULL, NULL, timeout);
if (ret > 0) return IO_DONE; if (ret > 0) return IO_RETRY;
else return IO_TIMEOUT; else return IO_TIMEOUT;
} else { } else {
*got = taken; *got = taken;

View File

@ -1,3 +1,7 @@
require "luasocket"
require "ltn12"
require "mime"
dofile("testsupport.lua") dofile("testsupport.lua")
local qptest = "qptest.bin" local qptest = "qptest.bin"
@ -92,7 +96,7 @@ local function transform(input, output, filter)
source = ltn12.source.chain(source, filter) source = ltn12.source.chain(source, filter)
end end
--what = not what --what = not what
ltn12.pump(source, sink) ltn12.pump.all(source, sink)
end end
local function encode_qptest(mode) local function encode_qptest(mode)

View File

@ -1,3 +1,5 @@
require "luasocket"
host = host or "localhost" host = host or "localhost"
port = port or "8080" port = port or "8080"

View File

@ -1,3 +1,5 @@
require "luasocket"
host = host or "localhost" host = host or "localhost"
port = port or "8080" port = port or "8080"
@ -22,8 +24,6 @@ while 1 do
print("server: closing connection...") print("server: closing connection...")
break break
end end
print(command);
(loadstring(command))() (loadstring(command))()
end end
end end