Skip to content

Commit

Permalink
Handle open/bind/listen manually to avoid exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchapyshev committed Nov 14, 2023
1 parent e1f91c4 commit f907ebe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
44 changes: 39 additions & 5 deletions source/relay/session_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,22 @@ std::unique_ptr<T> removeSessionT(std::vector<std::unique_ptr<T>>* session_list,

//--------------------------------------------------------------------------------------------------
SessionManager::SessionManager(std::shared_ptr<base::TaskRunner> task_runner,
const asio::ip::address& listen_address,
const asio::ip::address& address,
uint16_t port,
const std::chrono::minutes& idle_timeout,
bool statistics_enabled,
const std::chrono::seconds& statistics_interval)
: task_runner_(std::move(task_runner)),
acceptor_(base::MessageLoop::current()->pumpAsio()->ioContext(),
asio::ip::tcp::endpoint(listen_address, port)),
acceptor_(base::MessageLoop::current()->pumpAsio()->ioContext()),
address_(address),
port_(port),
idle_timeout_(idle_timeout),
idle_timer_(base::MessageLoop::current()->pumpAsio()->ioContext()),
stat_timer_(base::MessageLoop::current()->pumpAsio()->ioContext()),
statistics_enabled_(statistics_enabled),
statistics_interval_(statistics_interval)
{
DCHECK(task_runner_);

LOG(LS_INFO) << "Session manager port: " << port;
}

//--------------------------------------------------------------------------------------------------
Expand All @@ -131,6 +130,41 @@ void SessionManager::start(std::unique_ptr<SharedPool> shared_pool, Delegate* de
{
LOG(LS_INFO) << "Starting session manager";

asio::ip::tcp::endpoint endpoint(address_, port_);

std::error_code error_code;
acceptor_.open(endpoint.protocol(), error_code);
if (error_code)
{
LOG(LS_ERROR) << "acceptor_.open failed: "
<< base::utf16FromLocal8Bit(error_code.message());
return;
}

acceptor_.set_option(asio::ip::tcp::acceptor::reuse_address(true), error_code);
if (error_code)
{
LOG(LS_ERROR) << "acceptor_.set_option failed: "
<< base::utf16FromLocal8Bit(error_code.message());
return;
}

acceptor_.bind(endpoint, error_code);
if (error_code)
{
LOG(LS_ERROR) << "acceptor_.bind failed: "
<< base::utf16FromLocal8Bit(error_code.message());
return;
}

acceptor_.listen(asio::ip::tcp::socket::max_listen_connections, error_code);
if (error_code)
{
LOG(LS_ERROR) << "acceptor_.listen failed: "
<< base::utf16FromLocal8Bit(error_code.message());
return;
}

start_time_ = Clock::now();

shared_pool_ = std::move(shared_pool);
Expand Down
5 changes: 4 additions & 1 deletion source/relay/session_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SessionManager
};

SessionManager(std::shared_ptr<base::TaskRunner> task_runner,
const asio::ip::address& listen_address,
const asio::ip::address& address,
uint16_t port,
const std::chrono::minutes& idle_timeout,
bool statistics_enabled,
Expand Down Expand Up @@ -85,6 +85,9 @@ class SessionManager
std::vector<std::unique_ptr<PendingSession>> pending_sessions_;
std::vector<std::unique_ptr<Session>> active_sessions_;

const asio::ip::address address_;
const uint16_t port_;

const std::chrono::minutes idle_timeout_;
asio::high_resolution_timer idle_timer_;

Expand Down

0 comments on commit f907ebe

Please sign in to comment.