#include <asio.hpp>
#include <iostream>
#include <string>
#ifdef __linux__
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <unistd.h>
#endif
using namespace std;
namespace {
constexpr int kRequests = 5;
constexpr const char* kUrl = "https://postman-echo.com/get";
for (int i = 0; i < kRequests; ++i) {
++completed;
cout << " #" << i << " -> "
<< (ec ? ec.message() : to_string(res.
http_code_)) <<
"\n";
});
}
}
}
int main(int argc, char** argv) {
const string mode = argc > 1 ? argv[1] : "run";
asio::io_context io;
int completed = 0;
launch_requests(http, completed);
if (mode == "run") {
io.run();
} else if (mode == "poll") {
while (completed < kRequests) {
io.poll();
}
#ifdef __linux__
} else if (mode == "foreign") {
const int ep = epoll_create1(EPOLL_CLOEXEC);
const int tick = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
const itimerspec every_ms{{0, 1000000}, {0, 1000000}};
timerfd_settime(tick, 0, &every_ms, nullptr);
epoll_event want{};
want.events = EPOLLIN;
want.data.fd = tick;
epoll_ctl(ep, EPOLL_CTL_ADD, tick, &want);
while (completed < kRequests) {
epoll_event ev;
const int n = epoll_wait(ep, &ev, 1, 10);
if (n > 0 && ev.data.fd == tick) {
uint64_t expirations = 0;
[[maybe_unused]] const auto r =
read(tick, &expirations, sizeof(expirations));
}
io.poll();
}
close(tick);
close(ep);
#endif
} else {
cerr << "usage: " << argv[0] << " [run|poll|foreign]\n";
return 2;
}
cout << "done: " << completed << "/" << kRequests << " (" << mode
<< " mode)\n";
return completed == kRequests ? 0 : 1;
}
auto async_get(std::string url, CompletionToken &&token)
Definition cofetch.h:324
long http_code_
Definition cofetch.h:176