Connection closed detection was improved. Client numbers are now tracked

and sent along with broadcasted line.
This commit is contained in:
Diego Nehab 2001-03-12 20:00:47 +00:00
parent 3f52ed5c53
commit 98a7e91de5

View File

@ -28,30 +28,37 @@ server2:timeout(1)
server2.is_server = 1
set = {server1, server2}
number = 1
while 1 do
local r, s, e, l
local r, s, e, l, n
r, _, e = select(set, nil)
for i, v in r do
if v.is_server then
s = v:accept()
if s then
s:timeout(1)
s.number = number
number = number + 1
set_add(set, s)
write("Added new client. ", getn(set)-2, " total.\n")
write("Added client number ", s.number, ". ",
getn(set)-2, " total.\n")
end
else
l, e = v:receive()
n = v.number
if e then
v:close()
set_remove(set, v)
write("Removed client. ", getn(set)-2, " total.\n")
end
write("Broadcasting line '", tostring(l), "'.\n")
write("Removed client number ", n, ". ",
getn(set)-2, " total.\n")
else
write("Broadcasting line '", tostring(n), "> ",
tostring(l), "'.\n")
_, s, e = select(nil, set, 1)
if not e then
for i,v in s do
v:send(l, "\r\n")
v:send(tostring(n), "> ", l, "\r\n")
end
else
write("No one ready to listen!!!\n")
@ -59,3 +66,4 @@ while 1 do
end
end
end
end