2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2007-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
#include <array>
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2008-01-10 19:52:56 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/bufferevent.h>
|
2011-03-24 22:45:04 +00:00
|
|
|
|
2012-05-30 17:47:29 +00:00
|
|
|
#include <libutp/utp.h>
|
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/session.h"
|
|
|
|
#include "libtransmission/bandwidth.h"
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/net.h"
|
|
|
|
#include "libtransmission/peer-io.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/tr-utp.h"
|
|
|
|
#include "libtransmission/utils.h" // for _()
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#undef EAGAIN
|
|
|
|
#define EAGAIN WSAEWOULDBLOCK
|
|
|
|
#undef EINTR
|
|
|
|
#define EINTR WSAEINTR
|
|
|
|
#undef EINPROGRESS
|
|
|
|
#define EINPROGRESS WSAEINPROGRESS
|
|
|
|
#undef EPIPE
|
|
|
|
#define EPIPE WSAECONNRESET
|
2010-06-30 21:24:36 +00:00
|
|
|
#endif
|
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
#define tr_logAddErrorIo(io, msg) tr_logAddError(msg, (io)->display_name())
|
|
|
|
#define tr_logAddWarnIo(io, msg) tr_logAddWarn(msg, (io)->display_name())
|
|
|
|
#define tr_logAddDebugIo(io, msg) tr_logAddDebug(msg, (io)->display_name())
|
|
|
|
#define tr_logAddTraceIo(io, msg) tr_logAddTrace(msg, (io)->display_name())
|
2022-03-11 21:09:22 +00:00
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// Helps us to ignore errors that say "try again later"
|
|
|
|
// since that's what peer-io does by default anyway.
|
|
|
|
[[nodiscard]] auto constexpr canRetryFromError(int error_code) noexcept
|
|
|
|
{
|
|
|
|
return error_code == 0 || error_code == EAGAIN || error_code == EINTR || error_code == EINPROGRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_desired_output_buffer_size(tr_peerIo const* io, uint64_t now)
|
|
|
|
{
|
|
|
|
// this is all kind of arbitrary, but what seems to work well is
|
|
|
|
// being large enough to hold the next 20 seconds' worth of input,
|
|
|
|
// or a few blocks, whichever is bigger. OK to tweak this as needed.
|
|
|
|
static auto constexpr PeriodSecs = 15U;
|
|
|
|
|
|
|
|
// the 3 is an arbitrary number of blocks;
|
|
|
|
// the .5 is to leave room for protocol messages
|
|
|
|
static auto constexpr Floor = static_cast<size_t>(tr_block_info::BlockSize * 3.5);
|
|
|
|
|
|
|
|
auto const current_speed_bytes_per_second = io->get_piece_speed_bytes_per_second(now, TR_UP);
|
|
|
|
return std::max(Floor, current_speed_bytes_per_second * PeriodSecs);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
tr_peerIo::tr_peerIo(
|
|
|
|
tr_session* session,
|
|
|
|
tr_sha1_digest_t const* info_hash,
|
|
|
|
bool is_incoming,
|
|
|
|
bool is_seed,
|
|
|
|
tr_bandwidth* parent_bandwidth)
|
|
|
|
: bandwidth_{ parent_bandwidth }
|
|
|
|
, info_hash_{ info_hash != nullptr ? *info_hash : tr_sha1_digest_t{} }
|
|
|
|
, session_{ session }
|
|
|
|
, is_seed_{ is_seed }
|
|
|
|
, is_incoming_{ is_incoming }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<tr_peerIo> tr_peerIo::create(
|
|
|
|
tr_session* session,
|
|
|
|
tr_bandwidth* parent,
|
|
|
|
tr_sha1_digest_t const* info_hash,
|
|
|
|
bool is_incoming,
|
|
|
|
bool is_seed)
|
|
|
|
{
|
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
auto lock = session->unique_lock();
|
|
|
|
|
|
|
|
auto io = std::make_shared<tr_peerIo>(session, info_hash, is_incoming, is_seed, parent);
|
2023-04-23 01:25:55 +00:00
|
|
|
io->bandwidth().set_peer(io);
|
2022-12-16 23:21:01 +00:00
|
|
|
tr_logAddTraceIo(io, fmt::format("bandwidth is {}; its parent is {}", fmt::ptr(&io->bandwidth()), fmt::ptr(parent)));
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<tr_peerIo> tr_peerIo::new_incoming(tr_session* session, tr_bandwidth* parent, tr_peer_socket socket)
|
|
|
|
{
|
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
|
|
|
|
auto peer_io = tr_peerIo::create(session, parent, nullptr, true, false);
|
|
|
|
peer_io->set_socket(std::move(socket));
|
|
|
|
return peer_io;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<tr_peerIo> tr_peerIo::new_outgoing(
|
|
|
|
tr_session* session,
|
|
|
|
tr_bandwidth* parent,
|
|
|
|
tr_address const& addr,
|
|
|
|
tr_port port,
|
|
|
|
tr_sha1_digest_t const& info_hash,
|
|
|
|
bool is_seed,
|
|
|
|
bool utp)
|
|
|
|
{
|
2023-01-04 21:37:55 +00:00
|
|
|
TR_ASSERT(!tr_peer_socket::limit_reached(session));
|
2022-12-16 23:21:01 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(addr.is_valid());
|
|
|
|
TR_ASSERT(utp || session->allowsTCP());
|
|
|
|
|
|
|
|
if (!addr.is_valid_for_peers(port))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto peer_io = tr_peerIo::create(session, parent, &info_hash, false, is_seed);
|
|
|
|
|
|
|
|
#ifdef WITH_UTP
|
|
|
|
if (utp)
|
|
|
|
{
|
|
|
|
auto* const sock = utp_create_socket(session->utp_context);
|
|
|
|
utp_set_userdata(sock, peer_io.get());
|
|
|
|
peer_io->set_socket(tr_peer_socket{ addr, port, sock });
|
|
|
|
|
|
|
|
auto const [ss, sslen] = addr.to_sockaddr(port);
|
|
|
|
if (utp_connect(sock, reinterpret_cast<sockaddr const*>(&ss), sslen) == 0)
|
|
|
|
{
|
|
|
|
return peer_io;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-18 16:03:59 +00:00
|
|
|
if (!peer_io->socket_.is_valid())
|
|
|
|
{
|
|
|
|
if (auto sock = tr_netOpenPeerSocket(session, addr, port, is_seed); sock.is_valid())
|
|
|
|
{
|
|
|
|
peer_io->set_socket(std::move(sock));
|
|
|
|
return peer_io;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_peerIo::~tr_peerIo()
|
|
|
|
{
|
|
|
|
auto const lock = session_->unique_lock();
|
|
|
|
|
|
|
|
clear_callbacks();
|
|
|
|
tr_logAddTraceIo(this, "in tr_peerIo destructor");
|
|
|
|
event_disable(EV_READ | EV_WRITE);
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-12-16 23:21:01 +00:00
|
|
|
|
|
|
|
void tr_peerIo::set_socket(tr_peer_socket socket_in)
|
|
|
|
{
|
|
|
|
close(); // tear down the previous socket, if any
|
|
|
|
|
|
|
|
socket_ = std::move(socket_in);
|
|
|
|
|
|
|
|
if (socket_.is_tcp())
|
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
event_read_.reset(event_new(session_->event_base(), socket_.handle.tcp, EV_READ, &tr_peerIo::event_read_cb, this));
|
|
|
|
event_write_.reset(event_new(session_->event_base(), socket_.handle.tcp, EV_WRITE, &tr_peerIo::event_write_cb, this));
|
2022-12-16 23:21:01 +00:00
|
|
|
}
|
|
|
|
#ifdef WITH_UTP
|
|
|
|
else if (socket_.is_utp())
|
|
|
|
{
|
|
|
|
utp_set_userdata(socket_.handle.utp, this);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TR_ASSERT_MSG(false, "unsupported peer socket type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_peerIo::close()
|
|
|
|
{
|
2023-01-04 21:37:55 +00:00
|
|
|
socket_.close();
|
2022-12-16 23:21:01 +00:00
|
|
|
event_write_.reset();
|
|
|
|
event_read_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_peerIo::clear()
|
|
|
|
{
|
|
|
|
clear_callbacks();
|
|
|
|
set_enabled(TR_UP, false);
|
|
|
|
set_enabled(TR_DOWN, false);
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_peerIo::reconnect()
|
|
|
|
{
|
|
|
|
TR_ASSERT(!this->is_incoming());
|
|
|
|
TR_ASSERT(this->session_->allowsTCP());
|
|
|
|
|
|
|
|
short int const pending_events = this->pending_events_;
|
|
|
|
event_disable(EV_READ | EV_WRITE);
|
|
|
|
|
|
|
|
close();
|
|
|
|
|
2023-01-04 21:37:55 +00:00
|
|
|
if (tr_peer_socket::limit_reached(session_))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
auto const [addr, port] = socket_address();
|
|
|
|
socket_ = tr_netOpenPeerSocket(session_, addr, port, is_seed());
|
|
|
|
|
|
|
|
if (!socket_.is_tcp())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
this->event_read_.reset(event_new(session_->event_base(), socket_.handle.tcp, EV_READ, event_read_cb, this));
|
|
|
|
this->event_write_.reset(event_new(session_->event_base(), socket_.handle.tcp, EV_WRITE, event_write_cb, this));
|
2022-12-16 23:21:01 +00:00
|
|
|
|
|
|
|
event_enable(pending_events);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-12-16 23:21:01 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
void tr_peerIo::did_write_wrapper(size_t bytes_transferred)
|
2007-12-01 23:08:34 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const keep_alive = shared_from_this();
|
|
|
|
|
|
|
|
while (bytes_transferred != 0 && !std::empty(outbuf_info_))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
auto& [n_bytes_left, is_piece_data] = outbuf_info_.front();
|
2010-04-23 23:45:44 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
size_t const payload = std::min(uint64_t{ n_bytes_left }, uint64_t{ bytes_transferred });
|
2022-10-11 15:39:41 +00:00
|
|
|
/* For µTP sockets, the overhead is computed in utp_on_overhead. */
|
2022-12-16 07:23:12 +00:00
|
|
|
size_t const overhead = socket_.guess_packet_overhead(payload);
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t const now = tr_time_msec();
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
bandwidth().notify_bandwidth_consumed(TR_UP, payload, is_piece_data, now);
|
2008-11-26 15:58:26 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (overhead > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
bandwidth().notify_bandwidth_consumed(TR_UP, overhead, false, now);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (did_write_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
did_write_(this, payload, is_piece_data, user_data_);
|
2022-07-10 18:51:35 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-07-10 18:51:35 +00:00
|
|
|
bytes_transferred -= payload;
|
|
|
|
n_bytes_left -= payload;
|
|
|
|
if (n_bytes_left == 0)
|
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
outbuf_info_.pop_front();
|
2009-02-13 18:23:56 +00:00
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
2007-12-01 23:08:34 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
size_t tr_peerIo::try_write(size_t max)
|
|
|
|
{
|
|
|
|
static auto constexpr Dir = TR_UP;
|
|
|
|
|
|
|
|
if (max == 0)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& buf = outbuf_;
|
|
|
|
max = std::min(max, std::size(buf));
|
|
|
|
max = bandwidth().clamp(Dir, max);
|
|
|
|
if (max == 0)
|
|
|
|
{
|
|
|
|
set_enabled(Dir, false);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_error* error = nullptr;
|
|
|
|
auto const n_written = socket_.try_write(buf, max, &error);
|
|
|
|
// enable further writes if there's more data to write
|
|
|
|
set_enabled(Dir, !std::empty(buf) && (error == nullptr || canRetryFromError(error->code)));
|
|
|
|
|
|
|
|
if (error != nullptr)
|
|
|
|
{
|
|
|
|
if (!canRetryFromError(error->code))
|
|
|
|
{
|
|
|
|
tr_logAddTraceIo(
|
|
|
|
this,
|
|
|
|
fmt::format("try_write err: wrote:{}, errno:{} ({})", n_written, error->code, error->message));
|
|
|
|
call_error_callback(*error);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_error_clear(&error);
|
|
|
|
}
|
|
|
|
else if (n_written > 0U)
|
|
|
|
{
|
|
|
|
did_write_wrapper(n_written);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n_written;
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:18:25 +00:00
|
|
|
void tr_peerIo::event_write_cb([[maybe_unused]] evutil_socket_t fd, short /*event*/, void* vio)
|
2022-12-16 23:21:01 +00:00
|
|
|
{
|
|
|
|
auto* const io = static_cast<tr_peerIo*>(vio);
|
|
|
|
tr_logAddTraceIo(io, "libevent says this peer socket is ready for writing");
|
|
|
|
|
|
|
|
TR_ASSERT(io->socket_.is_tcp());
|
|
|
|
TR_ASSERT(io->socket_.handle.tcp == fd);
|
|
|
|
|
|
|
|
io->pending_events_ &= ~EV_WRITE;
|
|
|
|
|
|
|
|
// Write as much as possible. Since the socket is non-blocking,
|
|
|
|
// write() will return if it can't write any more without blocking
|
|
|
|
io->try_write(SIZE_MAX);
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-12-16 23:21:01 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
void tr_peerIo::can_read_wrapper()
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
// try to consume the input buffer
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if (can_read_ == nullptr)
|
2007-10-02 02:59:07 +00:00
|
|
|
{
|
2022-10-24 21:57:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-10-02 02:59:07 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const lock = session_->unique_lock();
|
|
|
|
auto const keep_alive = shared_from_this();
|
2021-10-22 13:51:36 +00:00
|
|
|
|
2022-10-24 21:57:07 +00:00
|
|
|
auto const now = tr_time_msec();
|
|
|
|
auto done = bool{ false };
|
|
|
|
auto err = bool{ false };
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2022-10-24 21:57:07 +00:00
|
|
|
while (!done && !err)
|
|
|
|
{
|
|
|
|
size_t piece = 0;
|
2022-12-16 07:23:12 +00:00
|
|
|
auto const old_len = read_buffer_size();
|
|
|
|
auto const read_state = can_read_ != nullptr ? can_read_(this, user_data_, &piece) : READ_ERR;
|
|
|
|
auto const used = old_len - read_buffer_size();
|
|
|
|
auto const overhead = socket_.guess_packet_overhead(used);
|
2022-10-24 21:57:07 +00:00
|
|
|
|
|
|
|
if (piece != 0 || piece != used)
|
|
|
|
{
|
|
|
|
if (piece != 0)
|
2010-10-24 01:08:08 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
bandwidth().notify_bandwidth_consumed(TR_DOWN, piece, true, now);
|
2010-10-24 01:08:08 +00:00
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2022-10-24 21:57:07 +00:00
|
|
|
if (used != piece)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
bandwidth().notify_bandwidth_consumed(TR_DOWN, used - piece, false, now);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-10-24 21:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (overhead > 0)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
bandwidth().notify_bandwidth_consumed(TR_DOWN, overhead, false, now);
|
2022-10-24 21:57:07 +00:00
|
|
|
}
|
2011-01-20 00:31:46 +00:00
|
|
|
|
2022-11-17 20:53:08 +00:00
|
|
|
switch (read_state)
|
2022-10-24 21:57:07 +00:00
|
|
|
{
|
|
|
|
case READ_NOW:
|
2022-12-16 07:23:12 +00:00
|
|
|
if (!std::empty(inbuf_))
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2022-10-24 21:57:07 +00:00
|
|
|
continue;
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
2022-10-24 21:57:07 +00:00
|
|
|
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case READ_LATER:
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case READ_ERR:
|
|
|
|
err = true;
|
|
|
|
break;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
size_t tr_peerIo::try_read(size_t max)
|
2022-12-16 07:23:12 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
static auto constexpr Dir = TR_DOWN;
|
2022-12-16 07:23:12 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
if (max == 0)
|
2022-12-06 00:53:31 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
return {};
|
2017-06-28 15:46:06 +00:00
|
|
|
}
|
2022-07-08 15:13:22 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
// Do not write more than the bandwidth allows.
|
|
|
|
// If there is no bandwidth left available, disable writes.
|
|
|
|
max = bandwidth().clamp(TR_DOWN, max);
|
|
|
|
if (max == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
set_enabled(Dir, false);
|
2022-12-16 07:23:12 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-18 16:01:52 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
auto& buf = inbuf_;
|
|
|
|
tr_error* error = nullptr;
|
2023-06-25 17:33:32 +00:00
|
|
|
auto const n_read = socket_.try_read(buf, max, std::empty(buf), &error);
|
2022-12-16 23:21:01 +00:00
|
|
|
set_enabled(Dir, error == nullptr || canRetryFromError(error->code));
|
2022-12-16 07:23:12 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
if (error != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
if (!canRetryFromError(error->code))
|
2022-12-16 07:23:12 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
tr_logAddTraceIo(this, fmt::format("try_read err: n_read:{} errno:{} ({})", n_read, error->code, error->message));
|
|
|
|
call_error_callback(*error);
|
2022-12-16 07:23:12 +00:00
|
|
|
}
|
2011-02-18 00:36:19 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
tr_error_clear(&error);
|
|
|
|
}
|
|
|
|
else if (!std::empty(buf))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
can_read_wrapper();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-12-15 04:26:31 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
return n_read;
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:18:25 +00:00
|
|
|
void tr_peerIo::event_read_cb([[maybe_unused]] evutil_socket_t fd, short /*event*/, void* vio)
|
2022-12-16 23:21:01 +00:00
|
|
|
{
|
|
|
|
static auto constexpr MaxLen = RcvBuf;
|
|
|
|
|
|
|
|
auto* const io = static_cast<tr_peerIo*>(vio);
|
|
|
|
tr_logAddTraceIo(io, "libevent says this peer socket is ready for reading");
|
|
|
|
|
|
|
|
TR_ASSERT(io->socket_.is_tcp());
|
|
|
|
TR_ASSERT(io->socket_.handle.tcp == fd);
|
|
|
|
|
|
|
|
io->pending_events_ &= ~EV_READ;
|
|
|
|
|
|
|
|
// if we don't have any bandwidth left, stop reading
|
|
|
|
auto const n_used = std::size(io->inbuf_);
|
|
|
|
auto const n_left = n_used >= MaxLen ? 0 : MaxLen - n_used;
|
|
|
|
io->try_read(n_left);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
void tr_peerIo::event_enable(short event)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
TR_ASSERT(session_ != nullptr);
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
bool const need_events = socket_.is_tcp();
|
|
|
|
TR_ASSERT(!need_events || event_read_);
|
|
|
|
TR_ASSERT(!need_events || event_write_);
|
2011-02-18 00:23:51 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if ((event & EV_READ) != 0 && (pending_events_ & EV_READ) == 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
tr_logAddTraceIo(this, "enabling ready-to-read polling");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (need_events)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
event_add(event_read_.get(), nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
pending_events_ |= EV_READ;
|
2010-01-17 19:21:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if ((event & EV_WRITE) != 0 && (pending_events_ & EV_WRITE) == 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
tr_logAddTraceIo(this, "enabling ready-to-write polling");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (need_events)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
event_add(event_write_.get(), nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
pending_events_ |= EV_WRITE;
|
2010-01-17 19:21:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
void tr_peerIo::event_disable(short event)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
bool const need_events = socket_.is_tcp();
|
|
|
|
TR_ASSERT(!need_events || event_read_);
|
|
|
|
TR_ASSERT(!need_events || event_write_);
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if ((event & EV_READ) != 0 && (pending_events_ & EV_READ) != 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
tr_logAddTraceIo(this, "disabling ready-to-read polling");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (need_events)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
event_del(event_read_.get());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
pending_events_ &= ~EV_READ;
|
2010-01-17 19:21:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
if ((event & EV_WRITE) != 0 && (pending_events_ & EV_WRITE) != 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
tr_logAddTraceIo(this, "disabling ready-to-write polling");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (need_events)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
event_del(event_write_.get());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
pending_events_ &= ~EV_WRITE;
|
2010-01-17 19:21:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
void tr_peerIo::set_enabled(tr_direction dir, bool is_enabled)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
short const event = dir == TR_UP ? EV_WRITE : EV_READ;
|
|
|
|
|
2022-08-29 20:58:18 +00:00
|
|
|
if (is_enabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
event_enable(event);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-01-17 19:21:04 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 07:23:12 +00:00
|
|
|
event_disable(event);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-01-17 19:21:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
size_t tr_peerIo::flush(tr_direction dir, size_t limit)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
return dir == TR_DOWN ? try_read(limit) : try_write(limit);
|
2009-01-05 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
size_t tr_peerIo::flush_outgoing_protocol_msgs()
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
size_t byte_count = 0;
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
/* count up how many bytes are used by non-piece-data messages
|
|
|
|
at the front of our outbound queue */
|
|
|
|
for (auto const& [n_bytes, is_piece_data] : outbuf_info_)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
if (is_piece_data)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
byte_count += n_bytes;
|
|
|
|
}
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
return flush(TR_UP, byte_count);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
size_t tr_peerIo::get_write_buffer_space(uint64_t now) const noexcept
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
size_t const desired_len = get_desired_output_buffer_size(this, now);
|
2022-12-16 07:23:12 +00:00
|
|
|
size_t const current_len = std::size(outbuf_);
|
2022-08-29 20:58:18 +00:00
|
|
|
return desired_len > current_len ? desired_len - current_len : 0U;
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2010-12-20 02:07:51 +00:00
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
void tr_peerIo::read_uint16(uint16_t* setme)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
auto tmp = uint16_t{};
|
2022-12-16 07:23:12 +00:00
|
|
|
read_bytes(&tmp, sizeof(tmp));
|
2017-04-19 12:04:45 +00:00
|
|
|
*setme = ntohs(tmp);
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
void tr_peerIo::read_uint32(uint32_t* setme)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
auto tmp = uint32_t{};
|
2022-12-16 07:23:12 +00:00
|
|
|
read_bytes(&tmp, sizeof(tmp));
|
2017-04-19 12:04:45 +00:00
|
|
|
*setme = ntohl(tmp);
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 07:23:12 +00:00
|
|
|
void tr_peerIo::read_buffer_drain(size_t byte_count)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
auto buf = std::array<char, 4096>{};
|
2008-12-21 18:15:00 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
while (byte_count > 0)
|
2008-12-29 19:01:47 +00:00
|
|
|
{
|
2022-08-29 18:33:08 +00:00
|
|
|
auto const this_pass = std::min(byte_count, std::size(buf));
|
2022-12-16 07:23:12 +00:00
|
|
|
read_bytes(std::data(buf), this_pass);
|
2022-08-25 01:19:21 +00:00
|
|
|
byte_count -= this_pass;
|
2008-12-29 19:01:47 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-01-11 02:09:20 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- UTP
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
#ifdef WITH_UTP
|
2022-10-29 00:12:37 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
void tr_peerIo::on_utp_state_change(int state)
|
|
|
|
{
|
|
|
|
if (state == UTP_STATE_CONNECT)
|
2022-02-22 22:10:49 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
tr_logAddTraceIo(this, "utp_on_state_change -- changed to connected");
|
|
|
|
utp_supported_ = true;
|
2022-02-22 22:10:49 +00:00
|
|
|
}
|
2022-12-16 23:21:01 +00:00
|
|
|
else if (state == UTP_STATE_WRITABLE)
|
2008-12-24 02:50:08 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
tr_logAddTraceIo(this, "utp_on_state_change -- changed to writable");
|
2022-12-16 07:23:12 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
if ((pending_events_ & EV_WRITE) != 0)
|
2022-02-22 22:10:49 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
try_write(SIZE_MAX);
|
2008-12-24 02:50:08 +00:00
|
|
|
}
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
2022-12-16 23:21:01 +00:00
|
|
|
else if (state == UTP_STATE_EOF)
|
2022-10-29 00:12:37 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
tr_error* error = nullptr;
|
2023-01-25 18:20:10 +00:00
|
|
|
tr_error_set_from_errno(&error, ENOTCONN);
|
2022-12-16 23:21:01 +00:00
|
|
|
call_error_callback(*error);
|
|
|
|
tr_error_clear(&error);
|
2022-10-29 00:12:37 +00:00
|
|
|
}
|
2022-12-16 23:21:01 +00:00
|
|
|
else if (state == UTP_STATE_DESTROYING)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
tr_logAddErrorIo(this, "Impossible state UTP_STATE_DESTROYING");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-12-16 23:21:01 +00:00
|
|
|
else
|
2008-12-24 02:50:08 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
tr_logAddErrorIo(this, fmt::format(_("Unknown state: {state}"), fmt::arg("state", state)));
|
2022-12-16 07:23:12 +00:00
|
|
|
}
|
2022-12-16 23:21:01 +00:00
|
|
|
}
|
2022-10-29 00:12:37 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
void tr_peerIo::on_utp_error(int errcode)
|
|
|
|
{
|
|
|
|
tr_logAddTraceIo(this, fmt::format("utp_on_error -- {}", utp_error_code_names[errcode]));
|
2022-07-08 15:13:22 +00:00
|
|
|
|
2023-02-18 16:03:59 +00:00
|
|
|
if (got_error_ == nullptr)
|
2022-12-16 07:23:12 +00:00
|
|
|
{
|
2023-02-18 16:03:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_error* error = nullptr;
|
|
|
|
switch (errcode)
|
|
|
|
{
|
|
|
|
case UTP_ECONNREFUSED:
|
|
|
|
tr_error_set_from_errno(&error, ECONNREFUSED);
|
|
|
|
break;
|
|
|
|
case UTP_ECONNRESET:
|
|
|
|
tr_error_set_from_errno(&error, ECONNRESET);
|
|
|
|
break;
|
|
|
|
case UTP_ETIMEDOUT:
|
|
|
|
tr_error_set_from_errno(&error, ETIMEDOUT);
|
|
|
|
break;
|
|
|
|
default:
|
2022-12-16 23:21:01 +00:00
|
|
|
tr_error_set(&error, errcode, utp_error_code_names[errcode]);
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
2023-02-18 16:03:59 +00:00
|
|
|
call_error_callback(*error);
|
|
|
|
tr_error_clear(&error);
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
#endif /* #ifdef WITH_UTP */
|
|
|
|
|
|
|
|
void tr_peerIo::utp_init([[maybe_unused]] struct_utp_context* ctx)
|
2008-12-16 22:08:17 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
#ifdef WITH_UTP
|
|
|
|
utp_context_set_option(ctx, UTP_RCVBUF, RcvBuf);
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
// note: all the callback handlers here need to check `userdata` for nullptr
|
|
|
|
// because libutp can fire callbacks on a socket after utp_close() is called
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
utp_set_callback(
|
|
|
|
ctx,
|
|
|
|
UTP_ON_READ,
|
|
|
|
[](utp_callback_arguments* args) -> uint64
|
|
|
|
{
|
|
|
|
if (auto* const io = static_cast<tr_peerIo*>(utp_get_userdata(args->socket)); io != nullptr)
|
|
|
|
{
|
|
|
|
io->inbuf_.add(args->buf, args->len);
|
|
|
|
io->set_enabled(TR_DOWN, true);
|
|
|
|
io->can_read_wrapper();
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
});
|
2009-04-21 16:18:51 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
utp_set_callback(
|
|
|
|
ctx,
|
|
|
|
UTP_GET_READ_BUFFER_SIZE,
|
|
|
|
[](utp_callback_arguments* args) -> uint64
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
if (auto const* const io = static_cast<tr_peerIo*>(utp_get_userdata(args->socket)); io != nullptr)
|
|
|
|
{
|
2023-02-27 21:03:45 +00:00
|
|
|
// We use this callback to enforce speed limits by telling
|
|
|
|
// libutp to read no more than `target_dl_bytes` bytes.
|
|
|
|
auto const target_dl_bytes = io->bandwidth_.clamp(TR_DOWN, RcvBuf);
|
|
|
|
|
|
|
|
// libutp's private function get_rcv_window() allows libutp
|
|
|
|
// to read up to (UTP_RCVBUF - READ_BUFFER_SIZE) bytes and
|
|
|
|
// UTP_RCVBUF is set to `RcvBuf` by tr_peerIo::utp_init().
|
|
|
|
// So to limit dl to `target_dl_bytes`, we need to return
|
|
|
|
// N where (`target_dl_bytes` == RcvBuf - N).
|
|
|
|
return RcvBuf - target_dl_bytes;
|
2022-12-16 23:21:01 +00:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
});
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
utp_set_callback(
|
|
|
|
ctx,
|
|
|
|
UTP_ON_ERROR,
|
|
|
|
[](utp_callback_arguments* args) -> uint64
|
|
|
|
{
|
|
|
|
if (auto* const io = static_cast<tr_peerIo*>(utp_get_userdata(args->socket)); io != nullptr)
|
|
|
|
{
|
2023-02-18 02:59:15 +00:00
|
|
|
io->on_utp_error(args->error_code);
|
2022-12-16 23:21:01 +00:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
});
|
2009-04-21 16:18:51 +00:00
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
utp_set_callback(
|
|
|
|
ctx,
|
|
|
|
UTP_ON_OVERHEAD_STATISTICS,
|
|
|
|
[](utp_callback_arguments* args) -> uint64
|
|
|
|
{
|
|
|
|
if (auto* const io = static_cast<tr_peerIo*>(utp_get_userdata(args->socket)); io != nullptr)
|
|
|
|
{
|
|
|
|
tr_logAddTraceIo(io, fmt::format("{:d} overhead bytes via utp", args->len));
|
2023-04-23 01:25:55 +00:00
|
|
|
io->bandwidth().notify_bandwidth_consumed(args->send != 0 ? TR_UP : TR_DOWN, args->len, false, tr_time_msec());
|
2022-12-16 23:21:01 +00:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
|
|
|
|
utp_set_callback(
|
|
|
|
ctx,
|
|
|
|
UTP_ON_STATE_CHANGE,
|
|
|
|
[](utp_callback_arguments* args) -> uint64
|
|
|
|
{
|
|
|
|
if (auto* const io = static_cast<tr_peerIo*>(utp_get_userdata(args->socket)); io != nullptr)
|
|
|
|
{
|
2023-02-18 02:59:15 +00:00
|
|
|
io->on_utp_state_change(args->state);
|
2022-12-16 23:21:01 +00:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
#endif
|
2009-04-21 16:18:51 +00:00
|
|
|
}
|