Merge pull request #54 from devyte/master
httpserver-connection: implement fragmentation logic
This commit is contained in:
commit
9bd12123b0
@ -25,21 +25,39 @@ function BufferedConnection:new(connection)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function newInstance:send(payload)
|
function newInstance:send(payload)
|
||||||
local l = payload:len()
|
local flushthreshold = 1400
|
||||||
if l + self.size > 1024 then
|
|
||||||
-- Send what we have buffered so far, not including payload.
|
local newsize = self.size + payload:len()
|
||||||
if self:flush() then
|
while newsize > flushthreshold do
|
||||||
coroutine.yield()
|
--STEP1: cut out piece from payload to complete threshold bytes in table
|
||||||
end
|
local piecesize = flushthreshold - self.size
|
||||||
|
local piece = payload:sub(1, piecesize)
|
||||||
|
payload = payload:sub(piecesize + 1, -1)
|
||||||
|
--STEP2: insert piece into table
|
||||||
|
table.insert(self.data, piece)
|
||||||
|
self.size = self.size + piecesize --size should be same as flushthreshold
|
||||||
|
--STEP3: flush entire table
|
||||||
|
if self:flush() then
|
||||||
|
coroutine.yield()
|
||||||
|
end
|
||||||
|
--at this point, size should be 0, because the table was just flushed
|
||||||
|
newsize = self.size + payload:len()
|
||||||
end
|
end
|
||||||
if l > 768 then
|
|
||||||
-- Payload is big. Send it now rather than buffering it for later.
|
--at this point, whatever is left in payload should be <= flushthreshold
|
||||||
self.connection:send(payload)
|
local plen = payload:len()
|
||||||
coroutine.yield()
|
if plen == flushthreshold then
|
||||||
else
|
--case 1: what is left in payload is exactly flushthreshold bytes (boundary case), so flush it
|
||||||
-- Payload is small. Save off payload for later sending.
|
table.insert(self.data, payload)
|
||||||
table.insert(self.data, payload)
|
self.size = self.size + plen
|
||||||
self.size = self.size + l
|
if self:flush() then
|
||||||
|
coroutine.yield()
|
||||||
|
end
|
||||||
|
elseif payload:len() then
|
||||||
|
--case 2: what is left in payload is less than flushthreshold, so just leave it in the table
|
||||||
|
table.insert(self.data, payload)
|
||||||
|
self.size = self.size + plen
|
||||||
|
--else, case 3: nothing left in payload, so do nothing
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user