2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2008-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2022-03-28 22:13:32 +00:00
|
|
|
#include <iterator>
|
2022-06-20 04:08:58 +00:00
|
|
|
#include <memory>
|
2022-06-29 01:48:39 +00:00
|
|
|
#include <numeric> // std::accumulate()
|
2021-09-27 19:28:11 +00:00
|
|
|
#include <set>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <vector>
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-03-30 19:59:13 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/bandwidth.h"
|
|
|
|
#include "libtransmission/cache.h"
|
|
|
|
#include "libtransmission/peer-io.h"
|
|
|
|
#include "libtransmission/peer-mgr.h"
|
|
|
|
#include "libtransmission/timer.h"
|
|
|
|
#include "libtransmission/torrent.h"
|
|
|
|
#include "libtransmission/utils-ev.h"
|
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
#include "libtransmission/web-utils.h"
|
|
|
|
#include "libtransmission/web.h"
|
|
|
|
#include "libtransmission/webseed.h"
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2022-02-12 22:52:40 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
class tr_webseed;
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2022-02-14 02:09:56 +00:00
|
|
|
void on_idle(tr_webseed* w);
|
|
|
|
|
|
|
|
class tr_webseed_task
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2022-02-14 02:09:56 +00:00
|
|
|
private:
|
2022-11-06 21:11:30 +00:00
|
|
|
libtransmission::evhelpers::evbuffer_unique_ptr const content_{ evbuffer_new() };
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2022-02-14 02:09:56 +00:00
|
|
|
public:
|
2022-02-23 13:38:18 +00:00
|
|
|
tr_webseed_task(tr_torrent* tor, tr_webseed* webseed_in, tr_block_span_t blocks_in)
|
2022-02-14 02:09:56 +00:00
|
|
|
: webseed{ webseed_in }
|
|
|
|
, session{ tor->session }
|
2022-02-23 13:38:18 +00:00
|
|
|
, blocks{ blocks_in }
|
2023-04-23 01:25:55 +00:00
|
|
|
, end_byte{ tor->block_loc(blocks.end - 1).byte + tor->block_size(blocks.end - 1) }
|
|
|
|
, loc{ tor->block_loc(blocks.begin) }
|
2022-02-14 02:09:56 +00:00
|
|
|
{
|
|
|
|
}
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2022-02-14 02:09:56 +00:00
|
|
|
tr_webseed* const webseed;
|
|
|
|
|
|
|
|
[[nodiscard]] auto* content() const
|
|
|
|
{
|
|
|
|
return content_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_session* const session;
|
2022-02-23 13:38:18 +00:00
|
|
|
tr_block_span_t const blocks;
|
|
|
|
uint64_t const end_byte;
|
|
|
|
|
|
|
|
// the current position in the task; i.e., the next block to save
|
|
|
|
tr_block_info::Location loc;
|
2022-02-14 02:09:56 +00:00
|
|
|
|
|
|
|
bool dead = false;
|
|
|
|
};
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2022-02-12 22:52:40 +00:00
|
|
|
/**
|
|
|
|
* Manages how many web tasks should be running at a time.
|
|
|
|
*
|
|
|
|
* - When all is well, allow multiple tasks running in parallel.
|
|
|
|
* - If we get an error, throttle down to only one at a time
|
|
|
|
* until we get piece data.
|
|
|
|
* - If we have too many errors in a row, put the peer in timeout
|
|
|
|
* and don't allow _any_ connections for awhile.
|
|
|
|
*/
|
|
|
|
class ConnectionLimiter
|
|
|
|
{
|
|
|
|
public:
|
2022-04-05 03:51:56 +00:00
|
|
|
constexpr void taskStarted() noexcept
|
2022-02-12 22:52:40 +00:00
|
|
|
{
|
|
|
|
++n_tasks;
|
|
|
|
}
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2022-02-12 22:52:40 +00:00
|
|
|
void taskFinished(bool success)
|
|
|
|
{
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
taskFailed();
|
|
|
|
}
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2022-02-12 22:52:40 +00:00
|
|
|
TR_ASSERT(n_tasks > 0);
|
|
|
|
--n_tasks;
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
constexpr void gotData() noexcept
|
2022-02-12 22:52:40 +00:00
|
|
|
{
|
|
|
|
TR_ASSERT(n_tasks > 0);
|
|
|
|
n_consecutive_failures = 0;
|
|
|
|
paused_until = 0;
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] size_t slotsAvailable() const noexcept
|
2022-02-12 22:52:40 +00:00
|
|
|
{
|
|
|
|
if (isPaused())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const max = maxConnections();
|
|
|
|
if (n_tasks >= max)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return max - n_tasks;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] bool isPaused() const noexcept
|
2022-02-12 22:52:40 +00:00
|
|
|
{
|
|
|
|
return paused_until > tr_time();
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr size_t maxConnections() const noexcept
|
2022-02-12 22:52:40 +00:00
|
|
|
{
|
|
|
|
return n_consecutive_failures > 0 ? 1 : MaxConnections;
|
|
|
|
}
|
|
|
|
|
|
|
|
void taskFailed()
|
|
|
|
{
|
|
|
|
TR_ASSERT(n_tasks > 0);
|
|
|
|
|
|
|
|
if (++n_consecutive_failures >= MaxConsecutiveFailures)
|
|
|
|
{
|
|
|
|
paused_until = tr_time() + TimeoutIntervalSecs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static time_t constexpr TimeoutIntervalSecs = 120;
|
|
|
|
static size_t constexpr MaxConnections = 4;
|
|
|
|
static size_t constexpr MaxConsecutiveFailures = MaxConnections;
|
|
|
|
|
|
|
|
size_t n_tasks = 0;
|
|
|
|
size_t n_consecutive_failures = 0;
|
|
|
|
time_t paused_until = 0;
|
|
|
|
};
|
2021-10-07 13:33:55 +00:00
|
|
|
|
2022-07-02 19:42:16 +00:00
|
|
|
void task_request_next_chunk(tr_webseed_task* task);
|
|
|
|
void onBufferGotData(evbuffer* /*buf*/, evbuffer_cb_info const* info, void* vtask);
|
|
|
|
|
2022-11-23 05:26:10 +00:00
|
|
|
class tr_webseed final : public tr_peer
|
2011-01-06 01:00:21 +00:00
|
|
|
{
|
2021-10-07 13:33:55 +00:00
|
|
|
public:
|
|
|
|
tr_webseed(struct tr_torrent* tor, std::string_view url, tr_peer_callback callback_in, void* callback_data_in)
|
|
|
|
: tr_peer{ tor }
|
|
|
|
, torrent_id{ tr_torrentId(tor) }
|
|
|
|
, base_url{ url }
|
|
|
|
, callback{ callback_in }
|
|
|
|
, callback_data{ callback_data_in }
|
2022-11-23 05:26:10 +00:00
|
|
|
, idle_timer_{ session->timerMaker().create([this]() { on_idle(this); }) }
|
2023-04-23 01:25:55 +00:00
|
|
|
, have_{ tor->piece_count() }
|
2022-10-01 14:12:49 +00:00
|
|
|
, bandwidth_{ &tor->bandwidth_ }
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
have_.set_has_all();
|
2023-05-06 04:11:05 +00:00
|
|
|
idle_timer_->start_repeating(IdleTimerInterval);
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 17:03:28 +00:00
|
|
|
tr_webseed(tr_webseed&&) = delete;
|
|
|
|
tr_webseed(tr_webseed const&) = delete;
|
|
|
|
tr_webseed& operator=(tr_webseed&&) = delete;
|
|
|
|
tr_webseed& operator=(tr_webseed const&) = delete;
|
|
|
|
|
2021-10-07 13:33:55 +00:00
|
|
|
~tr_webseed() override
|
|
|
|
{
|
|
|
|
// flag all the pending tasks as dead
|
|
|
|
std::for_each(std::begin(tasks), std::end(tasks), [](auto* task) { task->dead = true; });
|
|
|
|
tasks.clear();
|
|
|
|
}
|
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
[[nodiscard]] tr_torrent* getTorrent() const
|
|
|
|
{
|
|
|
|
return tr_torrentFindFromId(session, torrent_id);
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
[[nodiscard]] bool isTransferringPieces( //
|
|
|
|
uint64_t now,
|
|
|
|
tr_direction dir,
|
|
|
|
tr_bytes_per_second_t* setme_bytes_per_second) const override
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2022-10-25 16:14:42 +00:00
|
|
|
tr_bytes_per_second_t bytes_per_second = 0;
|
2021-10-07 13:33:55 +00:00
|
|
|
bool is_active = false;
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (dir == TR_DOWN)
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
|
|
|
is_active = !std::empty(tasks);
|
2023-04-23 01:25:55 +00:00
|
|
|
bytes_per_second = bandwidth_.get_piece_speed_bytes_per_second(now, dir);
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (setme_bytes_per_second != nullptr)
|
2021-10-07 13:33:55 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
*setme_bytes_per_second = bytes_per_second;
|
2021-10-07 13:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return is_active;
|
|
|
|
}
|
|
|
|
|
2022-06-29 20:08:58 +00:00
|
|
|
[[nodiscard]] tr_bandwidth& bandwidth() noexcept override
|
2022-06-29 01:48:39 +00:00
|
|
|
{
|
2022-06-29 20:08:58 +00:00
|
|
|
return bandwidth_;
|
2022-06-29 01:48:39 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 size_t activeReqCount(tr_direction dir) const noexcept override
|
2022-06-29 01:48:39 +00:00
|
|
|
{
|
|
|
|
if (dir == TR_CLIENT_TO_PEER) // blocks we've requested
|
|
|
|
{
|
|
|
|
return std::accumulate(
|
|
|
|
std::begin(tasks),
|
|
|
|
std::end(tasks),
|
|
|
|
size_t{},
|
|
|
|
[](size_t sum, auto const* task) { return sum + (task->blocks.end - task->blocks.begin); });
|
|
|
|
}
|
|
|
|
|
|
|
|
// webseed will never request blocks from us
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-08 22:44:19 +00:00
|
|
|
[[nodiscard]] std::string display_name() const override
|
2022-06-02 02:33:33 +00:00
|
|
|
{
|
|
|
|
if (auto const parsed = tr_urlParse(base_url); parsed)
|
|
|
|
{
|
|
|
|
return fmt::format(FMT_STRING("{:s}:{:d}"), parsed->host, parsed->port);
|
|
|
|
}
|
|
|
|
|
|
|
|
return base_url;
|
|
|
|
}
|
|
|
|
|
2022-11-23 05:26:10 +00:00
|
|
|
[[nodiscard]] tr_bitfield const& has() const noexcept override
|
2022-06-27 19:12:31 +00:00
|
|
|
{
|
2022-11-23 05:26:10 +00:00
|
|
|
return have_;
|
2022-06-27 19:12:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
void gotPieceData(uint32_t n_bytes)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
bandwidth_.notify_bandwidth_consumed(TR_DOWN, n_bytes, true, tr_time_msec());
|
2022-09-01 21:37:11 +00:00
|
|
|
publish(tr_peer_event::GotPieceData(n_bytes));
|
2022-02-23 13:38:18 +00:00
|
|
|
connection_limiter.gotData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void publishRejection(tr_block_span_t block_span)
|
|
|
|
{
|
2022-09-01 21:37:11 +00:00
|
|
|
auto const* const tor = getTorrent();
|
2022-02-23 13:38:18 +00:00
|
|
|
for (auto block = block_span.begin; block < block_span.end; ++block)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
publish(tr_peer_event::GotRejected(tor->block_info(), block));
|
2022-02-23 13:38:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 19:42:16 +00:00
|
|
|
void requestBlocks(tr_block_span_t const* block_spans, size_t n_spans) override
|
|
|
|
{
|
|
|
|
auto* const tor = getTorrent();
|
2023-04-23 01:25:55 +00:00
|
|
|
if (tor == nullptr || !tor->is_running() || tor->is_done())
|
2022-07-02 19:42:16 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const *span = block_spans, *end = span + n_spans; span != end; ++span)
|
|
|
|
{
|
|
|
|
auto* const task = new tr_webseed_task{ tor, this, *span };
|
|
|
|
evbuffer_add_cb(task->content(), onBufferGotData, task);
|
|
|
|
tasks.insert(task);
|
|
|
|
task_request_next_chunk(task);
|
|
|
|
|
|
|
|
tr_peerMgrClientSentRequests(tor, this, *span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:03:32 +00:00
|
|
|
[[nodiscard]] RequestLimit canRequest() const noexcept override
|
|
|
|
{
|
|
|
|
auto const n_slots = connection_limiter.slotsAvailable();
|
|
|
|
if (n_slots == 0)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
if (auto const* const tor = getTorrent(); tor == nullptr || !tor->is_running() || tor->is_done())
|
2022-07-04 18:03:32 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefer to request large, contiguous chunks from webseeds.
|
|
|
|
// The actual value of '64' is arbitrary here;
|
|
|
|
// we could probably be smarter about this.
|
|
|
|
auto constexpr PreferredBlocksPerTask = size_t{ 64 };
|
|
|
|
return { n_slots, n_slots * PreferredBlocksPerTask };
|
|
|
|
}
|
|
|
|
|
2022-09-01 21:37:11 +00:00
|
|
|
void publish(tr_peer_event const& peer_event)
|
|
|
|
{
|
|
|
|
if (callback != nullptr)
|
|
|
|
{
|
|
|
|
(*callback)(this, peer_event, callback_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 15:43:04 +00:00
|
|
|
tr_torrent_id_t const torrent_id;
|
2021-10-07 13:33:55 +00:00
|
|
|
std::string const base_url;
|
|
|
|
tr_peer_callback const callback;
|
|
|
|
void* const callback_data;
|
|
|
|
|
2022-02-12 22:52:40 +00:00
|
|
|
ConnectionLimiter connection_limiter;
|
2021-09-27 19:28:11 +00:00
|
|
|
std::set<tr_webseed_task*> tasks;
|
2011-07-10 15:24:51 +00:00
|
|
|
|
2022-02-14 02:09:56 +00:00
|
|
|
private:
|
2022-08-11 17:28:37 +00:00
|
|
|
static auto constexpr IdleTimerInterval = 2s;
|
2022-11-23 05:26:10 +00:00
|
|
|
|
|
|
|
std::unique_ptr<libtransmission::Timer> const idle_timer_;
|
|
|
|
|
|
|
|
tr_bitfield have_;
|
|
|
|
|
|
|
|
tr_bandwidth bandwidth_;
|
2022-02-14 02:09:56 +00:00
|
|
|
};
|
2021-12-07 18:11:28 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2011-09-12 21:46:15 +00:00
|
|
|
struct write_block_data
|
|
|
|
{
|
2022-02-14 02:09:56 +00:00
|
|
|
private:
|
2022-11-06 21:11:30 +00:00
|
|
|
libtransmission::evhelpers::evbuffer_unique_ptr const content_{ evbuffer_new() };
|
2022-02-14 02:09:56 +00:00
|
|
|
|
|
|
|
public:
|
2022-06-17 15:43:04 +00:00
|
|
|
write_block_data(
|
2022-06-20 04:08:58 +00:00
|
|
|
tr_session* session,
|
|
|
|
tr_torrent_id_t tor_id,
|
|
|
|
tr_block_index_t block,
|
2023-05-13 19:16:00 +00:00
|
|
|
std::unique_ptr<Cache::BlockData> data,
|
2022-06-20 04:08:58 +00:00
|
|
|
tr_webseed* webseed)
|
|
|
|
: session_{ session }
|
|
|
|
, tor_id_{ tor_id }
|
|
|
|
, block_{ block }
|
|
|
|
, data_{ std::move(data) }
|
|
|
|
, webseed_{ webseed }
|
2022-02-14 02:09:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-05 04:26:03 +00:00
|
|
|
void write_block_func()
|
2022-02-23 13:38:18 +00:00
|
|
|
{
|
2022-11-28 15:45:39 +00:00
|
|
|
if (auto const* const tor = tr_torrentFindFromId(session_, tor_id_); tor != nullptr)
|
2022-02-23 13:38:18 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
session_->cache->write_block(tor_id_, block_, std::move(data_));
|
2023-04-23 01:25:55 +00:00
|
|
|
webseed_->publish(tr_peer_event::GotBlock(tor->block_info(), block_));
|
2022-02-23 13:38:18 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 04:26:03 +00:00
|
|
|
delete this;
|
2022-02-23 13:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-06-20 04:08:58 +00:00
|
|
|
tr_session* const session_;
|
|
|
|
tr_torrent_id_t const tor_id_;
|
|
|
|
tr_block_index_t const block_;
|
2023-05-13 19:16:00 +00:00
|
|
|
std::unique_ptr<Cache::BlockData> data_;
|
2022-06-20 04:08:58 +00:00
|
|
|
tr_webseed* const webseed_;
|
2011-09-12 21:46:15 +00:00
|
|
|
};
|
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
void useFetchedBlocks(tr_webseed_task* task)
|
2011-07-10 15:24:51 +00:00
|
|
|
{
|
2022-02-23 13:38:18 +00:00
|
|
|
auto* const session = task->session;
|
|
|
|
auto const lock = session->unique_lock();
|
2011-07-10 15:24:51 +00:00
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
auto* const webseed = task->webseed;
|
|
|
|
auto const* const tor = webseed->getTorrent();
|
|
|
|
if (tor == nullptr)
|
2011-07-10 15:24:51 +00:00
|
|
|
{
|
2022-02-23 13:38:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-07-10 15:24:51 +00:00
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
auto* const buf = task->content();
|
|
|
|
for (;;)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const block_size = tor->block_size(task->loc.block);
|
2022-02-23 13:38:18 +00:00
|
|
|
if (evbuffer_get_length(buf) < block_size)
|
2011-07-10 15:24:51 +00:00
|
|
|
{
|
2022-02-23 13:38:18 +00:00
|
|
|
break;
|
2015-07-05 07:54:46 +00:00
|
|
|
}
|
2011-07-10 15:24:51 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
if (tor->has_block(task->loc.block))
|
2022-02-23 13:38:18 +00:00
|
|
|
{
|
|
|
|
evbuffer_drain(buf, block_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-13 19:16:00 +00:00
|
|
|
auto block_buf = std::make_unique<Cache::BlockData>(block_size);
|
2022-06-20 04:08:58 +00:00
|
|
|
evbuffer_remove(task->content(), std::data(*block_buf), std::size(*block_buf));
|
2023-05-13 19:16:00 +00:00
|
|
|
auto* const data = new write_block_data{ session, tor->id(), task->loc.block, std::move(block_buf), webseed };
|
2022-11-04 21:20:27 +00:00
|
|
|
session->runInSessionThread(&write_block_data::write_block_func, data);
|
2022-02-23 13:38:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
task->loc = tor->byte_loc(task->loc.byte + block_size);
|
2022-02-23 13:38:18 +00:00
|
|
|
|
2022-04-24 17:46:44 +00:00
|
|
|
TR_ASSERT(task->loc.byte <= task->end_byte);
|
2022-02-23 13:38:18 +00:00
|
|
|
TR_ASSERT(task->loc.byte == task->end_byte || task->loc.block_offset == 0);
|
|
|
|
}
|
2011-07-10 15:24:51 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2011-09-12 21:46:15 +00:00
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
void onBufferGotData(evbuffer* /*buf*/, evbuffer_cb_info const* info, void* vtask)
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const n_added = info->n_added;
|
2022-02-14 02:09:56 +00:00
|
|
|
auto* const task = static_cast<tr_webseed_task*>(vtask);
|
2022-02-23 13:38:18 +00:00
|
|
|
if (n_added == 0 || task->dead)
|
2011-01-06 01:00:21 +00:00
|
|
|
{
|
2022-02-23 13:38:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-05-18 20:49:40 +00:00
|
|
|
auto const lock = task->session->unique_lock();
|
|
|
|
task->webseed->gotPieceData(n_added);
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 19:42:16 +00:00
|
|
|
void on_idle(tr_webseed* webseed)
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2022-07-04 18:03:32 +00:00
|
|
|
auto const [max_spans, max_blocks] = webseed->canRequest();
|
|
|
|
if (max_spans == 0 || max_blocks == 0)
|
2022-02-23 13:38:18 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefer to request large, contiguous chunks from webseeds.
|
|
|
|
// The actual value of '64' is arbitrary here; we could probably
|
|
|
|
// be smarter about this.
|
2022-07-04 18:03:32 +00:00
|
|
|
auto spans = tr_peerMgrGetNextRequests(webseed->getTorrent(), webseed, max_blocks);
|
|
|
|
if (std::size(spans) > max_spans)
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2022-07-04 18:03:32 +00:00
|
|
|
spans.resize(max_spans);
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
2022-07-02 19:42:16 +00:00
|
|
|
webseed->requestBlocks(std::data(spans), std::size(spans));
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
void onPartialDataFetched(tr_web::FetchResponse const& web_response)
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
auto const& [status, body, did_connect, did_timeout, vtask] = web_response;
|
|
|
|
bool const success = status == 206;
|
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
auto* const task = static_cast<tr_webseed_task*>(vtask);
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
if (task->dead)
|
2013-02-04 16:23:33 +00:00
|
|
|
{
|
2022-02-23 13:38:18 +00:00
|
|
|
delete task;
|
2017-04-19 12:04:45 +00:00
|
|
|
return;
|
2013-02-04 16:23:33 +00:00
|
|
|
}
|
2008-11-08 22:24:07 +00:00
|
|
|
|
2022-12-23 17:34:08 +00:00
|
|
|
auto* const webseed = task->webseed;
|
|
|
|
webseed->connection_limiter.taskFinished(success);
|
|
|
|
|
2022-03-25 05:24:04 +00:00
|
|
|
if (auto const* const tor = webseed->getTorrent(); tor == nullptr)
|
2008-11-08 22:24:07 +00:00
|
|
|
{
|
2022-02-23 13:38:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-07-10 15:24:51 +00:00
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
webseed->publishRejection({ task->loc.block, task->blocks.end });
|
|
|
|
webseed->tasks.erase(task);
|
|
|
|
delete task;
|
|
|
|
return;
|
|
|
|
}
|
2011-07-10 15:24:51 +00:00
|
|
|
|
2022-05-18 20:49:40 +00:00
|
|
|
useFetchedBlocks(task);
|
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
if (task->loc.byte < task->end_byte)
|
|
|
|
{
|
|
|
|
// Request finished successfully but there's still data missing.
|
|
|
|
// That means we've reached the end of a file and need to request
|
|
|
|
// the next one
|
|
|
|
task_request_next_chunk(task);
|
|
|
|
return;
|
2008-11-08 22:24:07 +00:00
|
|
|
}
|
2022-02-23 13:38:18 +00:00
|
|
|
|
2022-04-24 17:46:44 +00:00
|
|
|
TR_ASSERT(evbuffer_get_length(task->content()) == 0);
|
|
|
|
TR_ASSERT(task->loc.byte == task->end_byte);
|
2022-02-23 13:38:18 +00:00
|
|
|
webseed->tasks.erase(task);
|
|
|
|
delete task;
|
|
|
|
|
|
|
|
on_idle(webseed);
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
template<typename OutputIt>
|
2022-09-06 17:52:58 +00:00
|
|
|
void makeUrl(tr_webseed const* const webseed, std::string_view name, OutputIt out)
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2022-09-06 17:52:58 +00:00
|
|
|
auto const& url = webseed->base_url;
|
2022-03-28 22:13:32 +00:00
|
|
|
|
|
|
|
out = std::copy(std::begin(url), std::end(url), out);
|
2011-01-06 01:00:21 +00:00
|
|
|
|
2022-02-12 22:52:40 +00:00
|
|
|
if (tr_strvEndsWith(url, "/"sv) && !std::empty(name))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-21 13:43:09 +00:00
|
|
|
tr_urlPercentEncode(out, name, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-01-06 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
void task_request_next_chunk(tr_webseed_task* task)
|
2011-01-06 01:00:21 +00:00
|
|
|
{
|
2022-02-23 13:38:18 +00:00
|
|
|
auto* const webseed = task->webseed;
|
2022-11-28 15:45:39 +00:00
|
|
|
auto const* const tor = webseed->getTorrent();
|
2022-02-14 02:09:56 +00:00
|
|
|
if (tor == nullptr)
|
2008-06-07 21:26:41 +00:00
|
|
|
{
|
2022-02-14 02:09:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const loc = tor->byte_loc(task->loc.byte + evbuffer_get_length(task->content()));
|
2013-02-04 16:23:33 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const [file_index, file_offset] = tor->file_offset(loc);
|
|
|
|
auto const left_in_file = tor->file_size(file_index) - file_offset;
|
2022-04-24 17:46:44 +00:00
|
|
|
auto const left_in_task = task->end_byte - loc.byte;
|
2022-02-23 13:38:18 +00:00
|
|
|
auto const this_chunk = std::min(left_in_file, left_in_task);
|
2022-04-24 17:46:44 +00:00
|
|
|
TR_ASSERT(this_chunk > 0U);
|
2013-04-13 20:25:28 +00:00
|
|
|
|
2022-02-25 20:45:00 +00:00
|
|
|
webseed->connection_limiter.taskStarted();
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
auto url = tr_urlbuf{};
|
2023-04-23 01:25:55 +00:00
|
|
|
makeUrl(webseed, tor->file_subpath(file_index), std::back_inserter(url));
|
2022-03-28 22:13:32 +00:00
|
|
|
auto options = tr_web::FetchOptions{ url.sv(), onPartialDataFetched, task };
|
2022-03-30 19:59:13 +00:00
|
|
|
options.range = fmt::format(FMT_STRING("{:d}-{:d}"), file_offset, file_offset + this_chunk - 1);
|
2022-06-17 15:43:04 +00:00
|
|
|
options.speed_limit_tag = tor->id();
|
2022-02-23 13:38:18 +00:00
|
|
|
options.buffer = task->content();
|
2022-10-14 02:25:02 +00:00
|
|
|
tor->session->fetch(std::move(options));
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 02:09:56 +00:00
|
|
|
} // namespace
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-06-10 02:36:52 +00:00
|
|
|
|
2022-02-14 02:09:56 +00:00
|
|
|
tr_peer* tr_webseedNew(tr_torrent* torrent, std::string_view url, tr_peer_callback callback, void* callback_data)
|
2008-06-10 01:38:12 +00:00
|
|
|
{
|
2022-02-14 02:09:56 +00:00
|
|
|
return new tr_webseed(torrent, url, callback, callback_data);
|
2008-06-10 01:38:12 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 02:09:56 +00:00
|
|
|
tr_webseed_view tr_webseedView(tr_peer const* peer)
|
2011-01-06 01:00:21 +00:00
|
|
|
{
|
2022-02-14 02:09:56 +00:00
|
|
|
auto const* w = dynamic_cast<tr_webseed const*>(peer);
|
|
|
|
if (w == nullptr)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
auto bytes_per_second = tr_bytes_per_second_t{ 0 };
|
2022-06-27 19:12:31 +00:00
|
|
|
auto const is_downloading = peer->isTransferringPieces(tr_time_msec(), TR_DOWN, &bytes_per_second);
|
2022-02-14 02:09:56 +00:00
|
|
|
return { w->base_url.c_str(), is_downloading, bytes_per_second };
|
2008-06-07 21:26:41 +00:00
|
|
|
}
|