libcurl:显示正在运行的上载和下载速率

问题描述 投票:0回答:1

我有一个用file upload实现file downloadlibcurl的类。代码如下。

内存结构

    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;
}

我想知道正在运行的下载/上传速率,以便可以在屏幕上显示它。在以上示例中,我最终得到了uploaddownload速率。我想计算运行中的上载和下载速率,并显示类似进度条的内容。如何使用libcurl做到这一点。

c++ c callback libcurl
1个回答
0
投票

我建议您检查文档中的'CURLOPT_PROGRESSFUNCTION'。您可以使用它轻松实现此类功能。

https://curl.haxx.se/libcurl/c/CURLOPT_PROGRESSFUNCTION.html

© www.soinside.com 2019 - 2024. All rights reserved.