2012-05-16 37 views
1

這裏我實現了從服務器下載文件的代碼。它的工作正常。 現在我想使我自己的進度條功能,它計算一些數據,如剩餘秒數據速率每秒等 因此從here我找到了一種使用捲曲進度條選項的方法。我們如何啓用此選項。 我完全用這個做了。libcurl控制檯文件下載進度條

我把我的代碼放在下面。這裏代碼my_progress_func經常按照捲曲庫時間間隔調用。我想改變這個間隔時間,並使其達到1秒。是否可以在curl庫中使用爲curl庫設置一些選項?

我想在每1秒鐘後調用這個my_progress_func函數。

代碼:

#include <stdio.h> 
#include <curl/curl.h> 

long test =0; 

struct FtpFile { 
    const char *filename; 
    FILE *stream; 
    long iAppend; 
}; 

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) 
{ 
    struct FtpFile *out=(struct FtpFile *)stream; 
    if(out && !out->stream) { 
    /* open file for writing */ 
     out->stream=fopen(out->filename, out->iAppend ? "ab":"wb"); 
    if(!out->stream) 
     return -1; /* failure, can't open file to write */ 
    } 
    out->iAppend += nmemb; 
    return fwrite(buffer, size, nmemb, out->stream); 
} 

int my_progress_func(void *bar, 
        double t, /* dltotal */ 
        double d, /* dlnow */ 
        double ultotal, 
        double ulnow) 
{ 
    printf("%f : %f \n", d, t); 

    return 0; 
} 

int main(void) 
{ 
    CURL *curl; 
    CURLcode res; 
    int c; 

    struct FtpFile ftpfile={ 
    "dev.zip", /* name to store the file as if succesful */ 
    NULL, 
    }; 

    curl_global_init(CURL_GLOBAL_DEFAULT); 

    curl = curl_easy_init(); 
    if(curl) { 
    curl_easy_setopt(curl, CURLOPT_URL, 
        "sftp://root:[email protected]/mnt/xyz.tar"); 


    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 120L); 


    /* Define our callback to get called when there's data to be written */ 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); 
    /* Set a pointer to our struct to pass to the callback */ 
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); 


    curl_easy_setopt(curl, CURLOPT_FTPPORT, "-"); 

    /* Switch on full protocol/debug output */ 

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 

    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); 
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func); 

    res = curl_easy_perform(curl); 

    printf("res is %d\n, data get %ld\n", res, ftpfile.iAppend); 


    ///Retry upto 100 times it timeout or connection drop occur 
    for (c = 0; (res != CURLE_OK) && (c < 100); c++) { 



     curl_easy_setopt(curl, CURLOPT_RESUME_FROM , ftpfile.iAppend); 
     res = curl_easy_perform(curl); 
     if(res == CURLE_OK) c =0; 
     printf("%d res is %d\n, data get %ld\n",c, res, ftpfile.iAppend); 

    } 
    /* always cleanup */ 
    curl_easy_cleanup(curl); 
    } 
    if(ftpfile.stream) 
    fclose(ftpfile.stream); /* close the local file */ 
    curl_global_cleanup(); 
    return 0; 
} 

回答

1

根據捲曲文檔: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

函數指針應與發現curl_progress_callback 原型。這個函數被libcurl 調用,而不是其內部等效的,在 操作期間頻繁的間隔(大約每秒一次或更早)無論數據是否被移植了 。傳遞給 回調的未知/未使用的參數值將設置爲零(例如,如果您只下載數據,則 上傳大小將保持爲0)。從此 回調中返回一個非零值將導致libcurl中止傳輸並返回 CURLE_ABORTED_BY_CALLBACK。

如果它調用過於頻繁,那麼你可以使用的時間()和靜止無功限制這個,是這樣的:

static time_t prevtime; 
time_t currtime; 
double dif; 
static int first = 1; 
if(first) { 
    time(&prevtime); 
    first = 0; 
} 
time(&currtime); 
dif = difftime(currtime, prevtime); 
if(dif < 1.0) 
    return; 
prevtime = currtime; 

顯然,你運行的捲曲可能不會調用此函數的風險又一秒。