Add. pause examples.

master
Alexey Melnichuk 2014-09-05 13:15:59 +05:00
parent 990c1e6725
commit 5cf2f412af
2 changed files with 80 additions and 3 deletions

78
examples/pause.lua Normal file
View File

@ -0,0 +1,78 @@
local curl = require "lcurl"
local WAIT_COUNT = 1
local RESOURCE_URL = "http://www.rfc-editor.org/rfc/rfc2543.txt"
local State = {
PAUSE = 0, -- write function should return CURL_WRITEFUNC_PAUSE
WAIT = 1, -- waiting for CURLPAUSE_CONT
WRITE = 2, -- write function should perform write
WRITTEN = 3, -- write function have performed write
}
-- Current state
local state = State.PAUSE
-- Countdown to continue writes
local waitCount = 0
-- Received data and data size
local data, datasize = {}, 0
local function WriteFunction(str)
if state == State.PAUSE then
state = State.WAIT
waitCount = WAIT_COUNT
print('Pause')
return curl.WRITEFUNC_PAUSE
end
if state == State.WAIT then
-- callback shouldn't be called in this state
print("WARNING: write-callback called in STATE_WAIT")
return curl.WRITEFUNC_PAUSE
end
if state == State.WRITE then
state = State.WRITTEN
end
datasize = datasize + #str
data[#data + 1] = str
end
local progress = 0
local function ProgressFunction()
progress = progress + 1
if state == State.WAIT then
-- Wait until unpause
waitCount = waitCount - 1
if waitCount == 0 then
state = State.WRITE
print('Unpause :', progress)
easy:pause(curl.PAUSE_CONT)
end
end
-- WriteFunction written some data
if state == State.WRITTEN then
-- send pause to WriteFunction
state = State.PAUSE
progress = 0
end
end
easy = curl.easy{
url = RESOURCE_URL,
accept_encoding = "gzip,deflate",
writefunction = WriteFunction,
progressfunction = ProgressFunction,
noprogress = false,
followlocation = true,
}
easy:perform()

View File

@ -5,7 +5,7 @@ local curl = require "lcurl"
-- i.e. 1 means that CURLPAUSE_CONT will be performed immediately after pause.)
local WAIT_COUNT = 32
local RESOURCE_URL = "http://www.gutenberg.org/files/1257/old/1musk10.zip"
local RESOURCE_URL = "http://www.rfc-editor.org/rfc/rfc2543.txt"
local State = {
PAUSE = 0, -- write function should return CURL_WRITEFUNC_PAUSE
@ -37,7 +37,7 @@ local function WriteFunction(str)
end
if state == State.WRITE then
state = State.WRITE
state = State.WRITTEN
end
datasize = datasize + #str
@ -58,7 +58,6 @@ local function perform(multi, easy)
if state == State.WRITTEN then
state = State.PAUSE
break
end
if 0 == handles then