Skip to content

Commit

Permalink
Updated the send queue to send all data on each call to update
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Ritzl committed Jul 28, 2017
1 parent 93956da commit 0618f14
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions defnet/tcp_send_queue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,42 @@ function M.create(client, chunk_size)
chunk_size = chunk_size or 10000

local instance = {}

local queue = {}

function instance.clear()
queue = {}
end

function instance.add(data)
assert(data, "You must provide some data")
for i=1,#data,chunk_size do
table.insert(queue, { data = data:sub(i, i + chunk_size - 1), sent_index = 0 })
end
end

function instance.send()
local first = queue[1]
if not first then
return
end

local sent_index, err, sent_index_on_err = client:send(first.data, first.sent_index + 1, #first.data)
if err then
first.sent_index = sent_index_on_err
return false, err
end
while true do
local first = queue[1]
if not first then
return true
end

first.sent_index = sent_index
if first.sent_index == #first.data then
table.remove(queue, 1)
local sent_index, err, sent_index_on_err = client:send(first.data, first.sent_index + 1, #first.data)
if err then
first.sent_index = sent_index_on_err
return false, err
end

first.sent_index = sent_index
if first.sent_index == #first.data then
table.remove(queue, 1)
end
end
return true
end

return instance
end


return M
return M

0 comments on commit 0618f14

Please sign in to comment.