2001-03-12 20:42:46 +01:00
|
|
|
function set_add(set, sock)
|
2003-03-28 22:24:30 +01:00
|
|
|
table.insert(set, sock)
|
2001-03-12 20:42:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function set_remove(set, sock)
|
2003-03-28 22:24:30 +01:00
|
|
|
for i = 1, table.getn(set) do
|
2001-03-12 20:42:46 +01:00
|
|
|
if set[i] == sock then
|
2003-03-28 22:24:30 +01:00
|
|
|
table.remove(set, i)
|
2001-03-12 20:42:46 +01:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
host = host or "*"
|
|
|
|
port1 = port1 or 8080
|
|
|
|
port2 = port2 or 8081
|
|
|
|
if arg then
|
|
|
|
host = arg[1] or host
|
|
|
|
port1 = arg[2] or port1
|
|
|
|
port2 = arg[3] or port2
|
|
|
|
end
|
|
|
|
|
2003-03-28 22:24:30 +01:00
|
|
|
server1, error = socket.bind(host, port1)
|
2002-07-08 22:56:36 +02:00
|
|
|
if not server1 then print(error) exit() end
|
2001-03-12 20:42:46 +01:00
|
|
|
server1:timeout(1)
|
2003-03-28 22:24:30 +01:00
|
|
|
server2, error = socket.bind(host, port2)
|
2002-07-08 22:56:36 +02:00
|
|
|
if not server2 then print(error) exit() end
|
2001-03-12 20:42:46 +01:00
|
|
|
server2:timeout(1)
|
|
|
|
|
2002-07-08 22:56:36 +02:00
|
|
|
sock_set = {server1, server2}
|
|
|
|
|
|
|
|
sock_id = {}
|
|
|
|
sock_id[server1] = 1
|
|
|
|
sock_id[server2] = 2
|
|
|
|
next_id = 3
|
2001-03-12 20:42:46 +01:00
|
|
|
|
|
|
|
while 1 do
|
2003-03-28 22:24:30 +01:00
|
|
|
local readable, _, error = socket.select(sock_set, nil)
|
2002-07-08 22:56:36 +02:00
|
|
|
for _, sock in readable do
|
|
|
|
-- is it a server socket
|
|
|
|
if sock_id[sock] < 3 then
|
|
|
|
local incomming = sock:accept()
|
|
|
|
if incomming then
|
|
|
|
incomming:timeout(1)
|
|
|
|
sock_id[incomming] = next_id
|
|
|
|
set_add(sock_set, incomming)
|
2003-03-28 22:24:30 +01:00
|
|
|
io.write("Added client id ", next_id, ". ",
|
|
|
|
table.getn(sock_set)-2, " total.\n")
|
2002-07-08 22:56:36 +02:00
|
|
|
next_id = next_id + 1
|
2001-03-12 20:42:46 +01:00
|
|
|
end
|
2002-07-08 22:56:36 +02:00
|
|
|
-- it is a client socket
|
2001-03-12 20:42:46 +01:00
|
|
|
else
|
2002-07-08 22:56:36 +02:00
|
|
|
local line, error = sock:receive()
|
|
|
|
local id = sock_id[sock]
|
|
|
|
if error then
|
|
|
|
sock:close()
|
|
|
|
set_remove(sock_set, sock)
|
2003-03-28 22:24:30 +01:00
|
|
|
io.write("Removed client number ", id, ". ",
|
2002-07-08 22:56:36 +02:00
|
|
|
getn(sock_set)-2, " total.\n")
|
2001-03-12 20:42:46 +01:00
|
|
|
else
|
2003-03-28 22:24:30 +01:00
|
|
|
io.write("Broadcasting line '", id, "> ", line, "'.\n")
|
|
|
|
__, writable, error = socket.select(nil, sock_set, 1)
|
2002-07-08 22:56:36 +02:00
|
|
|
if not error then
|
2003-03-28 22:24:30 +01:00
|
|
|
for ___, outgoing in writable do
|
|
|
|
io.write("Sending to client ", sock_id[outgoing], "\n")
|
2002-07-08 22:56:36 +02:00
|
|
|
outgoing:send(id, "> ", line, "\r\n")
|
2001-03-12 21:00:47 +01:00
|
|
|
end
|
2003-03-28 22:24:30 +01:00
|
|
|
else io.write("No one ready to listen!!!\n") end
|
2001-03-12 21:00:47 +01:00
|
|
|
end
|
2001-03-12 20:42:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|