This is really annoying... normalize line endings to Unix (LF). Please use Unix line endings so things diff right with git.
This commit is contained in:
parent
f060295ef7
commit
badbf6e2b9
@ -1,67 +1,59 @@
|
|||||||
-- httpserver-connection
|
-- httpserver-connection
|
||||||
-- Part of nodemcu-httpserver, provides a buffered connection object that can handle multiple
|
-- Part of nodemcu-httpserver, provides a buffered connection object that can handle multiple
|
||||||
-- consecutive send() calls, and buffers small payloads to send once they get big.
|
-- consecutive send() calls, and buffers small payloads to send once they get big.
|
||||||
-- For this to work, it must be used from a coroutine and owner is responsible for the final
|
-- For this to work, it must be used from a coroutine and owner is responsible for the final
|
||||||
-- flush() and for closing the connection.
|
-- flush() and for closing the connection.
|
||||||
-- Author: Philip Gladstone, Marcos Kirsch
|
-- Author: Philip Gladstone, Marcos Kirsch
|
||||||
|
|
||||||
BufferedConnection = {}
|
BufferedConnection = {}
|
||||||
|
|
||||||
-- parameter is the nodemcu-firmware connection
|
-- parameter is the nodemcu-firmware connection
|
||||||
function BufferedConnection:new(connection)
|
function BufferedConnection:new(connection)
|
||||||
local newInstance = {}
|
local newInstance = {}
|
||||||
newInstance.connection = connection
|
newInstance.connection = connection
|
||||||
newInstance.size = 0
|
newInstance.size = 0
|
||||||
newInstance.data = {}
|
newInstance.data = {}
|
||||||
|
|
||||||
function newInstance:flush()
|
-- Returns true if there was any data to be sent.
|
||||||
if self.size > 0 then
|
function newInstance:flush()
|
||||||
self.connection:send(table.concat(self.data, ""))
|
if self.size > 0 then
|
||||||
self.data = {}
|
self.connection:send(table.concat(self.data, ""))
|
||||||
self.size = 0
|
self.data = {}
|
||||||
return true
|
self.size = 0
|
||||||
end
|
return true
|
||||||
return false
|
end
|
||||||
end
|
return false
|
||||||
|
end
|
||||||
function newInstance:send(payload)
|
|
||||||
local flushthreshold = 1400
|
function newInstance:send(payload)
|
||||||
|
local flushThreshold = 1400
|
||||||
local newsize = self.size + payload:len()
|
local newSize = self.size + payload:len()
|
||||||
while newsize > flushthreshold do
|
while newSize >= flushThreshold do
|
||||||
--STEP1: cut out piece from payload to complete threshold bytes in table
|
--STEP1: cut out piece from payload to complete threshold bytes in table
|
||||||
local piecesize = flushthreshold - self.size
|
local pieceSize = flushThreshold - self.size
|
||||||
local piece = payload:sub(1, piecesize)
|
local piece = payload:sub(1, pieceSize)
|
||||||
payload = payload:sub(piecesize + 1, -1)
|
payload = payload:sub(pieceSize + 1, -1)
|
||||||
--STEP2: insert piece into table
|
--STEP2: insert piece into table
|
||||||
table.insert(self.data, piece)
|
table.insert(self.data, piece)
|
||||||
self.size = self.size + piecesize --size should be same as flushthreshold
|
piece = nil
|
||||||
--STEP3: flush entire table
|
self.size = self.size + pieceSize --size should be same as flushThreshold
|
||||||
if self:flush() then
|
--STEP3: flush entire table
|
||||||
coroutine.yield()
|
if self:flush() then
|
||||||
end
|
coroutine.yield()
|
||||||
--at this point, size should be 0, because the table was just flushed
|
end
|
||||||
newsize = self.size + payload:len()
|
--at this point, size should be 0, because the table was just flushed
|
||||||
end
|
newSize = self.size + payload:len()
|
||||||
|
end
|
||||||
--at this point, whatever is left in payload should be <= flushthreshold
|
|
||||||
local plen = payload:len()
|
--at this point, whatever is left in payload should be < flushThreshold
|
||||||
if plen == flushthreshold then
|
if payload:len() ~= 0 then
|
||||||
--case 1: what is left in payload is exactly flushthreshold bytes (boundary case), so flush it
|
--leave remaining data in the table
|
||||||
table.insert(self.data, payload)
|
table.insert(self.data, payload)
|
||||||
self.size = self.size + plen
|
self.size = self.size + payload:len()
|
||||||
if self:flush() then
|
end
|
||||||
coroutine.yield()
|
end
|
||||||
end
|
return newInstance
|
||||||
elseif payload:len() then
|
|
||||||
--case 2: what is left in payload is less than flushthreshold, so just leave it in the table
|
end
|
||||||
table.insert(self.data, payload)
|
|
||||||
self.size = self.size + plen
|
return BufferedConnection
|
||||||
--else, case 3: nothing left in payload, so do nothing
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return newInstance
|
|
||||||
end
|
|
||||||
|
|
||||||
return BufferedConnection
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user