Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::server::EventLoop Class Reference

Manages the server's event loop, handling I/O operations and dispatching tasks. More...

#include <EventLoop.hpp>

Collaboration diagram for orbit::server::EventLoop:

Public Member Functions

 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.
 
void run ()
 Starts the event loop.
 
void stop ()
 Stops the event loop.
 
void stop_accepting ()
 Stops accepting new connections but continues processing existing ones.
 
void request_shutdown (std::chrono::steady_clock::time_point deadline)
 Graceful shutdown; safe to call from any thread. Stops accepting, closes idle connections, lets in-flight requests finish (their responses carry Connection: close; HTTP/2 gets GOAWAY, WebSocket close 1001), and force-closes whatever is left at deadline. run() returns once no connection remains.
 
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 every 200 ms, so the hook also runs when no I/O happens.
 
concurrency::ThreadPool & get_thread_pool ()
 Gets the thread pool.
 
void share_connection_limit (std::atomic< size_t > *open)
 
size_t connection_count () const
 Connections this loop currently holds.
 

Private Member Functions

void do_accept ()
 
void do_read_quic ()
 
void on_accepted (network::socket_t client_fd, const sockaddr_in &addr)
 
void accept_pending ()
 
void wait_and_accept ()
 
bool reserve_slot ()
 
void pause_accepting (std::chrono::steady_clock::time_point until)
 
void resume_accepting_if_ready ()
 
bool at_connection_limit () const
 
void drain_step ()
 

Private Attributes

Listener & listener_
 
TimerManager timer_manager_
 
std::unique_ptr< network::Proactor > proactor_
 
concurrency::ThreadPool & thread_pool_
 
ConnectionManager connection_manager_
 
network::UdpSocket * quic_socket_
 
QuicConnectionManager * quic_manager_
 
std::atomic< bool > is_running_ {true}
 
std::function< void()> tick_hook_
 
std::atomic< bool > is_accepting_ {true}
 
std::atomic< bool > shutdown_requested_ {false}
 
std::atomic< std::chrono::steady_clock::rep > shutdown_deadline_ {0}
 
bool draining_ = false
 
std::chrono::seconds websocket_ping_interval_ {0}
 
std::chrono::steady_clock::time_point last_websocket_ping_ {}
 
size_t max_connections_ = 0
 
std::atomic< size_t > * shared_open_ = nullptr
 
bool nonblocking_accepts_ = true
 
bool accept_paused_ = false
 
std::chrono::steady_clock::time_point accept_resume_at_ {}
 
std::chrono::steady_clock::time_point last_limit_warning_ {}
 

Detailed Description

Manages the server's event loop, handling I/O operations and dispatching tasks.

Constructor & Destructor Documentation

◆ EventLoop()

orbit::server::EventLoop::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.

Parameters
listenerThe server listener.
routerThe router instance.
configThe server configuration.
tls_contextThe TLS context (optional).
quic_socketThe QUIC UDP socket (optional).
quic_managerThe QUIC connection manager (optional).
thread_poolRuns request handlers; shared by every loop of an App and must outlive this loop's connections.

Member Function Documentation

◆ accept_pending()

void orbit::server::EventLoop::accept_pending ( )
private

◆ at_connection_limit()

bool orbit::server::EventLoop::at_connection_limit ( ) const
private

◆ connection_count()

size_t orbit::server::EventLoop::connection_count ( ) const
inline

Connections this loop currently holds.

◆ do_accept()

void orbit::server::EventLoop::do_accept ( )
private

◆ do_read_quic()

void orbit::server::EventLoop::do_read_quic ( )
private

◆ drain_step()

void orbit::server::EventLoop::drain_step ( )
private

◆ get_thread_pool()

concurrency::ThreadPool & orbit::server::EventLoop::get_thread_pool ( )
inline

Gets the thread pool.

Returns
Reference to the thread pool.

◆ on_accepted()

void orbit::server::EventLoop::on_accepted ( network::socket_t  client_fd,
const sockaddr_in &  addr 
)
private

◆ pause_accepting()

void orbit::server::EventLoop::pause_accepting ( std::chrono::steady_clock::time_point  until)
private

