2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © 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
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdint>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <type_traits> // std::underlying_type_t
|
2023-07-08 15:24:03 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#else
|
|
|
|
#include <arpa/inet.h> // ntohl, ntohs
|
|
|
|
#endif
|
2008-01-10 19:52:56 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/event.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-09-16 01:23:34 +00:00
|
|
|
#include <small/map.hpp>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/bandwidth.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/block-info.h" // tr_block_info
|
2023-11-04 16:39:41 +00:00
|
|
|
#include "libtransmission/error.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/net.h"
|
|
|
|
#include "libtransmission/peer-io.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/peer-socket.h" // tr_peer_socket, tr_netOpen...
|
|
|
|
#include "libtransmission/session.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/utils.h" // for _()
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
struct sockaddr;
|
|
|
|
|
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.
|
2023-12-07 16:13:34 +00:00
|
|
|
[[nodiscard]] constexpr auto can_retry_from_error(int error_code) noexcept
|
2023-01-07 22:55:00 +00:00
|
|
|
{
|
2023-12-07 16:13:34 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return error_code == 0 || error_code == WSAEWOULDBLOCK || error_code == WSAEINTR || error_code == WSAEINPROGRESS;
|
|
|
|
#else
|
|
|
|
return error_code == 0 || error_code == EAGAIN || error_code == EWOULDBLOCK || error_code == EINTR ||
|
|
|
|
error_code == EINPROGRESS;
|
|
|
|
#endif
|
2023-01-07 22:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2023-11-10 23:12:24 +00:00
|
|
|
static auto constexpr PeriodSecs = uint64_t{ 15U };
|
2023-01-07 22:55:00 +00:00
|
|
|
|
|
|
|
// the 3 is an arbitrary number of blocks;
|
|
|
|
// the .5 is to leave room for protocol messages
|
2023-11-10 23:12:24 +00:00
|
|
|
static auto constexpr Floor = static_cast<uint64_t>(tr_block_info::BlockSize * 3.5);
|
2023-01-07 22:55:00 +00:00
|
|
|
|
2023-11-10 23:12:24 +00:00
|
|
|
auto const current_speed = io->get_piece_speed(now, TR_UP);
|
|
|
|
return std::max(Floor, current_speed.base_quantity() * PeriodSecs);
|
2023-01-07 22:55:00 +00:00
|
|
|
}
|
|
|
|
} // 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,
|
2023-07-05 16:33:50 +00:00
|
|
|
tr_socket_address const& socket_address,
|
2022-12-16 23:21:01 +00:00
|
|
|
tr_sha1_digest_t const& info_hash,
|
|
|
|
bool is_seed,
|
|
|
|
bool utp)
|
|
|
|
{
|
2023-09-16 01:23:34 +00:00
|
|
|
using preferred_key_t = std::underlying_type_t<tr_preferred_transport>;
|
|
|
|
auto const preferred = session->preferred_transport();
|
2023-07-05 01:29:12 +00:00
|
|
|
|
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);
|
2023-09-16 01:23:34 +00:00
|
|
|
TR_ASSERT(socket_address.is_valid());
|
2022-12-16 23:21:01 +00:00
|
|
|
TR_ASSERT(utp || session->allowsTCP());
|
|
|
|
|
|
|
|
auto peer_io = tr_peerIo::create(session, parent, &info_hash, false, is_seed);
|
2023-09-16 01:23:34 +00:00
|
|
|
auto const func = small::max_size_map<preferred_key_t, std::function<bool()>, TR_NUM_PREFERRED_TRANSPORT>{
|
|
|
|
{ TR_PREFER_UTP,
|
|
|
|
[&]()
|
|
|
|
{
|
2022-12-16 23:21:01 +00:00
|
|
|
#ifdef WITH_UTP
|
2023-09-16 01:23:34 +00:00
|
|
|
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{ socket_address, sock });
|
|
|
|
|
|
|
|
auto const [ss, sslen] = socket_address.to_sockaddr();
|
|
|
|
if (utp_connect(sock, reinterpret_cast<sockaddr const*>(&ss), sslen) == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
} },
|
|
|
|
{ TR_PREFER_TCP,
|
|
|
|
[&]()
|
|
|
|
{
|
|
|
|
if (!peer_io->socket_.is_valid())
|
|
|
|
{
|
|
|
|
if (auto sock = tr_netOpenPeerSocket(session, socket_address, is_seed); sock.is_valid())
|
|
|
|
{
|
|
|
|
peer_io->set_socket(std::move(sock));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} }
|
|
|
|
};
|
|
|
|
|
|
|
|
if (func.at(preferred)())
|
2022-12-16 23:21:01 +00:00
|
|
|
{
|
2023-09-16 01:23:34 +00:00
|
|
|
return peer_io;
|
2022-12-16 23:21:01 +00:00
|
|
|
}
|
2023-09-16 01:23:34 +00:00
|
|
|
for (preferred_key_t i = 0U; i < TR_NUM_PREFERRED_TRANSPORT; ++i)
|
2023-02-18 16:03:59 +00:00
|
|
|
{
|
2023-09-16 01:23:34 +00:00
|
|
|
if (i != preferred && func.at(i)())
|
2023-02-18 16:03:59 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-30 20:26:55 +00:00
|
|
|
auto sock = tr_netOpenPeerSocket(session_, socket_address(), is_seed());
|
|
|
|
if (!sock.is_tcp())
|
2022-12-16 23:21:01 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-30 20:26:55 +00:00
|
|
|
socket_ = std::move(sock);
|
2022-12-16 23:21:01 +00:00
|
|
|
|
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();
|
|
|
|
|
2023-12-07 16:13:34 +00:00
|
|
|
while (bytes_transferred != 0U && !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
|
|
|
|
2023-12-07 16:13:34 +00:00
|
|
|
if (overhead > 0U)
|
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;
|
2023-12-07 16:13:34 +00:00
|
|
|
if (n_bytes_left == 0U)
|
2022-07-10 18:51:35 +00:00
|
|
|
{
|
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;
|
|
|
|
|
2023-12-07 16:13:34 +00:00
|
|
|
if (max == 0U)
|
2022-12-16 23:21:01 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& buf = outbuf_;
|
|
|
|
max = std::min(max, std::size(buf));
|
|
|
|
max = bandwidth().clamp(Dir, max);
|
2023-12-07 16:13:34 +00:00
|
|
|
if (max == 0U)
|
2022-12-16 23:21:01 +00:00
|
|
|
{
|
|
|
|
set_enabled(Dir, false);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = tr_error{};
|
2022-12-16 23:21:01 +00:00
|
|
|
auto const n_written = socket_.try_write(buf, max, &error);
|
|
|
|
// enable further writes if there's more data to write
|
2023-12-07 16:13:34 +00:00
|
|
|
set_enabled(Dir, !std::empty(buf) && (!error || can_retry_from_error(error.code())));
|
2022-12-16 23:21:01 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
if (error)
|
2022-12-16 23:21:01 +00:00
|
|
|
{
|
2023-12-07 16:13:34 +00:00
|
|
|
if (!can_retry_from_error(error.code()))
|
2022-12-16 23:21:01 +00:00
|
|
|
{
|
|
|
|
tr_logAddTraceIo(
|
|
|
|
this,
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::format("try_write err: wrote:{}, errno:{} ({})", n_written, error.code(), error.message()));
|
|
|
|
call_error_callback(error);
|
2022-12-16 23:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
2024-02-13 16:42:19 +00:00
|
|
|
auto done = false;
|
|
|
|
auto err = false;
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2024-01-02 06:33:53 +00:00
|
|
|
// In normal conditions, only continue processing if we still have bandwidth
|
|
|
|
// quota for it.
|
|
|
|
//
|
|
|
|
// The read buffer will grow indefinitely if libutp or the TCP stack keeps buffering
|
|
|
|
// data faster than the bandwidth limit allows. To safeguard against that, we keep
|
|
|
|
// processing if the read buffer is more than twice as large as the target size.
|
|
|
|
while (!done && !err && (read_buffer_size() > RcvBuf * 2U || bandwidth().clamp(TR_DOWN, read_buffer_size()) != 0U))
|
2022-10-24 21:57:07 +00:00
|
|
|
{
|
2023-12-07 16:13:34 +00:00
|
|
|
size_t piece = 0U;
|
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
|
|
|
|
2024-01-02 06:33:53 +00:00
|
|
|
if (piece != 0U)
|
2022-10-24 21:57:07 +00:00
|
|
|
{
|
2024-01-02 06:33:53 +00:00
|
|
|
bandwidth().notify_bandwidth_consumed(TR_DOWN, piece, true, now);
|
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2024-01-02 06:33:53 +00:00
|
|
|
if (used != piece)
|
|
|
|
{
|
|
|
|
bandwidth().notify_bandwidth_consumed(TR_DOWN, used - piece, false, now);
|
2022-10-24 21:57:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 16:13:34 +00:00
|
|
|
if (overhead > 0U)
|
2022-10-24 21:57:07 +00:00
|
|
|
{
|
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
|
|
|
|
2023-12-07 16:13:34 +00:00
|
|
|
if (max == 0U)
|
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.
|
2023-10-06 23:30:04 +00:00
|
|
|
max = bandwidth().clamp(Dir, max);
|
2023-12-07 16:13:34 +00:00
|
|
|
if (max == 0U)
|
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_;
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = tr_error{};
|
2023-06-25 17:33:32 +00:00
|
|
|
auto const n_read = socket_.try_read(buf, max, std::empty(buf), &error);
|
2023-12-07 16:13:34 +00:00
|
|
|
set_enabled(Dir, !error || can_retry_from_error(error.code()));
|
2022-12-16 07:23:12 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
if (error)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-12-07 16:13:34 +00:00
|
|
|
if (!can_retry_from_error(error.code()))
|
2022-12-16 07:23:12 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +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
|
|
|
}
|
2022-12-16 23:21:01 +00:00
|
|
|
}
|
|
|
|
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_);
|
2023-12-07 16:13:34 +00:00
|
|
|
auto const n_left = n_used >= MaxLen ? 0U : MaxLen - n_used;
|
2022-12-16 23:21:01 +00:00
|
|
|
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
|
|
|
{
|
2023-12-07 16:13:34 +00:00
|
|
|
size_t byte_count = 0U;
|
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
|
|
|
|
2023-09-26 17:50:41 +00:00
|
|
|
void tr_peerIo::read_bytes(void* bytes, size_t n_bytes)
|
|
|
|
{
|
|
|
|
auto walk = reinterpret_cast<std::byte*>(bytes);
|
|
|
|
n_bytes = std::min(n_bytes, std::size(inbuf_));
|
|
|
|
if (decrypt_remain_len_)
|
|
|
|
{
|
|
|
|
if (*decrypt_remain_len_ <= n_bytes)
|
|
|
|
{
|
|
|
|
filter_.decrypt(std::data(inbuf_), *decrypt_remain_len_, walk);
|
|
|
|
inbuf_.drain(*decrypt_remain_len_);
|
|
|
|
if (walk != nullptr)
|
|
|
|
{
|
|
|
|
walk += *decrypt_remain_len_;
|
|
|
|
}
|
|
|
|
n_bytes -= *decrypt_remain_len_;
|
|
|
|
filter_.decrypt_disable();
|
|
|
|
decrypt_remain_len_.reset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*decrypt_remain_len_ -= n_bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filter_.decrypt(std::data(inbuf_), n_bytes, walk);
|
|
|
|
inbuf_.drain(n_bytes);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = tr_error{};
|
|
|
|
error.set_from_errno(ENOTCONN);
|
|
|
|
call_error_callback(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;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = tr_error{};
|
2023-02-18 16:03:59 +00:00
|
|
|
switch (errcode)
|
|
|
|
{
|
|
|
|
case UTP_ECONNREFUSED:
|
2023-11-04 16:39:41 +00:00
|
|
|
error.set_from_errno(ECONNREFUSED);
|
2023-02-18 16:03:59 +00:00
|
|
|
break;
|
|
|
|
case UTP_ECONNRESET:
|
2023-11-04 16:39:41 +00:00
|
|
|
error.set_from_errno(ECONNRESET);
|
2023-02-18 16:03:59 +00:00
|
|
|
break;
|
|
|
|
case UTP_ETIMEDOUT:
|
2023-11-04 16:39:41 +00:00
|
|
|
error.set_from_errno(ETIMEDOUT);
|
2023-02-18 16:03:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-11-04 16:39:41 +00:00
|
|
|
error.set(errcode, utp_error_code_names[errcode]);
|
|
|
|
break;
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
2023-11-04 16:39:41 +00:00
|
|
|
|
|
|
|
call_error_callback(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)
|
|
|
|
{
|
2024-01-21 00:09:28 +00:00
|
|
|
// The peer io object can destruct inside can_read_wrapper(), so keep
|
|
|
|
// it alive for the duration of this code block. This can happen when
|
|
|
|
// a BT handshake did not complete successfully for example.
|
|
|
|
auto const keep_alive = io->shared_from_this();
|
|
|
|
|
2022-12-16 23:21:01 +00:00
|
|
|
io->inbuf_.add(args->buf, args->len);
|
|
|
|
io->set_enabled(TR_DOWN, true);
|
|
|
|
io->can_read_wrapper();
|
2024-01-02 06:33:53 +00:00
|
|
|
|
2024-01-15 06:28:50 +00:00
|
|
|
// utp_read_drained() notifies libutp that we read a packet from them.
|
2024-01-02 06:33:53 +00:00
|
|
|
// It opens up the congestion window by sending an ACK (soonish) if
|
|
|
|
// one was not going to be sent.
|
2024-01-15 06:28:50 +00:00
|
|
|
utp_read_drained(args->socket);
|
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_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)
|
|
|
|
{
|
2024-01-02 06:33:53 +00:00
|
|
|
return io->read_buffer_size();
|
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
|
|
|
}
|