From 9596c7f95d3ab990bb741107f7cf94ec5dff3e3b Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Thu, 21 Apr 2005 05:38:07 +0000 Subject: [PATCH] Bug in forward.lua. Wasn't breaking from the loop. --- samples/forward.lua | 23 ++++++++++++++--------- src/ftp.lua | 4 ++-- src/tcp.c | 3 ++- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/samples/forward.lua b/samples/forward.lua index e51c5ce..a53ab5d 100644 --- a/samples/forward.lua +++ b/samples/forward.lua @@ -2,7 +2,7 @@ local socket = require"socket" -- creates a new set data structure -function newset() +function newset(a) local reverse = {} local set = {} return setmetatable(set, {__index = { @@ -29,7 +29,7 @@ end -- timeout before an inactive thread is kicked local TIMEOUT = 10 -- set of connections waiting to receive data -local receiving = newset() +local receiving = newset(1) -- set of sockets waiting to send data local sending = newset() -- context for connections and servers @@ -77,8 +77,8 @@ function connect(who, host, port) wait(who, "output") ret, err = who:connect(host, port) if not ret and err ~= "already connected" then - kick(who) kick(context[who].peer) + kick(who) return end end @@ -87,11 +87,11 @@ end -- gets rid of a client function kick(who) - if who and context[who] then + if who then sending:remove(who) receiving:remove(who) - context[who] = nil who:close() + context[who] = nil end end @@ -159,6 +159,7 @@ function forward(who) if not rec_err then kick(who) kick(peer) + break end end end @@ -171,13 +172,17 @@ function go() readable, writable = socket.select(receiving, sending) -- for all readable connections, resume its thread for _, who in ipairs(readable) do - receiving:remove(who) - coroutine.resume(context[who].thread, who) + if context[who] then + receiving:remove(who) + coroutine.resume(context[who].thread, who) + end end -- for all writable connections, do the same for _, who in ipairs(writable) do - sending:remove(who) - coroutine.resume(context[who].thread, who) + if context[who] then + sending:remove(who) + coroutine.resume(context[who].thread, who) + end end -- put all inactive threads in death row local now = socket.gettime() diff --git a/src/ftp.lua b/src/ftp.lua index 9b7c1e5..a793298 100644 --- a/src/ftp.lua +++ b/src/ftp.lua @@ -105,7 +105,7 @@ function metat.__index:send(sendt) if self.pasvt then self:pasvconnect() end -- get the transfer argument and command local argument = sendt.argument or - url.unescape(string.gsub(sendt.path or "", "^/", "")) + url.unescape(string.gsub(sendt.path or "", "^[/\\]", "")) if argument == "" then argument = nil end local command = sendt.command or "stor" -- send the transfer command and check the reply @@ -138,7 +138,7 @@ function metat.__index:receive(recvt) self.try(self.pasvt or self.server, "need port or pasv first") if self.pasvt then self:pasvconnect() end local argument = recvt.argument or - url.unescape(string.gsub(recvt.path or "", "^/", "")) + url.unescape(string.gsub(recvt.path or "", "^[/\\]", "")) if argument == "" then argument = nil end local command = recvt.command or "retr" self.try(self.tp:command(command, argument)) diff --git a/src/tcp.c b/src/tcp.c index eb84997..c79ddd1 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -213,13 +213,14 @@ static int meth_connect(lua_State *L) unsigned short port = (unsigned short) luaL_checknumber(L, 3); p_tm tm = tm_markstart(&tcp->tm); const char *err = inet_tryconnect(&tcp->sock, address, port, tm); + /* have to set the class even if it failed due to non-blocking connects */ + aux_setclass(L, "tcp{client}", 1); if (err) { lua_pushnil(L); lua_pushstring(L, err); return 2; } /* turn master object into a client object */ - aux_setclass(L, "tcp{client}", 1); lua_pushnumber(L, 1); return 1; }