◆ request_shutdown()

void orbit::server::EventLoop::request_shutdown ( std::chrono::steady_clock::time_point  deadline)

Graceful shutdown; safe to call from any thread. Stops accepting, closes idle connections, lets in-flight requests finish (their responses carry Connection: close; HTTP/2 gets GOAWAY, WebSocket close 1001), and force-closes whatever is left at deadline. run() returns once no connection remains.

◆ reserve_slot()

bool orbit::server::EventLoop::reserve_slot ( )
private

◆ resume_accepting_if_ready()

void orbit::server::EventLoop::resume_accepting_if_ready ( )
private

◆ run()

void orbit::server::EventLoop::run ( )

Starts the event loop.

EventLoop loop(listener, router, config);
loop.run();
Manages the server's event loop, handling I/O operations and dispatching tasks.
Definition EventLoop.hpp:26
Definition Config.hpp:8

◆ set_tick_hook()

void orbit::server::EventLoop::set_tick_hook ( std::function< void()>  hook)
inline

Registers a function the loop calls on every iteration, on the loop thread. The loop wakes at least every 200 ms, so the hook also runs when no I/O happens.

◆ share_connection_limit()

void orbit::server::EventLoop::share_connection_limit ( std::atomic< size_t > *  open)

Makes max_connections one budget shared by the loops of an App: open counts their connections, and a loop reserves a slot in it before each accept, so concurrent loops cannot together exceed the limit. Call before run(); open must outlive the loop.

◆ stop()

void orbit::server::EventLoop::stop ( )

Stops the event loop.

◆ stop_accepting()

void orbit::server::EventLoop::stop_accepting ( )

Stops accepting new connections but continues processing existing ones.

◆ wait_and_accept()

void orbit::server::EventLoop::wait_and_accept ( )
private

Member Data Documentation

◆ accept_paused_

bool orbit::server::EventLoop::accept_paused_ = false
private

◆ accept_resume_at_

std::chrono::steady_clock::time_point orbit::server::EventLoop::accept_resume_at_ {}
private

◆ connection_manager_

ConnectionManager orbit::server::EventLoop::connection_manager_
private

◆ draining_

bool orbit::server::EventLoop::draining_ = false
private

◆ is_accepting_

std::atomic<bool> orbit::server::EventLoop::is_accepting_ {true}
private

◆ is_running_

std::atomic<bool> orbit::server::EventLoop::is_running_ {true}
private

◆ last_limit_warning_

std::chrono::steady_clock::time_point orbit::server::EventLoop::last_limit_warning_ {}
private

◆ last_websocket_ping_

std::chrono::steady_clock::time_point orbit::server::EventLoop::last_websocket_ping_ {}
private

◆ listener_

Listener& orbit::server::EventLoop::listener_
private

◆ max_connections_

size_t orbit::server::EventLoop::max_connections_ = 0
private

◆ nonblocking_accepts_

bool orbit::server::EventLoop::nonblocking_accepts_ = true
private

◆ proactor_

std::unique_ptr<network::Proactor> orbit::server::EventLoop::proactor_
private

◆ quic_manager_

QuicConnectionManager* orbit::server::EventLoop::quic_manager_
private

◆ quic_socket_

network::UdpSocket* orbit::server::EventLoop::quic_socket_
private

◆ shared_open_

std::atomic<size_t>* orbit::server::EventLoop::shared_open_ = nullptr
private

◆ shutdown_deadline_

std::atomic<std::chrono::steady_clock::rep> orbit::server::EventLoop::shutdown_deadline_ {0}
private

◆ shutdown_requested_

std::atomic<bool> orbit::server::EventLoop::shutdown_requested_ {false}
private

◆ thread_pool_

concurrency::ThreadPool& orbit::server::EventLoop::thread_pool_
private

◆ tick_hook_

std::function<void()> orbit::server::EventLoop::tick_hook_
private

◆ timer_manager_

TimerManager orbit::server::EventLoop::timer_manager_
private

◆ websocket_ping_interval_

std::chrono::seconds orbit::server::EventLoop::websocket_ping_interval_ {0}
private

The documentation for this class was generated from the following file: