fix: don't call `tr_logAddTraceIo` before `tr_peerIo::set_socket()` (#6881)

* fix: add fallback for invalid address display name

* fix: only call tr_logAddTraceIo after `tr_peerIo::set_socket()` is called

* chore: housekeeping

* code review: handle `nullptr` from `inet_ntop()` instead

* code review: remove unclear comment

* code review: dedupe peerIo bandwidth log
This commit is contained in:
Yat Ho 2024-06-01 23:23:46 +08:00 committed by GitHub
parent f3f887c93e
commit efec65050e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 3 deletions

View File

@ -516,12 +516,16 @@ std::optional<tr_address> tr_address::from_string(std::string_view address_sv)
std::string_view tr_address::display_name(char* out, size_t outlen) const
{
TR_ASSERT(is_valid());
return evutil_inet_ntop(tr_ip_protocol_to_af(type), &addr, out, outlen);
if (auto* name = evutil_inet_ntop(tr_ip_protocol_to_af(type), &addr, out, outlen))
{
return name;
}
return "Invalid address"sv;
}
[[nodiscard]] std::string tr_address::display_name() const
{
auto buf = std::array<char, INET6_ADDRSTRLEN>{};
auto buf = std::array<char, std::max(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)>{};
return std::string{ display_name(std::data(buf), std::size(buf)) };
}

View File

@ -70,6 +70,13 @@ size_t get_desired_output_buffer_size(tr_peerIo const* io, uint64_t now)
auto const current_speed = io->get_piece_speed(now, TR_UP);
return std::max(Floor, current_speed.base_quantity() * PeriodSecs);
}
void log_peer_io_bandwidth(tr_peerIo const& peer_io, tr_bandwidth* const parent)
{
tr_logAddTraceIo(
&peer_io,
fmt::format("bandwidth is {}; its parent is {}", fmt::ptr(&peer_io.bandwidth()), fmt::ptr(parent)));
}
} // namespace
// ---
@ -100,7 +107,6 @@ std::shared_ptr<tr_peerIo> tr_peerIo::create(
auto io = std::make_shared<tr_peerIo>(session, info_hash, is_incoming, is_seed, parent);
io->bandwidth().set_peer(io);
tr_logAddTraceIo(io, fmt::format("bandwidth is {}; its parent is {}", fmt::ptr(&io->bandwidth()), fmt::ptr(parent)));
return io;
}
@ -110,6 +116,7 @@ std::shared_ptr<tr_peerIo> tr_peerIo::new_incoming(tr_session* session, tr_bandw
auto peer_io = tr_peerIo::create(session, parent, nullptr, true, false);
peer_io->set_socket(std::move(socket));
log_peer_io_bandwidth(*peer_io, parent);
return peer_io;
}
@ -167,12 +174,14 @@ std::shared_ptr<tr_peerIo> tr_peerIo::new_outgoing(
if (func.at(preferred)())
{
log_peer_io_bandwidth(*peer_io, parent);
return peer_io;
}
for (preferred_key_t i = 0U; i < TR_NUM_PREFERRED_TRANSPORT; ++i)
{
if (i != preferred && func.at(i)())
{
log_peer_io_bandwidth(*peer_io, parent);
return peer_io;
}
}