Better connection handling.

This commit is contained in:
Diego Nehab 2005-04-21 03:15:34 +00:00
parent 4e3cf63c95
commit 434e8e014c
4 changed files with 26 additions and 16 deletions

3
TODO
View File

@ -1,5 +1,4 @@
BUG NO SET DO TINYIRC!!! SINISTRO.
talk about the non-blocking connect in the manual talk about the non-blocking connect in the manual
test it on Windows!!! test it on Windows!!!
@ -41,3 +40,5 @@ testar os options!
- inet_ntoa também é uma merda. - inet_ntoa também é uma merda.
eliminate globals from namespaces created by module(). eliminate globals from namespaces created by module().
* BUG NO SET DO TINYIRC!!! SINISTRO.

View File

@ -76,14 +76,14 @@ function connect(who, host, port)
if not ret and err == "timeout" then if not ret and err == "timeout" then
wait(who, "output") wait(who, "output")
ret, err = who:connect(host, port) ret, err = who:connect(host, port)
end if not ret and err ~= "already connected" then
if not ret then
kick(who) kick(who)
kick(context[who].peer) kick(context[who].peer)
else return
return forward(who)
end end
end end
return forward(who)
end
-- gets rid of a client -- gets rid of a client
function kick(who) function kick(who)

View File

@ -24,20 +24,29 @@ io.write("Servers bound\n")
-- simple set implementation -- simple set implementation
-- the select function doesn't care about what is passed to it as long as -- the select function doesn't care about what is passed to it as long as
-- it behaves like a table -- it behaves like a table
-- creates a new set data structure
function newset() function newset()
local reverse = {} local reverse = {}
local set = {} local set = {}
setmetatable(set, { __index = { return setmetatable(set, {__index = {
insert = function(set, value) insert = function(set, value)
if not reverse[value] then
table.insert(set, value) table.insert(set, value)
reverse[value] = table.getn(set) reverse[value] = table.getn(set)
end
end, end,
remove = function(set, value) remove = function(set, value)
table.remove(set, reverse[value]) local index = reverse[value]
if index then
reverse[value] = nil reverse[value] = nil
end, local top = table.remove(set)
if top ~= value then
reverse[top] = index
set[index] = top
end
end
end
}}) }})
return set
end end
set = newset() set = newset()

View File

@ -208,7 +208,7 @@ static int meth_bind(lua_State *L)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_connect(lua_State *L) static int meth_connect(lua_State *L)
{ {
p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1);
const char *address = luaL_checkstring(L, 2); const char *address = luaL_checkstring(L, 2);
unsigned short port = (unsigned short) luaL_checknumber(L, 3); unsigned short port = (unsigned short) luaL_checknumber(L, 3);
p_tm tm = tm_markstart(&tcp->tm); p_tm tm = tm_markstart(&tcp->tm);