gpt4 book ai didi

c - 用于文件下载的 libcurl 控制台进度条

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:01 27 4
gpt4 key购买 nike

这里我实现了从服务器下载文件的代码。它工作正常。现在我想制作自己的进度条函数来计算一些数据,例如剩余秒数数据每秒速率等。所以来自here我找到了一种使用 curl 进度条选项的方法。我们如何启用此选项。我完全完成了这个。

我把我的代码放在下面。在此代码中,my_progress_func 根据 curl 库时间间隔频繁调用。我想把这个间隔时间改成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:xyz_@192.170.10.1/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;
}

最佳答案

根据 curl 文档: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

Function pointer that should match the curl_progress_callback prototype found in . This function gets called by libcurl instead of its internal equivalent with a frequent interval during operation (roughly once per second or sooner) no matter if data is being transfered or not. Unknown/unused argument values passed to the callback will be set to zero (like if you only download data, the upload size will remain 0). Returning a non-zero value from this callback will cause libcurl to abort the transfer and return CURLE_ABORTED_BY_CALLBACK.

如果它调用得太频繁,那么你可以使用 time() 和一个静态变量来限制它,像这样:

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;

显然,您冒着 curl 可能整整一秒都不会再次调用此函数的风险。

关于c - 用于文件下载的 libcurl 控制台进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10614062/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com