2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 18:05:39 +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.
|
2008-11-24 20:17:36 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2022-06-28 06:22:34 +00:00
|
|
|
#include <array>
|
2023-06-30 04:51:55 +00:00
|
|
|
#include <cstddef> // size_t
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <cstdint> // uint8_t, uint32_t, uint64_t
|
2023-01-28 02:12:09 +00:00
|
|
|
#include <string>
|
2022-04-08 01:50:26 +00:00
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2022-01-09 16:55:09 +00:00
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
#include "libtransmission/bitfield.h"
|
|
|
|
#include "libtransmission/block-info.h"
|
|
|
|
#include "libtransmission/history.h"
|
|
|
|
#include "libtransmission/net.h" // tr_port
|
2011-02-17 05:14:53 +00:00
|
|
|
|
2013-02-03 23:29:34 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup peers Peers
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
class tr_peer;
|
2021-10-10 00:13:40 +00:00
|
|
|
class tr_swarm;
|
2022-06-29 20:08:58 +00:00
|
|
|
struct tr_bandwidth;
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Peer Publish / Subscribe
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2022-09-01 21:37:11 +00:00
|
|
|
class tr_peer_event
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2022-09-01 21:37:11 +00:00
|
|
|
public:
|
|
|
|
enum class Type
|
|
|
|
{
|
2023-08-01 14:56:26 +00:00
|
|
|
// Unless otherwise specified, all events are for BT peers only
|
|
|
|
ClientGotBlock, // applies to webseed too
|
2022-09-01 21:37:11 +00:00
|
|
|
ClientGotChoke,
|
2023-08-01 14:56:26 +00:00
|
|
|
ClientGotPieceData, // applies to webseed too
|
2022-09-01 21:37:11 +00:00
|
|
|
ClientGotAllowedFast,
|
|
|
|
ClientGotSuggest,
|
|
|
|
ClientGotPort,
|
2023-08-01 14:56:26 +00:00
|
|
|
ClientGotRej, // applies to webseed too
|
2022-09-01 21:37:11 +00:00
|
|
|
ClientGotBitfield,
|
|
|
|
ClientGotHave,
|
|
|
|
ClientGotHaveAll,
|
|
|
|
ClientGotHaveNone,
|
|
|
|
ClientSentPieceData,
|
2023-08-01 14:56:26 +00:00
|
|
|
Error // generic
|
2022-09-01 21:37:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Type type = Type::Error;
|
|
|
|
|
|
|
|
tr_bitfield* bitfield = nullptr; // for GotBitfield
|
|
|
|
uint32_t pieceIndex = 0; // for GotBlock, GotHave, Cancel, Allowed, Suggest
|
|
|
|
uint32_t offset = 0; // for GotBlock
|
|
|
|
uint32_t length = 0; // for GotBlock, GotPieceData
|
|
|
|
int err = 0; // errno for GotError
|
|
|
|
tr_port port = {}; // for GotPort
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotBlock(tr_block_info const& block_info, tr_block_index_t block) noexcept
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const loc = block_info.block_loc(block);
|
2022-09-01 21:37:11 +00:00
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientGotBlock;
|
|
|
|
event.pieceIndex = loc.piece;
|
|
|
|
event.offset = loc.piece_offset;
|
2023-04-23 01:25:55 +00:00
|
|
|
event.length = block_info.block_size(block);
|
2022-09-01 21:37:11 +00:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotAllowedFast(tr_piece_index_t piece) noexcept
|
|
|
|
{
|
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientGotAllowedFast;
|
|
|
|
event.pieceIndex = piece;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotBitfield(tr_bitfield* bitfield) noexcept
|
|
|
|
{
|
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientGotBitfield;
|
|
|
|
event.bitfield = bitfield;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotChoke() noexcept
|
|
|
|
{
|
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientGotChoke;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotError(int err) noexcept
|
|
|
|
{
|
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::Error;
|
|
|
|
event.err = err;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotHave(tr_piece_index_t piece) noexcept
|
|
|
|
{
|
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientGotHave;
|
|
|
|
event.pieceIndex = piece;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotHaveAll() noexcept
|
|
|
|
{
|
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientGotHaveAll;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotHaveNone() noexcept
|
|
|
|
{
|
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientGotHaveNone;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotPieceData(uint32_t length) noexcept
|
|
|
|
{
|
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientGotPieceData;
|
|
|
|
event.length = length;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotPort(tr_port port) noexcept
|
|
|
|
{
|
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientGotPort;
|
|
|
|
event.port = port;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotRejected(tr_block_info const& block_info, tr_block_index_t block) noexcept
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const loc = block_info.block_loc(block);
|
2022-09-01 21:37:11 +00:00
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientGotRej;
|
|
|
|
event.pieceIndex = loc.piece;
|
|
|
|
event.offset = loc.piece_offset;
|
2023-04-23 01:25:55 +00:00
|
|
|
event.length = block_info.block_size(block);
|
2022-09-01 21:37:11 +00:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto GotSuggest(tr_piece_index_t piece) noexcept
|
|
|
|
{
|
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientGotSuggest;
|
|
|
|
event.pieceIndex = piece;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr static auto SentPieceData(uint32_t length) noexcept
|
|
|
|
{
|
|
|
|
auto event = tr_peer_event{};
|
|
|
|
event.type = Type::ClientSentPieceData;
|
|
|
|
event.length = length;
|
|
|
|
return event;
|
|
|
|
}
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
using tr_peer_callback_generic = void (*)(tr_peer* peer, tr_peer_event const& event, void* client_data);
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2013-02-03 22:40:00 +00:00
|
|
|
/**
|
|
|
|
* State information about a connected peer.
|
|
|
|
*
|
2023-07-14 12:06:25 +00:00
|
|
|
* @see tr_peer_info
|
2013-02-04 16:23:33 +00:00
|
|
|
* @see tr_peerMsgs
|
2013-02-03 22:40:00 +00:00
|
|
|
*/
|
2021-10-07 13:33:55 +00:00
|
|
|
class tr_peer
|
2013-02-03 22:40:00 +00:00
|
|
|
{
|
2021-10-07 13:33:55 +00:00
|
|
|
public:
|
2023-11-12 20:38:27 +00:00
|
|
|
using Speed = libtransmission::Values::Speed;
|
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
tr_peer(tr_torrent const* tor);
|
2021-10-07 13:33:55 +00:00
|
|
|
virtual ~tr_peer();
|
|
|
|
|
2023-11-12 20:38:27 +00:00
|
|
|
[[nodiscard]] virtual Speed get_piece_speed(uint64_t now, tr_direction direction) const = 0;
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2022-11-23 05:26:10 +00:00
|
|
|
[[nodiscard]] bool hasPiece(tr_piece_index_t piece) const noexcept
|
|
|
|
{
|
|
|
|
return has().test(piece);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] float percentDone() const noexcept
|
|
|
|
{
|
|
|
|
return has().percent();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool isSeed() const noexcept
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return has().has_all();
|
2022-11-23 05:26:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
[[nodiscard]] virtual std::string display_name() const = 0;
|
2022-06-02 02:33:33 +00:00
|
|
|
|
2022-11-23 05:26:10 +00:00
|
|
|
[[nodiscard]] virtual tr_bitfield const& has() const noexcept = 0;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2022-06-29 01:48:39 +00:00
|
|
|
// requests that have been made but haven't been fulfilled yet
|
|
|
|
[[nodiscard]] virtual size_t activeReqCount(tr_direction) const noexcept = 0;
|
|
|
|
|
2022-07-02 19:42:16 +00:00
|
|
|
virtual void requestBlocks(tr_block_span_t const* block_spans, size_t n_spans) = 0;
|
|
|
|
|
2022-07-04 18:03:32 +00:00
|
|
|
struct RequestLimit
|
|
|
|
{
|
|
|
|
// How many blocks we could request.
|
|
|
|
size_t max_spans = 0;
|
|
|
|
|
|
|
|
// How many spans those blocks could be in.
|
|
|
|
// This is for webseeds, which make parallel requests.
|
|
|
|
size_t max_blocks = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// how many blocks could we request from this peer right now?
|
|
|
|
[[nodiscard]] virtual RequestLimit canRequest() const noexcept = 0;
|
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
tr_session* const session;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
tr_swarm* const swarm;
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2022-06-27 19:12:31 +00:00
|
|
|
tr_recentHistory<uint16_t> blocks_sent_to_peer;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2022-06-27 19:12:31 +00:00
|
|
|
tr_recentHistory<uint16_t> cancels_sent_to_client;
|
|
|
|
|
|
|
|
/// The following fields are only to be used in peer-mgr.cc.
|
2023-01-23 16:26:11 +00:00
|
|
|
/// TODO(ckerr): refactor them out of `tr_peer`
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2022-06-27 19:12:31 +00:00
|
|
|
// whether or not this peer sent us any given block
|
|
|
|
tr_bitfield blame;
|
2013-02-03 22:40:00 +00:00
|
|
|
|
2022-06-27 19:12:31 +00:00
|
|
|
// whether or not we should free this peer soon.
|
|
|
|
bool do_purge = false;
|
2010-06-19 14:33:10 +00:00
|
|
|
|
2022-06-27 19:12:31 +00:00
|
|
|
// how many bad pieces this piece has contributed to
|
|
|
|
uint8_t strikes = 0;
|
|
|
|
|
|
|
|
// how many blocks this peer has sent us
|
|
|
|
tr_recentHistory<uint16_t> blocks_sent_to_client;
|
2011-02-24 14:35:45 +00:00
|
|
|
|
2022-06-27 19:12:31 +00:00
|
|
|
// how many requests we made to this peer and then canceled
|
|
|
|
tr_recentHistory<uint16_t> cancels_sent_to_peer;
|
|
|
|
};
|
2013-07-08 16:41:12 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2013-07-08 16:41:12 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_swarm_stats
|
2013-07-08 16:41:12 +00:00
|
|
|
{
|
2022-06-28 06:22:34 +00:00
|
|
|
std::array<uint16_t, 2> active_peer_count;
|
2022-04-28 01:06:51 +00:00
|
|
|
uint16_t active_webseed_count;
|
2023-11-08 17:17:00 +00:00
|
|
|
// connected peers
|
2022-04-28 01:06:51 +00:00
|
|
|
uint16_t peer_count;
|
2023-11-08 17:17:00 +00:00
|
|
|
// connected peers by peer source
|
2022-06-28 06:22:34 +00:00
|
|
|
std::array<uint16_t, TR_PEER_FROM__MAX> peer_from_count;
|
2023-11-08 17:17:00 +00:00
|
|
|
// known peers by peer source
|
|
|
|
std::array<uint16_t, TR_PEER_FROM__MAX> known_peer_from_count;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2013-07-08 16:41:12 +00:00
|
|
|
|
2022-06-28 06:22:34 +00:00
|
|
|
tr_swarm_stats tr_swarmGetStats(tr_swarm const* swarm);
|
2013-07-08 16:41:12 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2013-07-08 16:41:12 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#undef EMSGSIZE
|
|
|
|
#define EMSGSIZE WSAEMSGSIZE
|
2008-10-10 00:38:37 +00:00
|
|
|
#endif
|
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/** @} */
|