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
@ -14,6 +14,7 @@ function BufferedConnection:new(connection)
|
|||||||
newInstance.size = 0
|
newInstance.size = 0
|
||||||
newInstance.data = {}
|
newInstance.data = {}
|
||||||
|
|
||||||
|
-- Returns true if there was any data to be sent.
|
||||||
function newInstance:flush()
|
function newInstance:flush()
|
||||||
if self.size > 0 then
|
if self.size > 0 then
|
||||||
self.connection:send(table.concat(self.data, ""))
|
self.connection:send(table.concat(self.data, ""))
|
||||||
@ -25,43 +26,34 @@ function BufferedConnection:new(connection)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function newInstance:send(payload)
|
function newInstance:send(payload)
|
||||||
local flushthreshold = 1400
|
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)
|
piece = nil
|
||||||
self.size = self.size + piecesize --size should be same as flushthreshold
|
self.size = self.size + pieceSize --size should be same as flushThreshold
|
||||||
--STEP3: flush entire table
|
--STEP3: flush entire table
|
||||||
if self:flush() then
|
if self:flush() then
|
||||||
coroutine.yield()
|
coroutine.yield()
|
||||||
end
|
end
|
||||||
--at this point, size should be 0, because the table was just flushed
|
--at this point, size should be 0, because the table was just flushed
|
||||||
newsize = self.size + payload:len()
|
newSize = self.size + payload:len()
|
||||||
end
|
end
|
||||||
|
|
||||||
--at this point, whatever is left in payload should be <= flushthreshold
|
--at this point, whatever is left in payload should be < flushThreshold
|
||||||
local plen = payload:len()
|
if payload:len() ~= 0 then
|
||||||
if plen == flushthreshold then
|
--leave remaining data in the table
|
||||||
--case 1: what is left in payload is exactly flushthreshold bytes (boundary case), so flush it
|
table.insert(self.data, payload)
|
||||||
table.insert(self.data, payload)
|
self.size = self.size + payload:len()
|
||||||
self.size = self.size + plen
|
|
||||||
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
|
||||||
|
|
||||||
return newInstance
|
return newInstance
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return BufferedConnection
|
return BufferedConnection
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user