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