Added some more connection handling to the bridge

This commit is contained in:
Izaya 2018-04-15 22:21:12 +10:00
parent 90118de6ab
commit 83304aae9c
1 changed files with 9 additions and 2 deletions

View File

@ -7,6 +7,8 @@ local port, timeout = tonumber(tArgs[1]) or 4096, tonumber(tArgs[2]) or 60
local clients, coroutines, messages = {}, {}, {}
local clientcounter = 1
local function spawn(f)
coroutines[#coroutines+1] = coroutine.create(function()
pid = #coroutines
@ -46,9 +48,10 @@ function socketLoop()
local client,err = server:accept()
if client then
client:settimeout(0)
clients[#clients+1] = {["conn"]=client,last=os.time(),buffer=""}
clients[clientcounter+1] = {["conn"]=client,last=os.time(),buffer=""}
clientcounter = clientcounter + 1
local i,p = client:getsockname()
print("Gained client #"..tostring(#clients)..": "..i..":"..tostring(p))
print("Gained client #"..tostring(clientcounter)..": "..i..":"..tostring(p))
end
coroutine.yield()
end
@ -63,6 +66,10 @@ function clientLoop()
if s then
client.buffer = client.buffer .. s
client.last=os.time()
elseif b == "closed" then
print("Client "..tostring(id).." disconnected")
client.conn:close()
clients[id] = nil
end
if client.buffer:sub(1,3) == "\0\1\0" then
client.buffer=client.buffer:sub(4)