Moved BufferedConnection into its own file.
This commit is contained in:
parent
3357919f44
commit
e7db3dce7d
12
Makefile
12
Makefile
@ -11,7 +11,17 @@ SPEED=9600
|
|||||||
# End of user config
|
# End of user config
|
||||||
######################################################################
|
######################################################################
|
||||||
HTTP_FILES := $(wildcard http/*)
|
HTTP_FILES := $(wildcard http/*)
|
||||||
LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-basicauth.lua httpserver-b64decode.lua httpserver-conf.lua httpserver-static.lua httpserver-header.lua httpserver-error.lua
|
LUA_FILES := \
|
||||||
|
init.lua \
|
||||||
|
httpserver.lua \
|
||||||
|
httpserver-b64decode.lua \
|
||||||
|
httpserver-basicauth.lua \
|
||||||
|
httpserver-conf.lua \
|
||||||
|
httpserver-connection.lua \
|
||||||
|
httpserver-error.lua \
|
||||||
|
httpserver-header.lua \
|
||||||
|
httpserver-request.lua \
|
||||||
|
httpserver-static.lua \
|
||||||
|
|
||||||
# Print usage
|
# Print usage
|
||||||
usage:
|
usage:
|
||||||
|
|||||||
47
httpserver-connection.lua
Normal file
47
httpserver-connection.lua
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
-- httpserver-connection
|
||||||
|
-- Part of nodemcu-httpserver, provides a buffered connection object that can handle multiple
|
||||||
|
-- consecutive send() calls.
|
||||||
|
-- For this to work, it must be used from a coroutine.
|
||||||
|
-- Author: Philip Gladstone, Marcos Kirsch
|
||||||
|
|
||||||
|
BufferedConnection = {}
|
||||||
|
|
||||||
|
-- parameter is the nodemcu-firmware connection
|
||||||
|
function BufferedConnection:new(connection)
|
||||||
|
local newInstance = {}
|
||||||
|
newInstance.connection = connection
|
||||||
|
newInstance.size = 0
|
||||||
|
newInstance.data = {}
|
||||||
|
|
||||||
|
function newInstance:flush()
|
||||||
|
if self.size > 0 then
|
||||||
|
self.connection:send(table.concat(self.data, ""))
|
||||||
|
self.data = {}
|
||||||
|
self.size = 0
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
--@TODO What are the hardcoded 1000 and 800 about? Can we increase?
|
||||||
|
function newInstance:send(payload)
|
||||||
|
local l = payload:len()
|
||||||
|
if l + self.size > 1000 then
|
||||||
|
if self:flush() then
|
||||||
|
coroutine.yield()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if l > 800 then
|
||||||
|
self.connection:send(payload)
|
||||||
|
coroutine.yield()
|
||||||
|
else
|
||||||
|
table.insert(self.data, payload)
|
||||||
|
self.size = self.size + l
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return newInstance
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
return BufferedConnection
|
||||||
@ -18,47 +18,21 @@ return function (port)
|
|||||||
|
|
||||||
local function startServing(fileServeFunction, connection, req, args)
|
local function startServing(fileServeFunction, connection, req, args)
|
||||||
|
|
||||||
local bufferedConnection = {}
|
connectionThread = coroutine.create(function(fileServeFunction, bufferedConnection, req, args)
|
||||||
connectionThread = coroutine.create(function(fileServeFunction, bconnection, req, args)
|
fileServeFunction(bufferedConnection, req, args)
|
||||||
fileServeFunction(bconnection, req, args)
|
if not bufferedConnection:flush() then
|
||||||
if not bconnection:flush() then
|
|
||||||
connection:close()
|
connection:close()
|
||||||
connectionThread = nil
|
connectionThread = nil
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
function bufferedConnection:flush()
|
local BufferedConnectionClass = dofile("httpserver-connection.lc")
|
||||||
if self.size > 0 then
|
local bufferedConnection = BufferedConnectionClass:new(connection)
|
||||||
connection:send(table.concat(self.data, ""))
|
|
||||||
self.data = {}
|
|
||||||
self.size = 0
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
function bufferedConnection:send(payload)
|
|
||||||
local l = payload:len()
|
|
||||||
if l + self.size > 1000 then
|
|
||||||
if self:flush() then
|
|
||||||
coroutine.yield()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if l > 800 then
|
|
||||||
connection:send(payload)
|
|
||||||
coroutine.yield()
|
|
||||||
else
|
|
||||||
table.insert(self.data, payload)
|
|
||||||
self.size = self.size + l
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
bufferedConnection.size = 0
|
|
||||||
bufferedConnection.data = {}
|
|
||||||
local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
|
local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
|
||||||
if not status then
|
if not status then
|
||||||
print("Error: ", err)
|
print("Error: ", err)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function onRequest(connection, req)
|
local function onRequest(connection, req)
|
||||||
@ -185,6 +159,7 @@ return function (port)
|
|||||||
-- false and nil evaluate as false
|
-- false and nil evaluate as false
|
||||||
local ip = wifi.sta.getip()
|
local ip = wifi.sta.getip()
|
||||||
if not ip then ip = wifi.ap.getip() end
|
if not ip then ip = wifi.ap.getip() end
|
||||||
|
if not ip then ip = "unknown IP" end
|
||||||
print("nodemcu-httpserver running at http://" .. ip .. ":" .. port)
|
print("nodemcu-httpserver running at http://" .. ip .. ":" .. port)
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|||||||
12
init.lua
12
init.lua
@ -56,7 +56,17 @@ local compileAndRemoveIfNeeded = function(f)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local serverFiles = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'httpserver-b64decode.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'}
|
local serverFiles = {
|
||||||
|
'httpserver.lua',
|
||||||
|
'httpserver-b64decode.lua',
|
||||||
|
'httpserver-basicauth.lua',
|
||||||
|
'httpserver-conf.lua',
|
||||||
|
'httpserver-connection.lua',
|
||||||
|
'httpserver-error.lua',
|
||||||
|
'httpserver-header.lua',
|
||||||
|
'httpserver-request.lua',
|
||||||
|
'httpserver-static.lua',
|
||||||
|
}
|
||||||
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
|
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
|
||||||
|
|
||||||
compileAndRemoveIfNeeded = nil
|
compileAndRemoveIfNeeded = nil
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user