libcurl :显示正在运行的上传和下载速率
libcurl : showing running upload and download rate
>我有一个使用libcurl
实现file upload
和file download
的类。 代码如下。
内存结构
struct MemoryStruct {
uint8_t* memory;
size_t size;
};
回调函数
size_t handleData(void* contents, size_t size, size_t nmemb, void* stream) {
size_t realsize = size * nmemb;
struct MemoryStruct* mem = static_cast<struct MemoryStruct*>(stream);
uint8_t* ptr = static_cast<uint8_t*>(realloc(mem->memory, mem->size + realsize + 1));
if (ptr == NULL) {
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
下载功能
bool download(const std::string& url, std::string& md5sum) {
curl_off_t speed_download = 0;
CURLcode res;
struct MemoryStruct chunk{};
chunk.memory = static_cast<uint8_t*>(malloc(1));
if (nullptr == chunk.memory) {
m_logger->errorf("malloc failed: Not enough memory");
return false;
}
chunk.size = 0;
res = curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt (CURLOPT_URL) failed: %s", curl_easy_strerror(res));
free(chunk.memory);
return false;
}
res = curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, handleData);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt (CURLOPT_WRITEFUNCTION) failed: %s", curl_easy_strerror(res));
free(chunk.memory);
return false;
}
res = curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, (void*) &chunk);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt (CURLOPT_WRITEDATA) failed: %s", curl_easy_strerror(res));
free(chunk.memory);
return false;
}
res = curl_easy_perform(m_curl);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_perform() failed: %s", curl_easy_strerror(res));
free(chunk.memory);
return false;
}
std::vector<uint8_t> readData(chunk.memory, (chunk.memory + chunk.size));
md5sum = md5AsHex(readData);
res = curl_easy_getinfo(m_curl, CURLINFO_SPEED_DOWNLOAD, &speed_download);
if (CURLE_OK != res) {
m_logger->errorf("curl_easy_getinfo(CURLINFO_SPEED_DOWNLOAD) failed: %s", curl_easy_strerror(res));
free(chunk.memory);
return false;
}
m_logger->infof("Average download speed: %" CURL_FORMAT_CURL_OFF_T
"megabyte/sec.n", speed_download / (1024 * 1024));
free(chunk.memory);
return true;
}
上传功能
bool upload(const std::string& filename, const std::string& url) {
CURLcode res;
curl_off_t speed_upload = 0;
std::ifstream in(filename);
std::string data_contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
if (m_curl) {
res = curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_URL) failed: %s", curl_easy_strerror(res));
return false;
}
res = curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, data_contents.c_str());
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_POSTFIELDS) failed: %s", curl_easy_strerror(res));
return false;
}
res = curl_easy_setopt(m_curl, CURLOPT_POSTFIELDSIZE, (long) strlen(data_contents.c_str()));
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_POSTFIELDSIZE) failed: %s", curl_easy_strerror(res));
return false;
}
res = curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_VERBOSE) failed: %s", curl_easy_strerror(res));
return false;
}
res = curl_easy_perform(m_curl);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_perform() failed: %s", curl_easy_strerror(res));
return false;
}
res = curl_easy_getinfo(m_curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_getinfo(CURLINFO_SPEED_UPLOAD) failed: %s", curl_easy_strerror(res));
return false;
}
m_logger->infof("Average upload speed: %" CURL_FORMAT_CURL_OFF_T
" megabyte/sec.n", speed_upload / (1024 * 1024));
return true;
}
return false;
}
我想知道运行下载/上传速率,以便我可以在屏幕上显示它。在上面的例子中,我最后得到了upload
和download
率。我想计算正在运行的上传和下载速率并显示进度条之类的内容。我如何使用 libcurl 做到这一点。
我建议您检查文档中的"CURLOPT_PROGRESSFUNCTION"。 您可以使用它轻松实现此类功能。
https://curl.haxx.se/libcurl/c/CURLOPT_PROGRESSFUNCTION.html
相关文章:
- 下载URL中的所有文件
- 使用专用显卡进行 OpenGL 渲染时帧速率较低
- 在打开多个其他窗口时使用全屏窗口时帧速率非常低
- VisualStudio:使用 Suse Enterprise Server 12 SP5 时,不会下载远程库标头
- 从 C++ 上的网址下载文件
- libcurl :显示正在运行的上传和下载速率
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- 使用 libssh 从 SFTP 服务器下载文件并使用 C++ 将其写入 ofstream?
- 佳能SDK无法从相机下载图片
- 如何在IQualProp::get_AvgFrameRate中重置帧速率?
- 为什么当通过 TCP 发送的消息速率增加时,请求-响应消息对的延迟会降低?
- 使用 Libcurl 库进行文件下载
- Mac os x app bundle 在下载并运行时崩溃,但在终端或更改 Info.plist 时运行良好
- 同步两个具有不同帧速率的传感器
- 如何在不同的线程中保持一定的帧速率
- 根据帧速率缩放/缩小数字
- 通过boost asio iostream下载大文件的最快方法是什么?
- 下载 GPU Mats 的矢量到主机
- 有没有办法使用 mongocxx 驱动程序从 GridFS 集合的文件下载任意范围?
- 将Kinect v2帧速率(rgb、深度、骨架)设置为每秒25帧