warden.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #pragma once #include <felspar/coro/starter.hpp> #include <felspar/coro/stream.hpp> #include <felspar/io/completion.hpp> #include <felspar/io/deadline.hpp> #include <felspar/io/pipe.hpp> #include <felspar/io/posix.hpp> #include <felspar/memory/pmr.hpp> #include <chrono> #include <span> namespace felspar::io { class allocator; |
Warden
The warden is the primary abstraction of the library. It represents an event loop on a single thread without committing to how that event loop is implemented. It also acts as an allocator which is able to allocate coroutine frames.
Because of this dual-use this warden type must remain only an
interface. Any attributes it has will break allocators as they will have
two copies and no way to keep the attribute content the same.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | class warden : public felspar::pmr::memory_resource { friend class allocator; template<typename R> friend class felspar::io::iop; public: virtual ~warden() = default; template<typename R> using task = coro::task<R, warden>; template<typename R> using stream = coro::stream<R, warden>; template<typename R = void> using eager = coro::eager<task<R>>; template<typename R = void> using starter = coro::starter<task<R>>; |
Running coroutines
Primary coroutine
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | template<typename Ret, typename... PArgs, typename... MArgs> Ret run(task<Ret> (*f)(warden &, PArgs...), MArgs &&...margs) { auto handle = f(*this, std::forward<MArgs>(margs)...).release(); run_until(handle.get()); return handle.promise().consume_value(); } template<typename Ret, typename N, typename... PArgs, typename... MArgs> Ret run(N &o, coro::task<Ret> (N::*f)(warden &, PArgs...), MArgs &&...margs) { auto task = (o.*f)(*this, std::forward<MArgs>(margs)...); auto handle = task.release(); run_until(handle.get()); return handle.promise().consume_value(); } template<typename Ret, typename N, typename... PArgs, typename... MArgs> Ret run(N &o, task<Ret> (N::*f)(warden &, PArgs...), MArgs &&...margs) { auto task = (o.*f)(*this, std::forward<MArgs>(margs)...); auto handle = task.release(); run_until(handle.get()); return handle.promise().consume_value(); } |
Single batch
78 | virtual void run_batch() = 0; |
Run a single IO submission and resume ready coroutines. Only processing is performed without any waits
Delayed resume
85 86 87 88 89 90 | void async_resume(std::span<std::coroutine_handle<> const> handles) { do_async_resume(handles); } void async_resume(std::coroutine_handle<> h) { async_resume(std::span{&h, 1}); } |
Once the event loop has finished processing new events, then the coroutines sent here will be resumed. This slight asynchrony can be used to solve problems where a coroutine resume might destroy the object that triggers the coroutine to be resumed.
File descriptors
100 101 102 103 104 105 106 107 108 109 110 111 | iop<void> close(socket_descriptor fd, std::source_location const loc = std::source_location::current()) { return do_close(fd, loc); } iop<void> close(posix::fd s, std::source_location const loc = std::source_location::current()) { return close(s.release(), loc); } |
Time management
115 116 117 118 119 120 | iop<void> sleep(std::chrono::nanoseconds ns, std::source_location const loc = std::source_location::current()) { return do_sleep(ns, loc); } |
Reading and writing
Read or write bytes from the provided buffer returning the number of bytes read/written.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | iop<std::size_t> read_some( socket_descriptor fd, std::span<std::byte> s, std::optional<deadline> deadline = {}, std::source_location const loc = std::source_location::current()) { return do_read_some(fd, s, deadline, loc); } iop<std::size_t> read_some( socket_descriptor fd, std::span<std::byte> s, std::chrono::nanoseconds timeout, std::source_location const loc = std::source_location::current()) { return read_some(fd, s, deadline_from(timeout), loc); } iop<std::size_t> read_some( posix::fd const &s, std::span<std::byte> b, std::optional<deadline> deadline = {}, std::source_location const l = std::source_location::current()) { return read_some(s.native_handle(), b, deadline, l); } iop<std::size_t> read_some( posix::fd const &s, std::span<std::byte> b, std::chrono::nanoseconds timeout, std::source_location const l = std::source_location::current()) { return read_some(s.native_handle(), b, deadline_from(timeout), l); } iop<std::size_t> write_some( socket_descriptor fd, std::span<std::byte const> s, std::optional<deadline> deadline = {}, std::source_location const loc = std::source_location::current()) { return do_write_some(fd, s, deadline, loc); } iop<std::size_t> write_some( socket_descriptor fd, std::span<std::byte const> s, std::chrono::nanoseconds timeout, std::source_location const loc = std::source_location::current()) { return write_some(fd, s, deadline_from(timeout), loc); } iop<std::size_t> write_some( posix::fd const &s, std::span<std::byte const> b, std::optional<deadline> deadline = {}, std::source_location const l = std::source_location::current()) { return write_some(s.native_handle(), b, deadline, l); } iop<std::size_t> write_some( posix::fd const &s, std::span<std::byte const> b, std::chrono::nanoseconds timeout, std::source_location const l = std::source_location::current()) { return write_some(s.native_handle(), b, deadline_from(timeout), l); } |
Socket APIs
Socket creation
Some wardens may need special socket options to be set, so in order
to be portable across wardens use this API instead of the POSIX
::socket one.
198 199 200 201 202 | posix::fd create_socket( int domain, int type, int protocol, std::source_location = std::source_location::current()); |
Create a TCP socket
204 205 206 207 208 | posix::fd create_tcp_socket( std::source_location const loc = std::source_location::current()) { return create_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP, loc); } |
Pipe
Create a pipe usable by this warden. On Windows a true pipe will be simulated by a socket pair.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | pipe create_pipe(std::source_location = std::source_location::current()); iop<socket_descriptor> accept(socket_descriptor fd, std::optional<deadline> deadline = {}, std::source_location const loc = std::source_location::current()) { return do_accept(fd, deadline, loc); } iop<socket_descriptor> accept(socket_descriptor fd, std::chrono::nanoseconds timeout, std::source_location const loc = std::source_location::current()) { return accept(fd, deadline_from(timeout), loc); } iop<socket_descriptor> accept(posix::fd const &sock, std::optional<deadline> deadline = {}, std::source_location const loc = std::source_location::current()) { return accept(sock.native_handle(), deadline, loc); } iop<socket_descriptor> accept(posix::fd const &sock, std::chrono::nanoseconds timeout, std::source_location const loc = std::source_location::current()) { return accept(sock.native_handle(), deadline_from(timeout), loc); } iop<void> connect(socket_descriptor fd, sockaddr const *addr, socklen_t addrlen, std::optional<deadline> deadline = {}, std::source_location const loc = std::source_location::current()) { return do_connect(fd, addr, addrlen, deadline, loc); } iop<void> connect(socket_descriptor fd, sockaddr const *addr, socklen_t addrlen, std::chrono::nanoseconds timeout, std::source_location const loc = std::source_location::current()) { return connect(fd, addr, addrlen, deadline_from(timeout), loc); } iop<void> connect(posix::fd const &sock, sockaddr const *addr, socklen_t addrlen, std::optional<deadline> deadline = {}, std::source_location const loc = std::source_location::current()) { return connect(sock.native_handle(), addr, addrlen, deadline, loc); } iop<void> connect(posix::fd const &sock, sockaddr const *addr, socklen_t addrlen, std::chrono::nanoseconds timeout, std::source_location const loc = std::source_location::current()) { return connect( sock.native_handle(), addr, addrlen, deadline_from(timeout), loc); } |
File readiness
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | iop<void> read_ready( socket_descriptor fd, std::optional<deadline> deadline = {}, std::source_location const loc = std::source_location::current()) { return do_read_ready(fd, deadline, loc); } iop<void> read_ready( socket_descriptor fd, std::chrono::nanoseconds timeout, std::source_location const loc = std::source_location::current()) { return read_ready(fd, deadline_from(timeout), loc); } iop<void> read_ready( posix::fd const &sock, std::optional<deadline> deadline = {}, std::source_location const loc = std::source_location::current()) { return read_ready(sock.native_handle(), deadline, loc); } iop<void> read_ready( posix::fd const &sock, std::chrono::nanoseconds timeout, std::source_location const loc = std::source_location::current()) { return read_ready( sock.native_handle(), deadline_from(timeout), loc); } iop<void> write_ready( socket_descriptor fd, std::optional<deadline> deadline = {}, std::source_location const loc = std::source_location::current()) { return do_write_ready(fd, deadline, loc); } iop<void> write_ready( socket_descriptor fd, std::chrono::nanoseconds timeout, std::source_location const loc = std::source_location::current()) { return write_ready(fd, deadline_from(timeout), loc); } iop<void> write_ready( posix::fd const &sock, std::optional<deadline> deadline = {}, std::source_location const loc = std::source_location::current()) { return write_ready(sock.native_handle(), deadline, loc); } iop<void> write_ready( posix::fd const &sock, std::chrono::nanoseconds timeout, std::source_location const loc = std::source_location::current()) { return write_ready( sock.native_handle(), deadline_from(timeout), loc); } private: |
PMR based memory allocation
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | void *do_allocate( std::size_t const bytes, std::size_t const alignment) override { return felspar::pmr::new_delete_resource()->allocate( bytes, alignment); } void do_deallocate( void *const p, std::size_t const bytes, std::size_t const alignment) override { return felspar::pmr::new_delete_resource()->deallocate( p, bytes, alignment); } bool do_is_equal(memory_resource const &other) const noexcept override { return this == &other; } protected: virtual void run_until(std::coroutine_handle<>) = 0; virtual void do_async_resume(std::span<std::coroutine_handle<> const>) = 0; virtual iop<void> do_close(socket_descriptor fd, std::source_location) = 0; virtual iop<void> do_sleep(std::chrono::nanoseconds, std::source_location) = 0; virtual iop<std::size_t> do_read_some( socket_descriptor fd, std::span<std::byte>, std::optional<deadline> timeout, std::source_location) = 0; virtual iop<std::size_t> do_write_some( socket_descriptor fd, std::span<std::byte const>, std::optional<deadline> timeout, std::source_location) = 0; virtual void do_prepare_socket(socket_descriptor, std::source_location) { } virtual iop<socket_descriptor> do_accept( socket_descriptor fd, std::optional<deadline> timeout, std::source_location) = 0; virtual iop<void> do_connect( socket_descriptor fd, sockaddr const *, socklen_t, std::optional<deadline> timeout, std::source_location) = 0; virtual iop<void> do_read_ready( socket_descriptor fd, std::optional<deadline> timeout, std::source_location) = 0; virtual iop<void> do_write_ready( socket_descriptor fd, std::optional<deadline> timeout, std::source_location) = 0; }; template<typename R> inline iop<R>::~iop() { if (comp and comp->delete_due_to_iop_destructed()) { delete comp; } } } |