uvexec
is an implementation of the Senders api proposed by P2300 for libuv🦄🦖
Uvexec provides a few major components:
- A thread-safe event loop.
- Oneshot timers and signal handlers.
- Asynchronous TCP and UDP sockets with an api similar to the one described in P2762.
Uvexec supports all the platforms that libuv does and can be used to build cross-platform asynchronous applications using modern C++
Below is a simple server that accepts single connection, echoes it and stops.
#include <uvexec/uvexec.hpp>
#include <array>
auto main() -> int {
uvexec::loop_t loop;
std::array<std::byte, 1024> data{}; // 1KB buffer
auto server =
stdexec::schedule(loop.get_scheduler())
| stdexec::then([]() {
// Passes the address for listening
return uvexec::ip_v4_addr_t("127.0.0.1", 1329);
}) // Starts to listen to the provided address
| uvexec::bind_to([&](uvexec::tcp_listener_t& listener) {
// Waits for an incoming connection and accepts it
return uvexec::accept_from(listener, [&](uvexec::tcp_socket_t& socket) {
// Reads from the accepted connection to the provided buffer
return uvexec::receive(socket, std::span(data))
| stdexec::then([&](std::size_t n) {
// Passes the filled buffer to send
return std::span(data).first(n);
}) // Replies back
| uvexec::send(socket);
});
});
stdexec::sync_wait(server).value(); // Runs loop
return 0;
}
Clone it into your project, add uvexec root as subdirectory to your cmake project and link uvexec::uvexec
to your target
cmake_minimum_required(VERSION 3.22.1)
project(UvexecExample)
include(CPM.cmake)
CPMAddPackage(
NAME uvexec
GITHUB_REPOSITORY koloshmet/uvexec
GIT_TAG main
)
add_executable(example example.cpp)
target_link_libraries(example uvexec::uvexec)
C++20
- Clang 14+
- GCC 11+
- Apple Clang 15+
- MSVC 19.38+
It may compile on earlier versions of compilers, but hasn't been tested on them. This is especially true for MSVC