cppnet
C++ network utilities for asynchronous servers.
Loading...
Searching...
No Matches
async_context.hpp
Go to the documentation of this file.
1// Copyright 2025 Kevin Exton
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
19#pragma once
20#ifndef CPPNET_ASYNC_CONTEXT_HPP
21#define CPPNET_ASYNC_CONTEXT_HPP
22#include "net/detail/immovable.hpp"
23#include "net/timers/timers.hpp"
24
25#include <exec/async_scope.hpp>
26#include <io/io.hpp>
27
28#include <atomic>
29#include <cstdint>
31namespace net::service {
32
34struct async_context : detail::immovable {
36 using async_scope = exec::async_scope;
38 using multiplexer_type = io::execution::poll_multiplexer;
40 using triggers = io::execution::basic_triggers<multiplexer_type>;
42 using socket_dialog = triggers::socket_dialog;
44 using socket_type = io::socket::native_socket_type;
46 using signal_mask = std::uint64_t;
52 using clock = std::chrono::steady_clock;
54 using duration = std::chrono::milliseconds;
55
57 enum signals : std::uint8_t { terminate = 0, user1, END };
59 enum context_states : std::uint8_t { PENDING = 0, STARTED, STOPPED };
60
68 std::atomic<signal_mask> sigmask;
70 std::atomic<context_states> state{PENDING};
71
77 inline auto signal(int signum) -> void;
78
80 inline auto interrupt() const noexcept -> void;
81
104 template <typename Fn>
105 requires std::is_invocable_r_v<bool, Fn>
106 auto isr(const socket_dialog &socket, Fn routine) -> void;
107
109 inline auto run() -> void;
110};
111
112} // namespace net::service
113
114#include "impl/async_context_impl.hpp" // IWYU pragma: export
115
116#endif // CPPNET_ASYNC_CONTEXT_HPP
This namespace is for network services.
Definition async_context.hpp:31
An asynchronous execution context.
Definition async_context.hpp:34
std::chrono::milliseconds duration
The duration type.
Definition async_context.hpp:54
io::execution::basic_triggers< multiplexer_type > triggers
The io triggers type.
Definition async_context.hpp:40
triggers poller
The poll triggers.
Definition async_context.hpp:66
io::execution::poll_multiplexer multiplexer_type
The io multiplexer type.
Definition async_context.hpp:38
triggers::socket_dialog socket_dialog
The socket dialog type.
Definition async_context.hpp:42
std::chrono::steady_clock clock
The clock type.
Definition async_context.hpp:52
exec::async_scope async_scope
Asynchronous scope type.
Definition async_context.hpp:36
timers_type timers
The event loop timers.
Definition async_context.hpp:62
context_states
An enum of valid context states.
Definition async_context.hpp:59
auto isr(const socket_dialog &socket, Fn routine) -> void
An interrupt service routine for the poller.
io::socket::native_socket_type socket_type
The socket type.
Definition async_context.hpp:44
signals
An enum of all valid async context signals.
Definition async_context.hpp:57
auto run() -> void
Runs the event loop.
std::atomic< context_states > state
A counter that tracks the context state.
Definition async_context.hpp:70
auto interrupt() const noexcept -> void
Calls the timers interrupt.
async_scope scope
The asynchronous scope.
Definition async_context.hpp:64
std::atomic< signal_mask > sigmask
The active signal mask.
Definition async_context.hpp:68
std::uint64_t signal_mask
The signal mask type.
Definition async_context.hpp:46
auto signal(int signum) -> void
Sets the signal mask, then interrupts the service.
A socketpair interrupt source.
Definition interrupt.hpp:27