2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-2022 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 <algorithm>
|
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>
|
|
|
|
|
2022-04-01 23:00:51 +00:00
|
|
|
#include <fmt/format.h>
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "transmission.h"
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "session.h"
|
2008-11-24 04:21:23 +00:00
|
|
|
#include "bandwidth.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "net.h"
|
|
|
|
#include "peer-io.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2011-10-08 23:53:27 +00:00
|
|
|
#include "tr-utp.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
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-10-11 15:39:41 +00:00
|
|
|
/* The amount of read bufferring that we allow for µTP sockets. */
|
2011-02-18 00:36:05 +00:00
|
|
|
|
2022-08-31 04:17:23 +00:00
|
|
|
static constexpr auto UtpReadBufferSize = 256 * 1024;
|
2011-02-18 00:36:05 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
#define tr_logAddErrorIo(io, msg) tr_logAddError(msg, (io)->addrStr())
|
|
|
|
#define tr_logAddWarnIo(io, msg) tr_logAddWarn(msg, (io)->addrStr())
|
|
|
|
#define tr_logAddDebugIo(io, msg) tr_logAddDebug(msg, (io)->addrStr())
|
|
|
|
#define tr_logAddTraceIo(io, msg) tr_logAddTrace(msg, (io)->addrStr())
|
2022-03-11 21:09:22 +00:00
|
|
|
|
2022-08-31 04:17:23 +00:00
|
|
|
static constexpr size_t guessPacketOverhead(size_t d)
|
2008-11-06 02:56:51 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* http://sd.wareonearth.com/~phil/net/overhead/
|
|
|
|
*
|
|
|
|
* TCP over Ethernet:
|
|
|
|
* Assuming no header compression (e.g. not PPP)
|
|
|
|
* Add 20 IPv4 header or 40 IPv6 header (no options)
|
|
|
|
* Add 20 TCP header
|
|
|
|
* Add 12 bytes optional TCP timestamps
|
|
|
|
* Max TCP Payload data rates over ethernet are thus:
|
2012-12-05 17:29:46 +00:00
|
|
|
* (1500-40)/ (38+1500) = 94.9285 % IPv4, minimal headers
|
|
|
|
* (1500-52)/ (38+1500) = 94.1482 % IPv4, TCP timestamps
|
|
|
|
* (1500-52)/ (42+1500) = 93.9040 % 802.1q, IPv4, TCP timestamps
|
|
|
|
* (1500-60)/ (38+1500) = 93.6281 % IPv6, minimal headers
|
|
|
|
* (1500-72)/ (38+1500) = 92.8479 % IPv6, TCP timestamps
|
|
|
|
* (1500-72)/ (42+1500) = 92.6070 % 802.1q, IPv6, ICP timestamps
|
2008-11-06 02:56:51 +00:00
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
double const assumed_payload_data_rate = 94.0;
|
2008-11-06 02:56:51 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
return (size_t)(d * (100.0 / assumed_payload_data_rate) - d);
|
2008-11-06 02:56:51 +00:00
|
|
|
}
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
static void didWriteWrapper(tr_peerIo* io, size_t bytes_transferred)
|
2007-12-01 23:08:34 +00:00
|
|
|
{
|
2022-07-10 18:51:35 +00:00
|
|
|
while (bytes_transferred != 0 && tr_isPeerIo(io) && !std::empty(io->outbuf_info))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-10 18:51:35 +00:00
|
|
|
auto& [n_bytes_left, is_piece_data] = io->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-10-25 16:14:42 +00:00
|
|
|
size_t const overhead = io->socket.type == TR_PEER_SOCKET_TYPE_TCP ? guessPacketOverhead(payload) : 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t const now = tr_time_msec();
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2022-07-10 18:51:35 +00:00
|
|
|
io->bandwidth().notifyBandwidthConsumed(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
|
|
|
{
|
2022-06-29 20:08:58 +00:00
|
|
|
io->bandwidth().notifyBandwidthConsumed(TR_UP, overhead, false, now);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-11-17 04:00:57 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->didWrite != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-10 18:51:35 +00:00
|
|
|
io->didWrite(io, payload, is_piece_data, io->userData);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2022-07-10 18:51:35 +00:00
|
|
|
if (!tr_isPeerIo(io))
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
2022-07-10 18:51:35 +00:00
|
|
|
break;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
io->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-08-30 17:38:30 +00:00
|
|
|
static void canReadWrapper(tr_peerIo* io_in)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-30 17:38:30 +00:00
|
|
|
auto const io = io_in->shared_from_this();
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceIo(io, "canRead");
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-11-28 01:58:35 +00:00
|
|
|
tr_session const* const session = io->session;
|
2009-10-10 17:37:34 +00:00
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
/* try to consume the input buffer */
|
2022-10-24 21:57:07 +00:00
|
|
|
if (io->canRead == 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-10-24 21:57:07 +00:00
|
|
|
auto const lock = session->unique_lock();
|
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;
|
|
|
|
size_t const old_len = io->readBufferSize();
|
|
|
|
int const ret = io->canRead(io.get(), io->userData, &piece);
|
|
|
|
size_t const used = old_len - io->readBufferSize();
|
2022-10-25 16:14:42 +00:00
|
|
|
auto const overhead = guessPacketOverhead(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
|
|
|
{
|
2022-10-24 21:57:07 +00:00
|
|
|
io->bandwidth().notifyBandwidthConsumed(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
|
|
|
{
|
2022-10-24 21:57:07 +00:00
|
|
|
io->bandwidth().notifyBandwidthConsumed(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)
|
|
|
|
{
|
|
|
|
io->bandwidth().notifyBandwidthConsumed(TR_UP, overhead, false, now);
|
|
|
|
}
|
2011-01-20 00:31:46 +00:00
|
|
|
|
2022-10-24 21:57:07 +00:00
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case READ_NOW:
|
|
|
|
if (io->readBufferSize() != 0)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void event_read_cb(evutil_socket_t fd, short /*event*/, void* vio)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* io = static_cast<tr_peerIo*>(vio);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2017-06-28 15:46:06 +00:00
|
|
|
TR_ASSERT(io->socket.type == TR_PEER_SOCKET_TYPE_TCP);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2008-12-23 17:11:31 +00:00
|
|
|
/* Limit the input buffer to 256K, so it doesn't grow too large */
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_direction const dir = TR_DOWN;
|
2022-10-25 16:14:42 +00:00
|
|
|
size_t const max = 256 * 1024;
|
2008-12-23 17:23:07 +00:00
|
|
|
|
2009-01-24 14:49:35 +00:00
|
|
|
io->pendingEvents &= ~EV_READ;
|
2009-01-19 13:55:41 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
auto const curlen = io->readBufferSize();
|
2022-10-26 16:20:10 +00:00
|
|
|
auto howmuch = static_cast<unsigned int>(curlen >= max ? 0 : max - curlen);
|
2022-06-29 20:08:58 +00:00
|
|
|
howmuch = io->bandwidth().clamp(TR_DOWN, howmuch);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceIo(io, "libevent says this peer is ready to read");
|
2008-12-20 22:19:34 +00:00
|
|
|
|
|
|
|
/* if we don't have any bandwidth left, stop reading */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (howmuch < 1)
|
|
|
|
{
|
2022-08-29 20:58:18 +00:00
|
|
|
io->setEnabled(dir, false);
|
2008-12-20 22:19:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
tr_error* error = nullptr;
|
2022-10-25 03:13:09 +00:00
|
|
|
if (auto const res = io->inbuf.addSocket(fd, howmuch, &error); res > 0)
|
2008-12-22 04:55:07 +00:00
|
|
|
{
|
2022-08-29 20:58:18 +00:00
|
|
|
io->setEnabled(dir, true);
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2008-12-22 04:55:07 +00:00
|
|
|
/* Invoke the user callback - must always be called last */
|
2017-04-19 12:04:45 +00:00
|
|
|
canReadWrapper(io);
|
2008-12-22 04:55:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-20 02:07:51 +00:00
|
|
|
short what = BEV_EVENT_READING;
|
2008-12-22 04:55:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (res == 0) /* EOF */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-12-20 02:07:51 +00:00
|
|
|
what |= BEV_EVENT_EOF;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-10-19 16:42:08 +00:00
|
|
|
if (error != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
if (error->code == EAGAIN || error->code == EINTR)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-29 20:58:18 +00:00
|
|
|
io->setEnabled(dir, true);
|
2008-12-22 04:55:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
what |= BEV_EVENT_ERROR;
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
tr_logAddDebugIo(
|
|
|
|
io,
|
|
|
|
fmt::format("event_read_cb err: res:{}, what:{}, errno:{} ({})", res, what, error->code, error->message));
|
|
|
|
}
|
2008-12-24 02:50:08 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->gotError != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
io->gotError(io, what, io->userData);
|
|
|
|
}
|
2008-12-22 04:55:07 +00:00
|
|
|
}
|
2022-10-19 16:42:08 +00:00
|
|
|
|
|
|
|
tr_error_clear(&error);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void event_write_cb(evutil_socket_t fd, short /*event*/, void* vio)
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* io = static_cast<tr_peerIo*>(vio);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
2017-06-28 15:46:06 +00:00
|
|
|
TR_ASSERT(io->socket.type == TR_PEER_SOCKET_TYPE_TCP);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2009-01-24 14:49:35 +00:00
|
|
|
io->pendingEvents &= ~EV_WRITE;
|
2009-01-19 13:55:41 +00:00
|
|
|
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceIo(io, "libevent says this peer is ready to write");
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
/* Write as much as possible, since the socket is non-blocking, write() will
|
|
|
|
* return if it can't write any more data without blocking */
|
2022-09-23 13:39:13 +00:00
|
|
|
auto constexpr Dir = TR_UP;
|
2022-10-19 16:42:08 +00:00
|
|
|
auto const howmuch = io->bandwidth().clamp(Dir, std::size(io->outbuf));
|
2008-12-20 22:19:34 +00:00
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
// if we don't have any bandwidth left, stop writing
|
2017-04-19 12:04:45 +00:00
|
|
|
if (howmuch < 1)
|
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
io->setEnabled(Dir, false);
|
2008-12-20 22:19:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
EVUTIL_SET_SOCKET_ERROR(0);
|
2022-10-21 18:15:14 +00:00
|
|
|
auto const n_written = io->outbuf.toSocket(fd, howmuch); // -1 on err, 0 on EOF
|
2022-09-23 13:39:13 +00:00
|
|
|
auto const err = EVUTIL_SOCKET_ERROR();
|
|
|
|
auto const should_retry = n_written == -1 && (err == 0 || err == EAGAIN || err == EINTR || err == EINPROGRESS);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
// schedule another write if we have more data to write & think future writes would succeed
|
2022-10-19 16:42:08 +00:00
|
|
|
if (!std::empty(io->outbuf) && (n_written > 0 || should_retry))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
io->setEnabled(Dir, true);
|
2008-12-20 22:19:34 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
if (n_written > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
didWriteWrapper(io, n_written);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-09-23 13:39:13 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
auto const what = n_written == -1 ? BEV_EVENT_WRITING | BEV_EVENT_ERROR : BEV_EVENT_WRITING | BEV_EVENT_EOF;
|
|
|
|
auto const errmsg = tr_net_strerror(err);
|
|
|
|
tr_logAddDebugIo(
|
|
|
|
io,
|
|
|
|
fmt::format("event_write_cb got an err. n_written:{}, what:{}, errno:{} ({})", n_written, what, err, errmsg));
|
|
|
|
if (io->gotError != nullptr)
|
|
|
|
{
|
|
|
|
io->gotError(io, what, io->userData);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-09-17 19:44:24 +00:00
|
|
|
}
|
2007-11-16 20:40:03 +00:00
|
|
|
|
2008-12-20 22:19:34 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2021-11-14 06:41:44 +00:00
|
|
|
static void maybeSetCongestionAlgorithm(tr_socket_t socket, std::string const& algorithm)
|
2010-04-22 02:04:43 +00:00
|
|
|
{
|
2021-11-14 06:41:44 +00:00
|
|
|
if (!std::empty(algorithm))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-14 06:41:44 +00:00
|
|
|
tr_netSetCongestionControl(socket, algorithm.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-04-22 02:04:43 +00:00
|
|
|
}
|
|
|
|
|
2011-02-18 00:45:44 +00:00
|
|
|
#ifdef WITH_UTP
|
2022-10-11 15:39:41 +00:00
|
|
|
/* µTP callbacks */
|
2011-02-18 00:24:13 +00:00
|
|
|
|
2022-07-16 20:25:44 +00:00
|
|
|
void tr_peerIo::readBufferAdd(void const* data, size_t n_bytes)
|
2011-02-18 00:24:13 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
inbuf.add(data, n_bytes);
|
2022-08-29 20:58:18 +00:00
|
|
|
setEnabled(TR_DOWN, true);
|
2022-07-16 20:25:44 +00:00
|
|
|
canReadWrapper(this);
|
2011-02-18 00:24:13 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
static size_t utp_get_rb_size(tr_peerIo* const io)
|
2011-02-18 00:24:13 +00:00
|
|
|
{
|
2022-07-27 14:03:13 +00:00
|
|
|
size_t const bytes = io->bandwidth().clamp(TR_DOWN, UtpReadBufferSize);
|
2011-02-18 00:36:09 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceIo(io, fmt::format("utp_get_rb_size is saying it's ready to read {} bytes", bytes));
|
2022-06-01 16:56:59 +00:00
|
|
|
return UtpReadBufferSize - bytes;
|
2011-02-18 00:24:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
static ssize_t tr_peerIoTryWrite(tr_peerIo* io, size_t howmuch);
|
2012-09-05 11:39:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void utp_on_writable(tr_peerIo* io)
|
2012-09-05 11:39:57 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceIo(io, "libutp says this peer is ready to write");
|
2012-09-05 11:39:57 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
auto const n = tr_peerIoTryWrite(io, SIZE_MAX);
|
2022-10-19 16:42:08 +00:00
|
|
|
io->setEnabled(TR_UP, n != 0 && !std::empty(io->outbuf));
|
2012-09-05 11:39:57 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
static void utp_on_state_change(tr_peerIo* const io, int const state)
|
2011-02-18 00:24:13 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (state == UTP_STATE_CONNECT)
|
|
|
|
{
|
2022-03-24 18:46:34 +00:00
|
|
|
tr_logAddTraceIo(io, "utp_on_state_change -- changed to connected");
|
2022-05-26 17:17:03 +00:00
|
|
|
io->utp_supported_ = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (state == UTP_STATE_WRITABLE)
|
|
|
|
{
|
2022-03-24 18:46:34 +00:00
|
|
|
tr_logAddTraceIo(io, "utp_on_state_change -- changed to writable");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((io->pendingEvents & EV_WRITE) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
utp_on_writable(io);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == UTP_STATE_EOF)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->gotError != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
io->gotError(io, BEV_EVENT_EOF, io->userData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == UTP_STATE_DESTROYING)
|
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddErrorIo(io, "Impossible state UTP_STATE_DESTROYING");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddErrorIo(io, fmt::format(_("Unknown state: {state}"), fmt::arg("state", state)));
|
2011-02-18 00:24:22 +00:00
|
|
|
}
|
2011-02-18 00:24:13 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
static void utp_on_error(tr_peerIo* const io, int const errcode)
|
2011-02-18 00:24:13 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddDebugIo(io, fmt::format("utp_on_error -- errcode is {}", errcode));
|
2011-02-18 00:40:22 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->gotError != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-02-18 00:24:22 +00:00
|
|
|
errno = errcode;
|
2017-04-19 12:04:45 +00:00
|
|
|
io->gotError(io, BEV_EVENT_ERROR, io->userData);
|
2011-02-18 00:24:22 +00:00
|
|
|
}
|
2011-02-18 00:24:13 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
static void utp_on_overhead(tr_peerIo* const io, bool const send, size_t const count, int /*type*/)
|
2011-02-18 00:24:13 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceIo(io, fmt::format("utp_on_overhead -- count is {}", count));
|
2011-02-18 00:40:22 +00:00
|
|
|
|
2022-06-29 20:08:58 +00:00
|
|
|
io->bandwidth().notifyBandwidthConsumed(send ? TR_UP : TR_DOWN, count, false, tr_time_msec());
|
2011-02-18 00:24:13 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
static uint64 utp_callback(utp_callback_arguments* args)
|
|
|
|
{
|
|
|
|
auto* const io = static_cast<tr_peerIo*>(utp_get_userdata(args->socket));
|
2011-02-18 00:24:13 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
if (io == nullptr)
|
|
|
|
{
|
|
|
|
#ifdef TR_UTP_TRACE
|
2011-02-18 00:24:18 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
if (args->callback_type != UTP_ON_STATE_CHANGE || args->u1.state != UTP_STATE_DESTROYING)
|
|
|
|
{
|
|
|
|
fmt::print(
|
|
|
|
stderr,
|
2022-10-11 15:39:41 +00:00
|
|
|
FMT_STRING("[µTP] [{}:{}] [{}] io is null! buf={}, len={}, flags={}, send/error_code/state={}, type={}\n"),
|
2022-07-08 15:13:22 +00:00
|
|
|
fmt::ptr(args->context),
|
|
|
|
fmt::ptr(args->socket),
|
|
|
|
utp_callback_names[args->callback_type],
|
|
|
|
fmt::ptr(args->buf),
|
|
|
|
args->len,
|
|
|
|
args->flags,
|
|
|
|
args->u1.send,
|
|
|
|
args->u2.type);
|
|
|
|
}
|
2011-02-18 00:24:18 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
#endif
|
2011-02-18 00:24:18 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-02-18 00:24:18 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(io));
|
|
|
|
TR_ASSERT(io->socket.handle.utp == args->socket);
|
2011-02-18 00:24:18 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
switch (args->callback_type)
|
|
|
|
{
|
|
|
|
case UTP_ON_READ:
|
2022-07-16 20:25:44 +00:00
|
|
|
io->readBufferAdd(args->buf, args->len);
|
2022-07-08 15:13:22 +00:00
|
|
|
break;
|
2011-02-18 00:24:18 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
case UTP_GET_READ_BUFFER_SIZE:
|
|
|
|
return utp_get_rb_size(io);
|
2011-02-18 00:24:18 +00:00
|
|
|
|
2022-07-08 15:13:22 +00:00
|
|
|
case UTP_ON_STATE_CHANGE:
|
|
|
|
utp_on_state_change(io, args->u1.state);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UTP_ON_ERROR:
|
|
|
|
utp_on_error(io, args->u1.error_code);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UTP_ON_OVERHEAD_STATISTICS:
|
|
|
|
utp_on_overhead(io, args->u1.send != 0, args->len, args->u2.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-18 00:24:18 +00:00
|
|
|
|
2011-02-18 00:45:44 +00:00
|
|
|
#endif /* #ifdef WITH_UTP */
|
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
std::shared_ptr<tr_peerIo> tr_peerIo::create(
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_session* session,
|
2022-06-29 20:08:58 +00:00
|
|
|
tr_bandwidth* parent,
|
2022-07-09 23:44:20 +00:00
|
|
|
tr_address const* addr,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_port port,
|
2022-02-28 22:26:26 +00:00
|
|
|
time_t current_time,
|
2021-12-21 22:14:15 +00:00
|
|
|
tr_sha1_digest_t const* torrent_hash,
|
2021-12-25 17:22:12 +00:00
|
|
|
bool is_incoming,
|
2022-02-28 22:26:26 +00:00
|
|
|
bool is_seed,
|
2021-08-15 09:41:48 +00:00
|
|
|
struct tr_peer_socket const socket)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(session->events != nullptr);
|
2022-07-16 20:25:44 +00:00
|
|
|
auto lock = session->unique_lock();
|
2017-06-28 15:46:06 +00:00
|
|
|
|
|
|
|
#ifdef WITH_UTP
|
|
|
|
TR_ASSERT(socket.type == TR_PEER_SOCKET_TYPE_TCP || socket.type == TR_PEER_SOCKET_TYPE_UTP);
|
|
|
|
#else
|
|
|
|
TR_ASSERT(socket.type == TR_PEER_SOCKET_TYPE_TCP);
|
2011-02-18 00:45:44 +00:00
|
|
|
#endif
|
2022-08-26 02:27:11 +00:00
|
|
|
TR_ASSERT(session->allowsTCP() || socket.type != TR_PEER_SOCKET_TYPE_TCP);
|
2009-01-28 19:35:39 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (socket.type == TR_PEER_SOCKET_TYPE_TCP)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-09 23:44:20 +00:00
|
|
|
session->setSocketTOS(socket.handle.tcp, addr->type);
|
2021-11-14 06:41:44 +00:00
|
|
|
maybeSetCongestionAlgorithm(socket.handle.tcp, session->peerCongestionAlgorithm());
|
2010-04-22 01:49:16 +00:00
|
|
|
}
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
auto io = std::shared_ptr<tr_peerIo>{
|
|
|
|
new tr_peerIo{ session, torrent_hash, is_incoming, *addr, port, is_seed, current_time, parent }
|
|
|
|
};
|
2008-09-17 19:44:24 +00:00
|
|
|
io->socket = socket;
|
2022-06-29 20:08:58 +00:00
|
|
|
io->bandwidth().setPeer(io);
|
|
|
|
tr_logAddTraceIo(io, fmt::format("bandwidth is {}; its parent is {}", fmt::ptr(&io->bandwidth()), fmt::ptr(parent)));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
switch (socket.type)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_TCP:
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceIo(io, fmt::format("socket (tcp) is {}", socket.handle.tcp));
|
2022-08-30 17:38:30 +00:00
|
|
|
io->event_read = event_new(session->eventBase(), socket.handle.tcp, EV_READ, event_read_cb, io.get());
|
|
|
|
io->event_write = event_new(session->eventBase(), socket.handle.tcp, EV_WRITE, event_write_cb, io.get());
|
2017-06-28 15:46:06 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2011-02-18 00:45:44 +00:00
|
|
|
#ifdef WITH_UTP
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_UTP:
|
2022-10-11 15:39:41 +00:00
|
|
|
tr_logAddTraceIo(io, fmt::format("socket (µTP) is {}", fmt::ptr(socket.handle.utp)));
|
2022-08-30 17:38:30 +00:00
|
|
|
utp_set_userdata(socket.handle.utp, io.get());
|
2017-06-28 15:46:06 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2011-02-18 00:45:44 +00:00
|
|
|
#endif
|
2011-02-18 00:23:51 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
default:
|
2022-08-10 13:34:51 +00:00
|
|
|
TR_ASSERT_MSG(false, fmt::format("unsupported peer socket type {:d}", socket.type));
|
2017-06-28 15:46:06 +00:00
|
|
|
}
|
|
|
|
|
2008-09-17 19:44:24 +00:00
|
|
|
return io;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 20:58:18 +00:00
|
|
|
void tr_peerIo::utpInit([[maybe_unused]] struct_utp_context* ctx)
|
2022-07-08 15:13:22 +00:00
|
|
|
{
|
|
|
|
#ifdef WITH_UTP
|
|
|
|
|
|
|
|
utp_set_callback(ctx, UTP_ON_READ, &utp_callback);
|
|
|
|
utp_set_callback(ctx, UTP_GET_READ_BUFFER_SIZE, &utp_callback);
|
|
|
|
utp_set_callback(ctx, UTP_ON_STATE_CHANGE, &utp_callback);
|
|
|
|
utp_set_callback(ctx, UTP_ON_ERROR, &utp_callback);
|
|
|
|
utp_set_callback(ctx, UTP_ON_OVERHEAD_STATISTICS, &utp_callback);
|
|
|
|
|
|
|
|
utp_context_set_option(ctx, UTP_RCVBUF, UtpReadBufferSize);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
std::shared_ptr<tr_peerIo> tr_peerIo::newIncoming(
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_session* session,
|
2022-06-29 20:08:58 +00:00
|
|
|
tr_bandwidth* parent,
|
2022-07-09 23:44:20 +00:00
|
|
|
tr_address const* addr,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_port port,
|
2022-02-28 22:26:26 +00:00
|
|
|
time_t current_time,
|
2017-06-28 15:46:06 +00:00
|
|
|
struct tr_peer_socket const socket)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2022-07-09 23:44:20 +00:00
|
|
|
TR_ASSERT(tr_address_is_valid(addr));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-08-29 20:58:18 +00:00
|
|
|
return tr_peerIo::create(session, parent, addr, port, current_time, nullptr, true, false, socket);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
std::shared_ptr<tr_peerIo> tr_peerIo::newOutgoing(
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_session* session,
|
2022-06-29 20:08:58 +00:00
|
|
|
tr_bandwidth* parent,
|
2022-07-09 23:44:20 +00:00
|
|
|
tr_address const* addr,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_port port,
|
2022-02-28 22:26:26 +00:00
|
|
|
time_t current_time,
|
2021-12-21 22:14:15 +00:00
|
|
|
tr_sha1_digest_t const& torrent_hash,
|
2022-02-28 22:26:26 +00:00
|
|
|
bool is_seed,
|
2021-08-15 09:41:48 +00:00
|
|
|
bool utp)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2022-07-09 23:44:20 +00:00
|
|
|
TR_ASSERT(tr_address_is_valid(addr));
|
2022-08-26 02:27:11 +00:00
|
|
|
TR_ASSERT(utp || session->allowsTCP());
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto socket = tr_peer_socket{};
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (utp)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-28 22:26:26 +00:00
|
|
|
socket = tr_netOpenPeerUTPSocket(session, addr, port, is_seed);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-18 16:01:52 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (socket.type == TR_PEER_SOCKET_TYPE_NONE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-28 22:26:26 +00:00
|
|
|
socket = tr_netOpenPeerSocket(session, addr, port, is_seed);
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddDebug(fmt::format(
|
|
|
|
"tr_netOpenPeerSocket returned {}",
|
|
|
|
socket.type != TR_PEER_SOCKET_TYPE_NONE ? socket.handle.tcp : TR_BAD_SOCKET));
|
2011-02-18 00:36:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
if (socket.type == TR_PEER_SOCKET_TYPE_NONE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-12-15 04:26:31 +00:00
|
|
|
|
2022-08-29 20:58:18 +00:00
|
|
|
return create(session, parent, addr, port, current_time, &torrent_hash, false, is_seed, socket);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void event_enable(tr_peerIo* io, short event)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(io->session != nullptr);
|
|
|
|
TR_ASSERT(io->session->events != nullptr);
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
bool const need_events = io->socket.type == TR_PEER_SOCKET_TYPE_TCP;
|
|
|
|
|
|
|
|
if (need_events)
|
2012-09-05 11:39:57 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(event_initialized(io->event_read));
|
|
|
|
TR_ASSERT(event_initialized(io->event_write));
|
2012-09-05 11:39:57 +00:00
|
|
|
}
|
2011-02-18 00:23:51 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((event & EV_READ) != 0 && (io->pendingEvents & EV_READ) == 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceIo(io, "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
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
event_add(io->event_read, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents |= EV_READ;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((event & EV_WRITE) != 0 && (io->pendingEvents & EV_WRITE) == 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceIo(io, "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
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
event_add(io->event_write, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents |= EV_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 21:54:16 +00:00
|
|
|
static void event_disable(tr_peerIo* io, short event)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(io->session->events != nullptr);
|
2012-09-05 11:39:57 +00:00
|
|
|
|
2017-06-28 15:46:06 +00:00
|
|
|
bool const need_events = io->socket.type == TR_PEER_SOCKET_TYPE_TCP;
|
|
|
|
|
|
|
|
if (need_events)
|
2012-09-05 11:39:57 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(event_initialized(io->event_read));
|
|
|
|
TR_ASSERT(event_initialized(io->event_write));
|
2012-09-05 11:39:57 +00:00
|
|
|
}
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((event & EV_READ) != 0 && (io->pendingEvents & EV_READ) != 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceIo(io, "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
|
|
|
{
|
|
|
|
event_del(io->event_read);
|
|
|
|
}
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents &= ~EV_READ;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((event & EV_WRITE) != 0 && (io->pendingEvents & EV_WRITE) != 0)
|
2010-01-17 19:21:04 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceIo(io, "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
|
|
|
{
|
|
|
|
event_del(io->event_write);
|
|
|
|
}
|
|
|
|
|
2010-01-17 19:21:04 +00:00
|
|
|
io->pendingEvents &= ~EV_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 20:58:18 +00:00
|
|
|
void tr_peerIo::setEnabled(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-08-29 20:58:18 +00:00
|
|
|
event_enable(this, 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-08-29 20:58:18 +00:00
|
|
|
event_disable(this, event);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-01-17 19:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
static void io_close_socket(tr_peerIo* io)
|
2011-02-18 00:45:44 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
switch (io->socket.type)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_PEER_SOCKET_TYPE_TCP:
|
|
|
|
tr_netClose(io->session, io->socket.handle.tcp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef WITH_UTP
|
|
|
|
|
|
|
|
case TR_PEER_SOCKET_TYPE_UTP:
|
2022-07-08 15:13:22 +00:00
|
|
|
utp_set_userdata(io->socket.handle.utp, nullptr);
|
|
|
|
utp_close(io->socket.handle.utp);
|
2017-06-28 15:46:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddDebugIo(io, fmt::format("unsupported peer socket type {}", io->socket.type));
|
2011-03-29 21:09:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
io->socket = {};
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->event_read != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_free(io->event_read);
|
2021-09-15 00:18:09 +00:00
|
|
|
io->event_read = nullptr;
|
2011-03-29 21:09:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (io->event_write != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_free(io->event_write);
|
2021-09-15 00:18:09 +00:00
|
|
|
io->event_write = nullptr;
|
2011-02-18 00:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
tr_peerIo::~tr_peerIo()
|
2007-10-02 16:12:44 +00:00
|
|
|
{
|
2022-08-30 17:38:30 +00:00
|
|
|
auto const lock = session->unique_lock();
|
|
|
|
TR_ASSERT(session->events != nullptr);
|
2009-01-05 04:27:54 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
this->canRead = nullptr;
|
|
|
|
this->didWrite = nullptr;
|
|
|
|
this->gotError = nullptr;
|
2009-01-24 17:20:07 +00:00
|
|
|
|
2022-08-30 17:38:30 +00:00
|
|
|
tr_logAddTraceIo(this, "in tr_peerIo destructor");
|
|
|
|
event_disable(this, EV_READ | EV_WRITE);
|
|
|
|
io_close_socket(this);
|
2009-01-05 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 21:09:22 +00:00
|
|
|
std::string tr_peerIo::addrStr() const
|
|
|
|
{
|
2022-05-26 17:17:03 +00:00
|
|
|
return tr_isPeerIo(this) ? this->addr_.readable(this->port_) : "error";
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
void tr_peerIo::setCallbacks(tr_can_read_cb readcb, tr_did_write_cb writecb, tr_net_error_cb errcb, void* user_data)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-29 18:33:08 +00:00
|
|
|
this->canRead = readcb;
|
|
|
|
this->didWrite = writecb;
|
|
|
|
this->gotError = errcb;
|
|
|
|
this->userData = user_data;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
void tr_peerIo::clear()
|
2009-01-05 18:20:47 +00:00
|
|
|
{
|
2022-08-29 18:33:08 +00:00
|
|
|
setCallbacks(nullptr, nullptr, nullptr, nullptr);
|
2022-08-29 20:58:18 +00:00
|
|
|
setEnabled(TR_UP, false);
|
|
|
|
setEnabled(TR_DOWN, false);
|
2022-08-29 18:33:08 +00:00
|
|
|
io_close_socket(this);
|
2009-01-05 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
int tr_peerIo::reconnect()
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-29 18:33:08 +00:00
|
|
|
TR_ASSERT(tr_isPeerIo(this));
|
|
|
|
TR_ASSERT(!this->isIncoming());
|
|
|
|
TR_ASSERT(this->session->allowsTCP());
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
short int const pending_events = this->pendingEvents;
|
|
|
|
event_disable(this, EV_READ | EV_WRITE);
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
io_close_socket(this);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
auto const [addr, port] = this->socketAddress();
|
|
|
|
this->socket = tr_netOpenPeerSocket(session, &addr, port, this->isSeed());
|
2010-01-17 19:21:04 +00:00
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
if (this->socket.type != TR_PEER_SOCKET_TYPE_TCP)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-06-28 15:46:06 +00:00
|
|
|
return -1;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
this->event_read = event_new(session->eventBase(), this->socket.handle.tcp, EV_READ, event_read_cb, this);
|
|
|
|
this->event_write = event_new(session->eventBase(), this->socket.handle.tcp, EV_WRITE, event_write_cb, this);
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
event_enable(this, pending_events);
|
|
|
|
this->session->setSocketTOS(this->socket.handle.tcp, addr.type);
|
|
|
|
maybeSetCongestionAlgorithm(this->socket.handle.tcp, session->peerCongestionAlgorithm());
|
2017-06-28 15:46:06 +00:00
|
|
|
|
|
|
|
return 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
static size_t getDesiredOutputBufferSize(tr_peerIo const* io, uint64_t now)
|
2008-11-28 16:00:29 +00:00
|
|
|
{
|
|
|
|
/* this is all kind of arbitrary, but what seems to work well is
|
2008-12-15 21:22:08 +00:00
|
|
|
* being large enough to hold the next 20 seconds' worth of input,
|
|
|
|
* or a few blocks, whichever is bigger.
|
2008-11-28 16:00:29 +00:00
|
|
|
* It's okay to tweak this as needed */
|
2022-10-25 16:14:42 +00:00
|
|
|
auto const current_speed_bytes_per_second = io->bandwidth().getPieceSpeedBytesPerSecond(now, TR_UP);
|
2019-03-17 04:07:48 +00:00
|
|
|
unsigned int const period = 15U; /* arbitrary */
|
2010-07-03 00:25:22 +00:00
|
|
|
/* the 3 is arbitrary; the .5 is to leave room for messages */
|
2022-10-25 16:14:42 +00:00
|
|
|
static auto const ceiling = static_cast<size_t>(tr_block_info::BlockSize * 3.5);
|
2022-09-07 16:04:28 +00:00
|
|
|
return std::max(ceiling, current_speed_bytes_per_second * period);
|
2008-11-28 16:00:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 20:58:18 +00:00
|
|
|
size_t tr_peerIo::getWriteBufferSpace(uint64_t now) const
|
2008-09-17 19:44:24 +00:00
|
|
|
{
|
2022-08-29 20:58:18 +00:00
|
|
|
size_t const desired_len = getDesiredOutputBufferSize(this, now);
|
2022-10-19 16:42:08 +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
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
void tr_peerIo::write(libtransmission::Buffer& buf, bool is_piece_data)
|
2015-06-24 21:24:41 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
auto const n_bytes = std::size(buf);
|
2015-06-24 21:24:41 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
auto const old_size = std::size(outbuf);
|
|
|
|
outbuf.add(buf);
|
|
|
|
for (auto iter = std::begin(outbuf) + old_size, end = std::end(outbuf); iter != end; ++iter)
|
2015-06-24 21:24:41 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
encrypt(1, &*iter);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
outbuf_info.emplace_back(n_bytes, is_piece_data);
|
2009-01-22 14:32:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
void tr_peerIo::writeBytes(void const* bytes, size_t n_bytes, bool is_piece_data)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
auto const old_size = std::size(outbuf);
|
2022-07-15 00:54:10 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf.reserve(old_size + n_bytes);
|
|
|
|
outbuf.add(bytes, n_bytes);
|
|
|
|
|
|
|
|
for (auto iter = std::begin(outbuf) + old_size, end = std::end(outbuf); iter != end; ++iter)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
encrypt(1, &*iter);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-04-27 17:52:28 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
outbuf_info.emplace_back(n_bytes, is_piece_data);
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
void tr_peerIo::readBytes(void* bytes, size_t byte_count)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-29 18:33:08 +00:00
|
|
|
TR_ASSERT(readBufferSize() >= byte_count);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
inbuf.toBuf(bytes, byte_count);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
if (isEncrypted())
|
2022-07-15 00:54:10 +00:00
|
|
|
{
|
2022-08-29 18:33:08 +00:00
|
|
|
decrypt(byte_count, bytes);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
void tr_peerIo::readUint16(uint16_t* setme)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
auto tmp = uint16_t{};
|
2022-08-29 18:33:08 +00:00
|
|
|
readBytes(&tmp, sizeof(tmp));
|
2017-04-19 12:04:45 +00:00
|
|
|
*setme = ntohs(tmp);
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
void tr_peerIo::readUint32(uint32_t* setme)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2021-10-22 13:51:36 +00:00
|
|
|
auto tmp = uint32_t{};
|
2022-08-29 18:33:08 +00:00
|
|
|
readBytes(&tmp, sizeof(tmp));
|
2017-04-19 12:04:45 +00:00
|
|
|
*setme = ntohl(tmp);
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 18:33:08 +00:00
|
|
|
void tr_peerIo::readBufferDrain(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));
|
|
|
|
readBytes(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
|
|
|
|
2008-12-16 22:08:17 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
static ssize_t tr_peerIoTryRead(tr_peerIo* io, size_t howmuch)
|
2008-12-16 22:08:17 +00:00
|
|
|
{
|
2022-06-29 20:08:58 +00:00
|
|
|
howmuch = io->bandwidth().clamp(TR_DOWN, howmuch);
|
2022-02-22 22:10:49 +00:00
|
|
|
if (howmuch == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
auto res = ssize_t{};
|
2022-02-22 22:10:49 +00:00
|
|
|
switch (io->socket.type)
|
2008-12-24 02:50:08 +00:00
|
|
|
{
|
2022-02-22 22:10:49 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_UTP:
|
2022-04-21 14:28:38 +00:00
|
|
|
/* UTP_RBDrained notifies libutp that your read buffer is empty.
|
2022-02-22 22:10:49 +00:00
|
|
|
* It opens up the congestion window by sending an ACK (soonish)
|
|
|
|
* if one was not going to be sent. */
|
2022-10-19 16:42:08 +00:00
|
|
|
if (io->readBufferSize() == 0)
|
2011-02-18 00:39:33 +00:00
|
|
|
{
|
2022-07-08 15:13:22 +00:00
|
|
|
utp_read_drained(io->socket.handle.utp);
|
2022-02-22 22:10:49 +00:00
|
|
|
}
|
2010-06-30 21:24:36 +00:00
|
|
|
|
2022-02-22 22:10:49 +00:00
|
|
|
break;
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2022-02-22 22:10:49 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_TCP:
|
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
tr_error* error = nullptr;
|
|
|
|
res = io->inbuf.addSocket(io->socket.handle.tcp, howmuch, &error);
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
if (io->readBufferSize() != 0)
|
2022-02-22 22:10:49 +00:00
|
|
|
{
|
|
|
|
canReadWrapper(io);
|
|
|
|
}
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
if (error != nullptr)
|
2022-02-22 22:10:49 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
if (error->code != EAGAIN && error->code != EINTR && error->code != EINPROGRESS && io->gotError != nullptr)
|
2017-06-28 15:46:06 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
short what = BEV_EVENT_READING | BEV_EVENT_ERROR;
|
|
|
|
|
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
what |= BEV_EVENT_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_logAddTraceIo(
|
|
|
|
io,
|
|
|
|
fmt::format(
|
|
|
|
"tr_peerIoTryRead err: res:{} what:{}, errno:{} ({})",
|
|
|
|
res,
|
|
|
|
what,
|
|
|
|
error->code,
|
|
|
|
error->message));
|
|
|
|
|
|
|
|
io->gotError(io, what, io->userData);
|
2017-06-28 15:46:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
tr_error_clear(&error);
|
2011-02-18 00:39:33 +00:00
|
|
|
}
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2022-02-22 22:10:49 +00:00
|
|
|
break;
|
2008-12-24 02:50:08 +00:00
|
|
|
}
|
2022-02-22 22:10:49 +00:00
|
|
|
|
|
|
|
default:
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddDebugIo(io, fmt::format("unsupported peer socket type {}", io->socket.type));
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
static ssize_t tr_peerIoTryWrite(tr_peerIo* io, size_t howmuch)
|
2008-12-16 22:08:17 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
auto const old_len = std::size(io->outbuf);
|
2021-10-22 13:51:36 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTraceIo(io, fmt::format("in tr_peerIoTryWrite {}", howmuch));
|
2022-02-22 22:10:49 +00:00
|
|
|
howmuch = std::min(howmuch, old_len);
|
2022-06-29 20:08:58 +00:00
|
|
|
howmuch = io->bandwidth().clamp(TR_UP, howmuch);
|
2022-02-22 22:10:49 +00:00
|
|
|
if (howmuch == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-22 22:10:49 +00:00
|
|
|
return 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
auto n = ssize_t{};
|
2022-02-22 22:10:49 +00:00
|
|
|
switch (io->socket.type)
|
2008-12-24 02:50:08 +00:00
|
|
|
{
|
2022-02-22 22:10:49 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_UTP:
|
2022-07-08 15:13:22 +00:00
|
|
|
{
|
2022-10-19 16:42:08 +00:00
|
|
|
auto iov = io->outbuf.vecs(howmuch);
|
|
|
|
n = utp_writev(io->socket.handle.utp, reinterpret_cast<struct utp_iovec*>(std::data(iov)), std::size(iov));
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
io->outbuf.drain(n);
|
|
|
|
didWriteWrapper(io, n);
|
|
|
|
}
|
|
|
|
break;
|
2022-07-08 15:13:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 22:10:49 +00:00
|
|
|
case TR_PEER_SOCKET_TYPE_TCP:
|
2011-02-18 00:39:33 +00:00
|
|
|
{
|
2022-02-22 22:10:49 +00:00
|
|
|
EVUTIL_SET_SOCKET_ERROR(0);
|
2022-10-21 18:15:14 +00:00
|
|
|
n = io->outbuf.toSocket(io->socket.handle.tcp, howmuch);
|
2022-02-22 22:10:49 +00:00
|
|
|
int const e = EVUTIL_SOCKET_ERROR();
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2022-02-22 22:10:49 +00:00
|
|
|
if (n > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-22 22:10:49 +00:00
|
|
|
didWriteWrapper(io, n);
|
|
|
|
}
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2022-02-22 22:10:49 +00:00
|
|
|
if (n < 0 && io->gotError != nullptr && e != 0 && e != EPIPE && e != EAGAIN && e != EINTR && e != EINPROGRESS)
|
|
|
|
{
|
|
|
|
short const what = BEV_EVENT_WRITING | BEV_EVENT_ERROR;
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTraceIo(
|
|
|
|
io,
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::format("tr_peerIoTryWrite err: res:{}, what:{}, errno:{} ({})", n, what, e, tr_net_strerror(e)));
|
2022-02-22 22:10:49 +00:00
|
|
|
io->gotError(io, what, io->userData);
|
2011-02-18 00:39:33 +00:00
|
|
|
}
|
2017-06-28 15:46:06 +00:00
|
|
|
|
2022-02-22 22:10:49 +00:00
|
|
|
break;
|
2008-12-24 02:50:08 +00:00
|
|
|
}
|
2022-02-22 22:10:49 +00:00
|
|
|
|
|
|
|
default:
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddDebugIo(io, fmt::format("unsupported peer socket type {}", io->socket.type));
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
ssize_t tr_peerIo::flush(tr_direction dir, size_t limit)
|
2008-12-16 22:08:17 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2008-12-16 22:08:17 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
auto const bytes_used = dir == TR_DOWN ? tr_peerIoTryRead(this, limit) : tr_peerIoTryWrite(this, limit);
|
2022-08-29 20:58:18 +00:00
|
|
|
tr_logAddTraceIo(this, fmt::format("flushing peer-io, direction:{}, limit:{}, byte_used:{}", dir, limit, bytes_used));
|
2022-02-22 22:10:49 +00:00
|
|
|
return bytes_used;
|
2008-12-16 22:08:17 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
ssize_t tr_peerIo::flushOutgoingProtocolMsgs()
|
2009-04-21 16:18:51 +00:00
|
|
|
{
|
2022-08-29 20:58:18 +00:00
|
|
|
size_t byte_count = 0;
|
2009-04-21 16:18:51 +00:00
|
|
|
|
|
|
|
/* count up how many bytes are used by non-piece-data messages
|
|
|
|
at the front of our outbound queue */
|
2022-08-29 20:58:18 +00:00
|
|
|
for (auto const& [n_bytes, is_piece_data] : outbuf_info)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-10 18:51:35 +00:00
|
|
|
if (is_piece_data)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-04-21 16:18:51 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2022-08-29 20:58:18 +00:00
|
|
|
byte_count += n_bytes;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-21 16:18:51 +00:00
|
|
|
|
2022-08-29 20:58:18 +00:00
|
|
|
return flush(TR_UP, byte_count);
|
2009-04-21 16:18:51 +00:00
|
|
|
}
|