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.
|
2007-09-20 16:32:01 +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
|
|
|
|
|
2023-06-30 04:51:55 +00:00
|
|
|
#include <array>
|
2022-12-22 23:43:36 +00:00
|
|
|
#include <atomic>
|
2023-04-14 21:03:08 +00:00
|
|
|
#include <cstddef> // for size_t
|
2022-08-30 17:38:30 +00:00
|
|
|
#include <memory>
|
2021-12-15 21:25:42 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/transmission.h" // for tr_direction, tr_block_ind...
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/interned-string.h"
|
|
|
|
#include "libtransmission/net.h" // tr_socket_address
|
|
|
|
#include "libtransmission/peer-common.h" // for tr_peer
|
|
|
|
|
2021-10-11 21:54:16 +00:00
|
|
|
class tr_peerIo;
|
2023-08-01 14:56:26 +00:00
|
|
|
class tr_peerMsgs;
|
2023-07-14 12:06:25 +00:00
|
|
|
class tr_peer_info;
|
2023-04-14 21:03:08 +00:00
|
|
|
struct tr_torrent;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup peers Peers
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
using tr_peer_callback_bt = void (*)(tr_peerMsgs* peer, tr_peer_event const& event, void* client_data);
|
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
class tr_peerMsgs : public tr_peer
|
|
|
|
{
|
|
|
|
public:
|
2023-04-14 21:03:08 +00:00
|
|
|
tr_peerMsgs(
|
|
|
|
tr_torrent const* tor,
|
2023-07-14 12:06:25 +00:00
|
|
|
tr_peer_info* peer_info_in,
|
2023-04-14 21:03:08 +00:00
|
|
|
tr_interned_string user_agent,
|
|
|
|
bool connection_is_encrypted,
|
|
|
|
bool connection_is_incoming,
|
2023-08-01 14:56:26 +00:00
|
|
|
bool connection_is_utp);
|
2022-12-22 23:43:36 +00:00
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
~tr_peerMsgs() override;
|
2022-12-22 23:43:36 +00:00
|
|
|
|
2023-04-14 21:03:08 +00:00
|
|
|
[[nodiscard]] static auto size() noexcept
|
2022-12-22 23:43:36 +00:00
|
|
|
{
|
2023-01-28 23:58:20 +00:00
|
|
|
return n_peers.load();
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-04-14 21:03:08 +00:00
|
|
|
[[nodiscard]] constexpr auto client_is_choked() const noexcept
|
|
|
|
{
|
|
|
|
return client_is_choked_;
|
|
|
|
}
|
2010-03-06 14:56:15 +00:00
|
|
|
|
2023-04-14 21:03:08 +00:00
|
|
|
[[nodiscard]] constexpr auto client_is_interested() const noexcept
|
|
|
|
{
|
|
|
|
return client_is_interested_;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2023-04-14 21:03:08 +00:00
|
|
|
[[nodiscard]] constexpr auto peer_is_choked() const noexcept
|
|
|
|
{
|
|
|
|
return peer_is_choked_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto peer_is_interested() const noexcept
|
|
|
|
{
|
|
|
|
return peer_is_interested_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_encrypted() const noexcept
|
|
|
|
{
|
|
|
|
return connection_is_encrypted_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_incoming_connection() const noexcept
|
|
|
|
{
|
|
|
|
return connection_is_incoming_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_utp_connection() const noexcept
|
|
|
|
{
|
|
|
|
return connection_is_utp_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& user_agent() const noexcept
|
|
|
|
{
|
|
|
|
return user_agent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_active(tr_direction direction) const noexcept
|
|
|
|
{
|
|
|
|
return is_active_[direction];
|
|
|
|
}
|
2010-03-08 04:29:58 +00:00
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
[[nodiscard]] virtual tr_socket_address socket_address() const = 0;
|
2022-06-02 02:33:33 +00:00
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
virtual void cancel_block_request(tr_block_index_t block) = 0;
|
2008-09-17 19:44:24 +00:00
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
virtual void set_choke(bool peer_is_choked) = 0;
|
|
|
|
virtual void set_interested(bool client_is_interested) = 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
virtual void pulse() = 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-06-27 19:12:31 +00:00
|
|
|
virtual void onTorrentGotMetainfo() = 0;
|
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
virtual void on_piece_completed(tr_piece_index_t) = 0;
|
2022-06-27 19:12:31 +00:00
|
|
|
|
|
|
|
protected:
|
2023-04-14 21:03:08 +00:00
|
|
|
constexpr void set_client_choked(bool val) noexcept
|
|
|
|
{
|
|
|
|
client_is_choked_ = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void set_client_interested(bool val) noexcept
|
|
|
|
{
|
|
|
|
client_is_interested_ = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void set_peer_choked(bool val) noexcept
|
|
|
|
{
|
|
|
|
peer_is_choked_ = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void set_peer_interested(bool val) noexcept
|
|
|
|
{
|
|
|
|
peer_is_interested_ = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void set_active(tr_direction direction, bool active) noexcept
|
|
|
|
{
|
|
|
|
is_active_[direction] = active;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void set_user_agent(tr_interned_string val) noexcept
|
|
|
|
{
|
|
|
|
user_agent_ = val;
|
|
|
|
}
|
2022-12-22 23:43:36 +00:00
|
|
|
|
2023-08-01 14:56:26 +00:00
|
|
|
public:
|
|
|
|
// TODO(tearfur): change this to reference
|
|
|
|
tr_peer_info* const peer_info;
|
|
|
|
|
2022-12-22 23:43:36 +00:00
|
|
|
private:
|
2023-01-28 23:58:20 +00:00
|
|
|
static inline auto n_peers = std::atomic<size_t>{};
|
2023-04-14 21:03:08 +00:00
|
|
|
|
|
|
|
// What software the peer is running.
|
|
|
|
// Derived from the `v` string in LTEP's handshake dictionary, when available.
|
|
|
|
tr_interned_string user_agent_;
|
|
|
|
|
|
|
|
bool const connection_is_encrypted_;
|
|
|
|
bool const connection_is_incoming_;
|
|
|
|
bool const connection_is_utp_;
|
|
|
|
|
|
|
|
std::array<bool, 2> is_active_ = {};
|
|
|
|
|
|
|
|
// whether or not the peer is choking us.
|
|
|
|
bool client_is_choked_ = true;
|
|
|
|
|
|
|
|
// whether or not we've indicated to the peer that we would download from them if unchoked
|
|
|
|
bool client_is_interested_ = false;
|
|
|
|
|
|
|
|
// whether or not we've choked this peer
|
|
|
|
bool peer_is_choked_ = true;
|
|
|
|
|
|
|
|
// whether or not the peer has indicated it will download from us
|
|
|
|
bool peer_is_interested_ = false;
|
2021-10-07 13:33:55 +00:00
|
|
|
};
|
2013-05-27 21:04:48 +00:00
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
tr_peerMsgs* tr_peerMsgsNew(
|
|
|
|
tr_torrent* torrent,
|
2023-07-14 12:06:25 +00:00
|
|
|
tr_peer_info* peer_info,
|
2022-08-30 17:38:30 +00:00
|
|
|
std::shared_ptr<tr_peerIo> io,
|
2023-04-14 21:03:08 +00:00
|
|
|
tr_interned_string user_agent,
|
2023-08-01 14:56:26 +00:00
|
|
|
tr_peer_callback_bt callback,
|
2021-10-07 13:33:55 +00:00
|
|
|
void* callback_data);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/* @} */
|