qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
acceptor.h
Go to the documentation of this file.
1
24
25#ifndef QB_IO_ASYNC_TCP_ACCEPTOR_H
26#define QB_IO_ASYNC_TCP_ACCEPTOR_H
27
28#include <filesystem>
29#ifdef QB_IO_WITH_SSL
31#endif
33#include "../io.h"
34
35namespace qb::io::async::tcp {
36
49template <typename _Derived, typename _Prot>
51 : public input<acceptor<_Derived, _Prot>>
52 , public _Prot {
53 friend class input<acceptor<_Derived, _Prot>>;
54 using base_t = input<acceptor<_Derived, _Prot>>;
55 using Protocol =
57 typename _Prot::socket_type>;
59
60public:
72 void
73 on(event::disconnected &&e) {
74 if constexpr (has_method_on<_Derived, void, event::disconnected>::value)
75 static_cast<_Derived &>(*this).on(std::forward<event::disconnected>(e));
76 else
77 throw std::runtime_error("Acceptor has been disconnected");
78 }
79
80public:
84 using accepted_socket_type = typename _Prot::socket_type;
85
86public:
92 acceptor() noexcept
93 : base_t(new Protocol(*this)) {}
94
98 ~acceptor() = default;
99
108 void
109 on(typename Protocol::message &&new_socket) {
110 static_cast<_Derived &>(*this).on(
111 std::forward<typename Protocol::message>(new_socket));
112 }
113
123 std::filesystem::path cert_file = {},
124 std::filesystem::path key_file = {},
125 std::vector<std::string> alpn_protocols = {}) {
126#ifdef QB_IO_WITH_SSL
127 using tpt = std::decay_t<decltype(this->transport())>;
128 if constexpr (tpt::is_secure()) {
129 this->transport().init(qb::io::ssl::create_server_context(TLS_server_method(), cert_file, key_file));
130 if (!this->transport().ssl_handle()) {
131 LOG_CRIT("Failed to initialize SSL/TLS server context.");
132 return false;
133 }
134 this->transport().set_supported_alpn_protocols(std::move(alpn_protocols));
135 }
136#endif
137 return !this->transport().listen(std::move(uri));
138 }
139};
140
141} // namespace qb::io::async::tcp
142
143#endif // QB_IO_ASYNC_TCP_ACCEPTOR_H
typename transport::accept::socket_type accepted_socket_type
Definition acceptor.h:84
~acceptor()=default
Destructor.
bool listen(qb::io::uri uri, std::filesystem::path cert_file={}, std::filesystem::path key_file={}, std::vector< std::string > alpn_protocols={})
Listen for incoming connections on a given URI.
Definition acceptor.h:122
void on(typename Protocol::message &&new_socket)
Handler for new connections.
Definition acceptor.h:109
acceptor() noexcept
Constructor.
Definition acceptor.h:92
void on(event::disconnected &&e)
Handler for disconnection events.
Definition acceptor.h:73
Protocol for handling the acceptance of new network connections.
Definition accept.h:46
_Socket message
Type alias for the socket type.
Definition accept.h:52
Class for parsing, manipulating, and representing URIs.
Definition uri.h:181
SSL_CTX * create_server_context(const SSL_METHOD *method, std::filesystem::path cert_path, std::filesystem::path key_path)
Create an SSL context (SSL_CTX) configured for server-side SSL/TLS operations.
Core asynchronous I/O class templates for event-driven operations.
#define LOG_CRIT(X)
Critical-level log macro (no-op if QB_STDOUT_LOG is not defined)
Definition io.h:243
Protocol for accepting new connections in asynchronous I/O.
Implementation of a secure SSL/TLS listener for the QB IO library.