Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
EventLoop.hpp
Go to the documentation of this file.
1#pragma once
3#include <chrono>
4#include <functional>
14#ifdef ORBIT_ENABLE_HTTP3
16#else
17namespace orbit::server { class QuicConnectionManager; }
18#endif
19#include <atomic>
20
21namespace orbit::server {
22
26class EventLoop {
27public:
40 concurrency::ThreadPool& thread_pool, network::TlsContext* tls_context = nullptr,
41 network::UdpSocket* quic_socket = nullptr, QuicConnectionManager* quic_manager = nullptr);
42
51 void run();
52
56 void stop();
57
62
70 void request_shutdown(std::chrono::steady_clock::time_point deadline);
71
77 void set_tick_hook(std::function<void()> hook) { tick_hook_ = std::move(hook); }
78
84
89 void share_connection_limit(std::atomic<size_t>* open);
90
93
94private:
95 void do_accept();
97 // Hands an accepted socket to the connection manager.
98 void on_accepted(network::socket_t client_fd, const sockaddr_in& addr);
99 // Accepts whatever else is already queued on the listener (POSIX).
101 void wait_and_accept(); // accept with a reserved slot (shared limit)
103 // Stops arming accept until `until` (and while at max_connections).
104 void pause_accepting(std::chrono::steady_clock::time_point until);
107
110 std::unique_ptr<network::Proactor> proactor_;
115
116
117 std::atomic<bool> is_running_{true};
118 std::function<void()> tick_hook_;
119 std::atomic<bool> is_accepting_{true};
120
121 // Graceful shutdown: requested from any thread, carried out by run().
122 std::atomic<bool> shutdown_requested_{false};
123 std::atomic<std::chrono::steady_clock::rep> shutdown_deadline_{0};
124 bool draining_ = false; // loop thread only
125
126 std::chrono::seconds websocket_ping_interval_{0};
127 std::chrono::steady_clock::time_point last_websocket_ping_{};
129
131
132 std::atomic<size_t>* shared_open_ = nullptr; // see share_connection_limit()
133 bool nonblocking_accepts_ = true; // accepted sockets must match the proactor's own accept
134 // Loop-thread only: accept is not armed while paused (fd limit or
135 // max_connections reached); new connections wait in the backlog.
136 bool accept_paused_ = false;
137 std::chrono::steady_clock::time_point accept_resume_at_{};
138 std::chrono::steady_clock::time_point last_limit_warning_{};
139};
140
141} // namespace server
Definition ThreadPool.hpp:13
Server TLS settings: certificates (with SNI) and ALPN.
Definition TlsContext.hpp:22
Definition UdpSocket.hpp:8
Manages routing of HTTP requests to their appropriate handlers.
Definition Router.hpp:46
Manages active HTTP connections.
Definition ConnectionManager.hpp:20
size_t get_connection_count() const
Gets the current number of active connections.
Manages the server's event loop, handling I/O operations and dispatching tasks.
Definition EventLoop.hpp:26
Listener & listener_
Definition EventLoop.hpp:108
std::chrono::steady_clock::time_point last_websocket_ping_
Definition EventLoop.hpp:127
std::chrono::steady_clock::time_point last_limit_warning_
Definition EventLoop.hpp:138
void stop()
Stops the event loop.
void run()
Starts the event loop.
size_t max_connections_
Definition EventLoop.hpp:130
bool at_connection_limit() const
void share_connection_limit(std::atomic< size_t > *open)
QuicConnectionManager * quic_manager_
Definition EventLoop.hpp:114
concurrency::ThreadPool & get_thread_pool()
Gets the thread pool.
Definition EventLoop.hpp:83
concurrency::ThreadPool & thread_pool_
Definition EventLoop.hpp:111
bool accept_paused_
Definition EventLoop.hpp:136
std::atomic< bool > is_accepting_
Definition EventLoop.hpp:119
std::function< void()> tick_hook_
Definition EventLoop.hpp:118
void on_accepted(network::socket_t client_fd, const sockaddr_in &addr)
std::unique_ptr< network::Proactor > proactor_
Definition EventLoop.hpp:110
void set_tick_hook(std::function< void()> hook)
Registers a function the loop calls on every iteration, on the loop thread. The loop wakes at least e...
Definition EventLoop.hpp:77
size_t connection_count() const
Connections this loop currently holds.
Definition EventLoop.hpp:92
void stop_accepting()
Stops accepting new connections but continues processing existing ones.
std::atomic< std::chrono::steady_clock::rep > shutdown_deadline_
Definition EventLoop.hpp:123
std::chrono::seconds websocket_ping_interval_
Definition EventLoop.hpp:126
void request_shutdown(std::chrono::steady_clock::time_point deadline)
Graceful shutdown; safe to call from any thread. Stops accepting, closes idle connections,...
bool nonblocking_accepts_
Definition EventLoop.hpp:133
EventLoop(Listener &listener, const routing::Router &router, const config::ServerConfig &config, concurrency::ThreadPool &thread_pool, network::TlsContext *tls_context=nullptr, network::UdpSocket *quic_socket=nullptr, QuicConnectionManager *quic_manager=nullptr)
Constructs an EventLoop.
std::atomic< bool > shutdown_requested_
Definition EventLoop.hpp:122
std::atomic< bool > is_running_
Definition EventLoop.hpp:117
std::atomic< size_t > * shared_open_
Definition EventLoop.hpp:132
TimerManager timer_manager_
Definition EventLoop.hpp:109
ConnectionManager connection_manager_
Definition EventLoop.hpp:112
bool draining_
Definition EventLoop.hpp:124
network::UdpSocket * quic_socket_
Definition EventLoop.hpp:113
std::chrono::steady_clock::time_point accept_resume_at_
Definition EventLoop.hpp:137
void pause_accepting(std::chrono::steady_clock::time_point until)
Listens for incoming network connections.
Definition Listener.hpp:13
Manages QUIC connections.
Definition QuicConnectionManager.hpp:57
Manages timers for connection timeouts.
Definition TimerManager.hpp:29
Definition Config.hpp:8
int socket_t
Definition PlatformSocket.hpp:39
Definition Http2Session.hpp:18
Definition Config.hpp:30