lib/curl/Multi: add Add(), Remove(), ...

This commit is contained in:
Max Kellermann 2020-10-15 16:18:27 +02:00
parent baba41e304
commit c13004f985
3 changed files with 44 additions and 10 deletions

View File

@ -158,10 +158,7 @@ CurlGlobal::Add(CurlRequest &r)
{
assert(GetEventLoop().IsInside());
CURLMcode mcode = curl_multi_add_handle(multi.Get(), r.Get());
if (mcode != CURLM_OK)
throw FormatRuntimeError("curl_multi_add_handle() failed: %s",
curl_multi_strerror(mcode));
multi.Add(r.Get());
InvalidateSockets();
}
@ -171,7 +168,7 @@ CurlGlobal::Remove(CurlRequest &r) noexcept
{
assert(GetEventLoop().IsInside());
curl_multi_remove_handle(multi.Get(), r.Get());
multi.Remove(r.Get());
}
/**
@ -195,10 +192,8 @@ CurlGlobal::ReadInfo() noexcept
assert(GetEventLoop().IsInside());
CURLMsg *msg;
int msgs_in_queue;
while ((msg = curl_multi_info_read(multi.Get(),
&msgs_in_queue)) != nullptr) {
while ((msg = multi.InfoRead()) != nullptr) {
if (msg->msg == CURLMSG_DONE) {
auto *request = ToRequest(msg->easy_handle);
if (request != nullptr)

View File

@ -1,5 +1,5 @@
/*
* Copyright 2008-2019 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2008-2020 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2016-2020 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -32,6 +32,7 @@
#include <curl/curl.h>
#include <chrono>
#include <utility>
#include <stdexcept>
#include <cstddef>
@ -87,6 +88,44 @@ public:
if (code != CURLM_OK)
throw std::runtime_error(curl_multi_strerror(code));
}
void Add(CURL *easy_handle) {
auto code = curl_multi_add_handle(handle, easy_handle);
if (code != CURLM_OK)
throw std::runtime_error(curl_multi_strerror(code));
}
void Remove(CURL *easy_handle) {
auto code = curl_multi_remove_handle(handle, easy_handle);
if (code != CURLM_OK)
throw std::runtime_error(curl_multi_strerror(code));
}
CURLMsg *InfoRead() {
int msgs_in_queue;
return curl_multi_info_read(handle, &msgs_in_queue);
}
unsigned Perform() {
int running_handles;
auto code = curl_multi_perform(handle, &running_handles);
if (code != CURLM_OK)
throw std::runtime_error(curl_multi_strerror(code));
return running_handles;
}
unsigned Wait(int timeout=-1) {
int numfds;
auto code = curl_multi_wait(handle, nullptr, 0, timeout,
&numfds);
if (code != CURLM_OK)
throw std::runtime_error(curl_multi_strerror(code));
return numfds;
}
unsigned Wait(std::chrono::milliseconds timeout) {
return Wait(timeout.count());
}
};
#endif