Move function definition to inside the socket listen argument list. This is harder to follow, but allows the coroutine to be unique since it is now part of the closure. Multiple files can be served simultaneously this way.

This commit is contained in:
Marcos Kirsch 2015-03-10 22:15:52 -05:00
parent 65538efc1b
commit d5fcc71c23

View File

@ -54,6 +54,14 @@ local function parseUri(uri)
return r return r
end end
-- Starts web server in the specified port.
return function (port)
local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
s:listen(
port,
function (connection)
-- This variable holds the thread used for sending data back to the user. -- This variable holds the thread used for sending data back to the user.
-- We do it in a separate thread because we need to yield when sending lots -- We do it in a separate thread because we need to yield when sending lots
-- of data in order to avoid overflowing the mcu's buffer. -- of data in order to avoid overflowing the mcu's buffer.
@ -76,6 +84,7 @@ local function onGet(connection, uri)
fileServeFunction = dofile("httpserver-static.lc") fileServeFunction = dofile("httpserver-static.lc")
end end
connectionThread = coroutine.create(fileServeFunction) connectionThread = coroutine.create(fileServeFunction)
--print("Thread created", connectionThread)
coroutine.resume(connectionThread, connection, uri.args) coroutine.resume(connectionThread, connection, uri.args)
end end
@ -91,12 +100,16 @@ local function onReceive(connection, payload)
end end
local function onSent(connection, payload) local function onSent(connection, payload)
if coroutine.status(connectionThread) == "dead" then local connectionThreadStatus = coroutine.status(connectionThread)
--print (connectionThread, "status is", connectionThreadStatus)
if connectionThreadStatus == "dead" then
-- We're done sending file. -- We're done sending file.
--print("Done sending file", connectionThread)
connection:close() connection:close()
connectionThread = nil connectionThread = nil
elseif coroutine.status(connectionThread) == "suspended" then elseif connectionThreadStatus == "suspended" then
-- Not finished sending file, resume. -- Not finished sending file, resume.
--print("Resume thread", connectionThread)
coroutine.resume(connectionThread) coroutine.resume(connectionThread)
else else
print ("Fatal error! I did not expect to hit this codepath") print ("Fatal error! I did not expect to hit this codepath")
@ -104,15 +117,12 @@ local function onSent(connection, payload)
end end
end end
local function handleRequest(connection)
connection:on("receive", onReceive) connection:on("receive", onReceive)
connection:on("sent", onSent) connection:on("sent", onSent)
end
-- Starts web server in the specified port. end
return function (port) )
local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
s:listen(port, handleRequest)
print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port) print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port)
return s return s
end end