10 #include <boost/asio.hpp> 16 const std::shared_ptr<application::AppStateManager> &app_state_manager,
17 std::shared_ptr<Context> context,
20 : context_{std::move(context)},
21 config_{std::move(listener_config)},
24 BOOST_ASSERT(app_state_manager);
25 app_state_manager->takeControl(*
this);
32 }
catch (
const boost::wrapexcept<boost::system::system_error> &exception) {
33 logger_->critical(
"Failed to prepare a listener: {}", exception.what());
35 }
catch (
const std::exception &exception) {
36 logger_->critical(
"Exception when preparing a listener: {}",
41 boost::system::error_code ec;
42 acceptor_->set_option(boost::asio::socket_base::reuse_address(
true), ec);
44 logger_->error(
"Failed to set `reuse address` option to acceptor");
54 logger_->error(
"error: trying to start on non opened acceptor");
59 "Listening for new connections on {}:{}",
75 std::make_unique<NewSessionHandler>(std::move(on_new_session));
81 auto on_accept = [wp = weak_from_this()](boost::system::error_code ec) {
82 if (
auto self = wp.lock()) {
84 if (self->on_new_session_) {
85 (*
self->on_new_session_)(self->new_session_);
87 self->new_session_->start();
90 if (self->acceptor_->is_open()) {
std::shared_ptr< Context > context_
std::shared_ptr< SessionImpl > new_session_
std::function< void(const std::shared_ptr< Session > &)> NewSessionHandler
std::unique_ptr< Acceptor > acceptOnFreePort(std::shared_ptr< boost::asio::io_context > context, Endpoint endpoint, uint16_t port_tolerance, const log::Logger &logger)
const Configuration config_
Endpoint endpoint
listening endpoint
std::unique_ptr< NewSessionHandler > on_new_session_
void acceptOnce() override
Accept incoming connection.
std::unique_ptr< Acceptor > acceptor_
const SessionImpl::Configuration session_config_
constexpr uint16_t kDefaultPortTolerance
void setHandlerForNewSession(NewSessionHandler &&on_new_session) override
Set handler for working new session.
Logger createLogger(const std::string &tag)
HttpListenerImpl(const std::shared_ptr< application::AppStateManager > &app_state_manager, std::shared_ptr< Context > context, Configuration listener_config, SessionImpl::Configuration session_config)