#include "uvco/promise/promise.h" #include "uvco/run.h" #include "uvco/tcp.h" #include "uvco/tcp_stream.h" #include #include #include #include using namespace uvco; // Using co_await in a function turns it into a coroutine. You can co_await all // Promise and MultiPromise values; the right thing will happen. Promise testHttpRequest(const Loop &loop) { TcpClient client{loop, "borgac.net", 80, AF_UNSPEC}; TcpStream stream = co_await client.connect(); co_await stream.write( fmt::format("HEAD / HTTP/1.0\r\nHost: borgac.net\r\n\r\n")); do { std::optional chunk = co_await stream.read(); if (chunk) { fmt::print("Got chunk: >> {} <<\n", *chunk); } else { break; } } while (true); stream.close(); } // Manual setup: this will be part of uvco later. void run_loop() { // As described in the first example. uvco::runMain([](const Loop &loop) -> uvco::Promise { return testHttpRequest(loop); }); } int main() { run_loop(); return 0; }