add Network.getTotalUpload, getTotalDownload
This commit is contained in:
parent
46dfdac998
commit
d7389c2220
@ -19,6 +19,9 @@ size_t write_callback(char* ptr, size_t size, size_t nmemb, void* userdata) {
|
|||||||
|
|
||||||
class CurlHttp : public Http {
|
class CurlHttp : public Http {
|
||||||
CURL* curl;
|
CURL* curl;
|
||||||
|
|
||||||
|
size_t totalUpload = 0;
|
||||||
|
size_t totalDownload = 0;
|
||||||
public:
|
public:
|
||||||
CurlHttp(CURL* curl) : curl(curl) {
|
CurlHttp(CURL* curl) : curl(curl) {
|
||||||
}
|
}
|
||||||
@ -33,9 +36,27 @@ public:
|
|||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||||
CURLcode res = curl_easy_perform(curl);
|
CURLcode res = curl_easy_perform(curl);
|
||||||
|
if (res == CURLE_OK) {
|
||||||
|
long size;
|
||||||
|
if (!curl_easy_getinfo(curl, CURLINFO_REQUEST_SIZE, &size)) {
|
||||||
|
totalUpload += size;
|
||||||
|
}
|
||||||
|
if (!curl_easy_getinfo(curl, CURLINFO_HEADER_SIZE, &size)) {
|
||||||
|
totalDownload += size;
|
||||||
|
}
|
||||||
|
totalDownload += buffer.size();
|
||||||
|
}
|
||||||
callback(res, std::move(buffer));
|
callback(res, std::move(buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t getTotalUpload() const override {
|
||||||
|
return totalUpload;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t getTotalDownload() const override {
|
||||||
|
return totalDownload;
|
||||||
|
}
|
||||||
|
|
||||||
static std::unique_ptr<CurlHttp> create() {
|
static std::unique_ptr<CurlHttp> create() {
|
||||||
if (auto curl = curl_easy_init()) {
|
if (auto curl = curl_easy_init()) {
|
||||||
return std::make_unique<CurlHttp>(curl);
|
return std::make_unique<CurlHttp>(curl);
|
||||||
@ -54,6 +75,14 @@ void Network::httpGet(const std::string& url, const OnResponse& callback) {
|
|||||||
http->get(url, callback);
|
http->get(url, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t Network::getTotalUpload() const {
|
||||||
|
return http->getTotalUpload();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Network::getTotalDownload() const {
|
||||||
|
return http->getTotalDownload();
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<Network> Network::create(const NetworkSettings& settings) {
|
std::unique_ptr<Network> Network::create(const NetworkSettings& settings) {
|
||||||
auto http = CurlHttp::create();
|
auto http = CurlHttp::create();
|
||||||
return std::make_unique<Network>(std::move(http));
|
return std::make_unique<Network>(std::move(http));
|
||||||
|
|||||||
@ -16,6 +16,8 @@ namespace network {
|
|||||||
virtual ~Http() {}
|
virtual ~Http() {}
|
||||||
|
|
||||||
virtual void get(const std::string& url, const OnResponse& callback) = 0;
|
virtual void get(const std::string& url, const OnResponse& callback) = 0;
|
||||||
|
virtual size_t getTotalUpload() const = 0;
|
||||||
|
virtual size_t getTotalDownload() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Network {
|
class Network {
|
||||||
@ -26,6 +28,9 @@ namespace network {
|
|||||||
|
|
||||||
void httpGet(const std::string& url, const OnResponse& callback);
|
void httpGet(const std::string& url, const OnResponse& callback);
|
||||||
|
|
||||||
|
size_t getTotalUpload() const;
|
||||||
|
size_t getTotalDownload() const;
|
||||||
|
|
||||||
static std::unique_ptr<Network> create(const NetworkSettings& settings);
|
static std::unique_ptr<Network> create(const NetworkSettings& settings);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,4 +15,6 @@ TEST(curltest, curltest) {
|
|||||||
std::cout << value << std::endl;
|
std::cout << value << std::endl;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
std::cout << "upload: " << network->getTotalUpload() << " B" << std::endl;
|
||||||
|
std::cout << "download: " << network->getTotalDownload() << " B" << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user