Skip to content

Commit dc07a79

Browse files
committed
Merge socket.select calls together
allows us to have infinite timeout and fixes high cpu usage
1 parent 7dd747d commit dc07a79

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

OCNetFS/lfs-server.lua

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Warning, given a bug in this program, the client could potentially access files outside the folder
22
-- Best to chroot/limit permissions for this server
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
@@ -86,7 +86,7 @@ if not stat then
8686
end
8787
local sID, sPort = server:getsockname()
8888
server:settimeout(0)
89-
servert[1] = server
89+
sockets[1] = server
9090

9191
print("Listening on " .. sID .. ":" .. sPort)
9292

@@ -172,30 +172,29 @@ local function checkArg(pos,obj,what)
172172
end
173173

174174
local function update()
175-
-- Check for new clients
176-
local test = socket.select(servert,nil,0)
177-
if test and test[server] then
178-
local client = server:accept()
179-
if client ~= nil then
180-
local ci,cp = client:getpeername()
181-
print("User connected from: " .. ci .. ":" .. cp)
182-
clients[#clients + 1] = client
183-
client:settimeout(0)
175+
-- Check for new data or new clients
176+
local ready, err, err2 = socket.select(sockets,nil) or {}
177+
for _, client in ipairs(ready) do
178+
if client == server then
179+
client = server:accept()
180+
if client ~= nil then
181+
local ci,cp = client:getpeername()
182+
print("User connected from: " .. ci .. ":" .. cp)
183+
sockets[#sockets + 1] = client
184+
client:settimeout(0)
185+
end
186+
break
184187
end
185-
end
186-
-- Check for new data
187-
local ready = socket.select(clients,nil,0) or {}
188-
for _,client in ipairs(ready) do
189188
curclient = client
190189
local line, err = client:receive()
191190
if not line then
192191
print("socket receive gave: " .. err)
193192
if err ~= "closed" then
194193
pcall(client.close,client)
195194
end
196-
for i = 1,#clients do
197-
if clients[i] == client then
198-
table.remove(clients, i)
195+
for i = 1,#sockets do
196+
if sockets[i] == client then
197+
table.remove(sockets, i)
199198
break
200199
end
201200
end

0 commit comments

Comments
 (0)