Skip to content

Commit e53ab46

Browse files
committed
Merge socket.select calls for Love2D
1 parent dc07a79 commit e53ab46

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

OCNetFS/main.lua

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local socket = require("socket")
22

33
local server, curclient, totalspace, curspace, port, label, change, debug, recurseCount
4-
local clients, hndls, servert = {}, {}, {}
4+
local sockets, hndls = {}, {}
55

66
-- Configuration
77
totalspace = math.huge
@@ -27,7 +27,7 @@ function love.load(arg)
2727
end
2828
local sID, sPort = server:getsockname()
2929
server:settimeout(0)
30-
servert[1] = server
30+
sockets[1] = server
3131

3232
print("Listening on " .. sID .. ":" .. sPort)
3333
end
@@ -141,30 +141,29 @@ local function checkArg(pos,obj,what)
141141
end
142142

143143
function love.update()
144-
-- Check for new clients
145-
local test = socket.select(servert,nil,0)
146-
if test and test[server] then
147-
local client = server:accept()
148-
if client ~= nil then
149-
local ci,cp = client:getpeername()
150-
print("User connected from: " .. ci .. ":" .. cp)
151-
clients[#clients + 1] = client
152-
client:settimeout(0)
153-
end
154-
end
155-
-- Check for new data
156-
local ready = socket.select(clients,nil,0) or {}
144+
-- Check for new data or new clients
145+
local ready = socket.select(sockets,nil) or {}
157146
for _,client in ipairs(ready) do
147+
if client == server then
148+
local client = server:accept()
149+
if client ~= nil then
150+
local ci,cp = client:getpeername()
151+
print("User connected from: " .. ci .. ":" .. cp)
152+
sockets[#sockets + 1] = client
153+
client:settimeout(0)
154+
end
155+
break
156+
end
158157
curclient = client
159158
local line, err = client:receive()
160159
if not line then
161160
print("socket receive gave: " .. err)
162161
if err ~= "closed" then
163162
pcall(client.close,client)
164163
end
165-
for i = 1,#clients do
166-
if clients[i] == client then
167-
table.remove(clients, i)
164+
for i = 1,#sockets do
165+
if sockets[i] == client then
166+
table.remove(sockets, i)
168167
break
169168
end
170169
end

0 commit comments

Comments
 (0)