Skip to content

Commit

Permalink
More correctly handle errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchapyshev committed May 25, 2024
1 parent 03421b9 commit d187f85
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
13 changes: 11 additions & 2 deletions source/base/net/tcp_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,21 @@ void TcpChannel::setDecryptor(std::unique_ptr<MessageDecryptor> decryptor)
//--------------------------------------------------------------------------------------------------
std::u16string TcpChannel::peerAddress() const
{
if (!socket_.is_open())
if (!socket_.is_open() || !isConnected())
return std::u16string();

try
{
asio::ip::address address = socket_.remote_endpoint().address();
std::error_code error_code;
asio::ip::tcp::endpoint endpoint = socket_.remote_endpoint(error_code);
if (error_code)
{
LOG(LS_ERROR) << "Unable to get peer address: "
<< base::utf16FromLocal8Bit(error_code.message());
return std::u16string();
}

asio::ip::address address = endpoint.address();
if (address.is_v4())
{
asio::ip::address_v4 ipv4_address = address.to_v4();
Expand Down
20 changes: 19 additions & 1 deletion source/relay/pending_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,25 @@ PendingSession::PendingSession(std::shared_ptr<base::TaskRunner> task_runner,
timer_(base::WaitableTimer::Type::SINGLE_SHOT, std::move(task_runner)),
socket_(std::move(socket))
{
address_ = socket_.remote_endpoint().address().to_string();
try
{
std::error_code error_code;
asio::ip::tcp::endpoint endpoint = socket_.remote_endpoint(error_code);
if (error_code)
{
LOG(LS_ERROR) << "Unable to get endpoint for pending session: "
<< base::utf16FromLocal8Bit(error_code.message());
}
else
{
address_ = endpoint.address().to_string();
}
}
catch (const std::error_code& error_code)
{
LOG(LS_ERROR) << "Unable to get address for pending session: "
<< base::utf16FromLocal8Bit(error_code.message());
}
}

//--------------------------------------------------------------------------------------------------
Expand Down
28 changes: 26 additions & 2 deletions source/relay/session_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ std::unique_ptr<T> removeSessionT(std::vector<std::unique_ptr<T>>* session_list,
return nullptr;
}

std::u16string peerAddress(const asio::ip::tcp::socket& socket)
{
try
{
std::error_code error_code;
asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(error_code);
if (error_code)
{
LOG(LS_ERROR) << "Unable to get endpoint for accepted connection: "
<< base::utf16FromLocal8Bit(error_code.message());
}
else
{
return base::utf16FromLocal8Bit(endpoint.address().to_string());
}
}
catch (const std::error_code& error_code)
{
LOG(LS_ERROR) << "Unable to get address for pending session: "
<< base::utf16FromLocal8Bit(error_code.message());
}

return std::u16string();
}

} // namespace

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -284,8 +309,7 @@ void SessionManager::doAccept(SessionManager* self)
{
if (!error_code)
{
LOG(LS_INFO) << "New accepted connection: " << base::utf16FromLocal8Bit(
socket.remote_endpoint().address().to_string());
LOG(LS_INFO) << "New accepted connection: " << peerAddress(socket);

// A new peer is connected. Create and start the pending session.
self->pending_sessions_.emplace_back(std::make_unique<PendingSession>(
Expand Down

0 comments on commit d187f85

Please sign in to comment.