Merge pull request #6724 from glassez/libt11

Don't use features, deprecated in libtorrent-1.1
This commit is contained in:
Vladimir Golovnev 2017-05-16 18:54:00 +03:00 committed by GitHub
commit 44f8a48d73
24 changed files with 578 additions and 437 deletions

View file

@ -51,6 +51,7 @@
#include <libtorrent/bdecode.hpp>
#endif
#include <libtorrent/bencode.hpp>
#include <libtorrent/disk_io_thread.hpp>
#include <libtorrent/error_code.hpp>
#include <libtorrent/extensions/ut_metadata.hpp>
#include <libtorrent/extensions/ut_pex.hpp>
@ -62,6 +63,10 @@
#endif
#include <libtorrent/magnet_uri.hpp>
#include <libtorrent/session.hpp>
#if LIBTORRENT_VERSION_NUM >= 10100
#include <libtorrent/session_stats.hpp>
#endif
#include <libtorrent/session_status.hpp>
#include <libtorrent/torrent_info.hpp>
#include "base/logger.h"
@ -78,13 +83,11 @@
#include "base/utils/fs.h"
#include "base/utils/random.h"
#include "base/utils/string.h"
#include "cachestatus.h"
#include "magneturi.h"
#include "private/filterparserthread.h"
#include "private/statistics.h"
#include "private/bandwidthscheduler.h"
#include "private/resumedatasavingmanager.h"
#include "sessionstatus.h"
#include "torrenthandle.h"
#include "tracker.h"
#include "trackerentry.h"
@ -357,6 +360,8 @@ Session::Session(QObject *parent)
{
QMetaObject::invokeMethod(this, "readAlerts", Qt::QueuedConnection);
});
configurePeerClasses();
#endif
// Enabling plugins
@ -429,6 +434,11 @@ Session::Session(QObject *parent)
// initialize PortForwarder instance
Net::PortForwarder::initInstance(m_nativeSession);
#if LIBTORRENT_VERSION_NUM >= 10100
initMetrics();
m_statsUpdateTimer.start();
#endif
qDebug("* BitTorrent Session constructed");
}
@ -870,6 +880,7 @@ void Session::configure()
libt::settings_pack settingsPack = m_nativeSession->get_settings();
configure(settingsPack);
m_nativeSession->apply_settings(settingsPack);
configurePeerClasses();
#endif
if (m_IPFilteringChanged) {
@ -909,6 +920,75 @@ void Session::adjustLimits(libt::settings_pack &settingsPack)
, maxActive > -1 ? maxActive + m_extraLimit : maxActive);
}
void Session::initMetrics()
{
m_metricIndices.net.hasIncomingConnections = libt::find_metric_idx("net.has_incoming_connections");
Q_ASSERT(m_metricIndices.net.hasIncomingConnections >= 0);
m_metricIndices.net.sentPayloadBytes = libt::find_metric_idx("net.sent_payload_bytes");
Q_ASSERT(m_metricIndices.net.sentPayloadBytes >= 0);
m_metricIndices.net.recvPayloadBytes = libt::find_metric_idx("net.recv_payload_bytes");
Q_ASSERT(m_metricIndices.net.recvPayloadBytes >= 0);
m_metricIndices.net.sentBytes = libt::find_metric_idx("net.sent_bytes");
Q_ASSERT(m_metricIndices.net.sentBytes >= 0);
m_metricIndices.net.recvBytes = libt::find_metric_idx("net.recv_bytes");
Q_ASSERT(m_metricIndices.net.recvBytes >= 0);
m_metricIndices.net.sentIPOverheadBytes = libt::find_metric_idx("net.sent_ip_overhead_bytes");
Q_ASSERT(m_metricIndices.net.sentIPOverheadBytes >= 0);
m_metricIndices.net.recvIPOverheadBytes = libt::find_metric_idx("net.recv_ip_overhead_bytes");
Q_ASSERT(m_metricIndices.net.recvIPOverheadBytes >= 0);
m_metricIndices.net.sentTrackerBytes = libt::find_metric_idx("net.sent_tracker_bytes");
Q_ASSERT(m_metricIndices.net.sentTrackerBytes >= 0);
m_metricIndices.net.recvTrackerBytes = libt::find_metric_idx("net.recv_tracker_bytes");
Q_ASSERT(m_metricIndices.net.recvTrackerBytes >= 0);
m_metricIndices.net.recvRedundantBytes = libt::find_metric_idx("net.recv_redundant_bytes");
Q_ASSERT(m_metricIndices.net.recvRedundantBytes >= 0);
m_metricIndices.net.recvFailedBytes = libt::find_metric_idx("net.recv_failed_bytes");
Q_ASSERT(m_metricIndices.net.recvFailedBytes >= 0);
m_metricIndices.peer.numPeersConnected = libt::find_metric_idx("peer.num_peers_connected");
Q_ASSERT(m_metricIndices.peer.numPeersConnected >= 0);
m_metricIndices.peer.numPeersDownDisk = libt::find_metric_idx("peer.num_peers_down_disk");
Q_ASSERT(m_metricIndices.peer.numPeersDownDisk >= 0);
m_metricIndices.peer.numPeersUpDisk = libt::find_metric_idx("peer.num_peers_up_disk");
Q_ASSERT(m_metricIndices.peer.numPeersUpDisk >= 0);
m_metricIndices.dht.dhtBytesIn = libt::find_metric_idx("dht.dht_bytes_in");
Q_ASSERT(m_metricIndices.dht.dhtBytesIn >= 0);
m_metricIndices.dht.dhtBytesOut = libt::find_metric_idx("dht.dht_bytes_out");
Q_ASSERT(m_metricIndices.dht.dhtBytesOut >= 0);
m_metricIndices.dht.dhtNodes = libt::find_metric_idx("dht.dht_nodes");
Q_ASSERT(m_metricIndices.dht.dhtNodes >= 0);
m_metricIndices.disk.diskBlocksInUse = libt::find_metric_idx("disk.disk_blocks_in_use");
Q_ASSERT(m_metricIndices.disk.diskBlocksInUse >= 0);
m_metricIndices.disk.numBlocksRead = libt::find_metric_idx("disk.num_blocks_read");
Q_ASSERT(m_metricIndices.disk.numBlocksRead >= 0);
m_metricIndices.disk.numBlocksCacheHits = libt::find_metric_idx("disk.num_blocks_cache_hits");
Q_ASSERT(m_metricIndices.disk.numBlocksCacheHits >= 0);
m_metricIndices.disk.queuedDiskJobs = libt::find_metric_idx("disk.queued_disk_jobs");
Q_ASSERT(m_metricIndices.disk.queuedDiskJobs >= 0);
m_metricIndices.disk.diskJobTime = libt::find_metric_idx("disk.disk_job_time");
Q_ASSERT(m_metricIndices.disk.diskJobTime >= 0);
}
void Session::configure(libtorrent::settings_pack &settingsPack)
{
Logger* const logger = Logger::instance();
@ -1048,16 +1128,12 @@ void Session::configure(libtorrent::settings_pack &settingsPack)
settingsPack.set_int(libt::settings_pack::outgoing_port, outgoingPortsMin());
settingsPack.set_int(libt::settings_pack::num_outgoing_ports, outgoingPortsMax() - outgoingPortsMin() + 1);
// Ignore limits on LAN
settingsPack.set_bool(libt::settings_pack::ignore_limits_on_local_network, ignoreLimitsOnLAN());
// Include overhead in transfer limits
settingsPack.set_bool(libt::settings_pack::rate_limit_ip_overhead, includeOverheadInLimits());
// IP address to announce to trackers
settingsPack.set_str(libt::settings_pack::announce_ip, announceIP().toStdString());
// Super seeding
settingsPack.set_bool(libt::settings_pack::strict_super_seeding, isSuperSeedingEnabled());
// * Max Half-open connections
settingsPack.set_int(libt::settings_pack::half_open_limit, maxHalfOpenConnections());
// * Max connections limit
settingsPack.set_int(libt::settings_pack::connections_limit, maxConnections());
// * Global max upload slots
@ -1065,8 +1141,6 @@ void Session::configure(libtorrent::settings_pack &settingsPack)
// uTP
settingsPack.set_bool(libt::settings_pack::enable_incoming_utp, isUTPEnabled());
settingsPack.set_bool(libt::settings_pack::enable_outgoing_utp, isUTPEnabled());
// uTP rate limiting
settingsPack.set_bool(libt::settings_pack::rate_limit_utp, isUTPRateLimited());
settingsPack.set_int(libt::settings_pack::mixed_mode_algorithm, isUTPRateLimited()
? libt::settings_pack::prefer_tcp
: libt::settings_pack::peer_proportional);
@ -1079,6 +1153,70 @@ void Session::configure(libtorrent::settings_pack &settingsPack)
settingsPack.set_bool(libt::settings_pack::enable_lsd, isLSDEnabled());
}
void Session::configurePeerClasses()
{
libt::ip_filter f;
f.add_rule(libt::address_v4::from_string("0.0.0.0")
, libt::address_v4::from_string("255.255.255.255")
, 1 << libt::session::global_peer_class_id);
#if TORRENT_USE_IPV6
f.add_rule(libt::address_v6::from_string("::0")
, libt::address_v6::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
, 1 << libt::session::global_peer_class_id);
#endif
if (ignoreLimitsOnLAN()) {
// local networks
f.add_rule(libt::address_v4::from_string("10.0.0.0")
, libt::address_v4::from_string("10.255.255.255")
, 1 << libt::session::local_peer_class_id);
f.add_rule(libt::address_v4::from_string("172.16.0.0")
, libt::address_v4::from_string("172.31.255.255")
, 1 << libt::session::local_peer_class_id);
f.add_rule(libt::address_v4::from_string("192.168.0.0")
, libt::address_v4::from_string("192.168.255.255")
, 1 << libt::session::local_peer_class_id);
// link local
f.add_rule(libt::address_v4::from_string("169.254.0.0")
, libt::address_v4::from_string("169.254.255.255")
, 1 << libt::session::local_peer_class_id);
// loopback
f.add_rule(libt::address_v4::from_string("127.0.0.0")
, libt::address_v4::from_string("127.255.255.255")
, 1 << libt::session::local_peer_class_id);
#if TORRENT_USE_IPV6
// link local
f.add_rule(libt::address_v6::from_string("fe80::")
, libt::address_v6::from_string("febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
, 1 << libt::session::local_peer_class_id);
// unique local addresses
f.add_rule(libt::address_v6::from_string("fc00::")
, libt::address_v6::from_string("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
, 1 << libt::session::local_peer_class_id);
// loopback
f.add_rule(libt::address_v6::from_string("::1")
, libt::address_v6::from_string("::1")
, 1 << libt::session::local_peer_class_id);
#endif
}
m_nativeSession->set_peer_class_filter(f);
libt::peer_class_type_filter peerClassTypeFilter;
peerClassTypeFilter.add(libt::peer_class_type_filter::tcp_socket, libt::session::tcp_peer_class_id);
peerClassTypeFilter.add(libt::peer_class_type_filter::ssl_tcp_socket, libt::session::tcp_peer_class_id);
peerClassTypeFilter.add(libt::peer_class_type_filter::i2p_socket, libt::session::tcp_peer_class_id);
if (isUTPRateLimited()) {
peerClassTypeFilter.add(libt::peer_class_type_filter::utp_socket
, libt::session::local_peer_class_id);
peerClassTypeFilter.add(libt::peer_class_type_filter::utp_socket
, libt::session::global_peer_class_id);
peerClassTypeFilter.add(libt::peer_class_type_filter::ssl_utp_socket
, libt::session::local_peer_class_id);
peerClassTypeFilter.add(libt::peer_class_type_filter::ssl_utp_socket
, libt::session::global_peer_class_id);
}
m_nativeSession->set_peer_class_type_filter(peerClassTypeFilter);
}
#else
void Session::adjustLimits(libt::session_settings &sessionSettings)
@ -1609,7 +1747,6 @@ bool Session::addTorrent_impl(AddTorrentData addData, const MagnetUri &magnetUri
libt::add_torrent_params p;
InfoHash hash;
std::vector<char> buf(fastresumeData.constData(), fastresumeData.constData() + fastresumeData.size());
std::vector<boost::uint8_t> filePriorities;
QString savePath;
@ -1663,7 +1800,7 @@ bool Session::addTorrent_impl(AddTorrentData addData, const MagnetUri &magnetUri
if (addData.resumed && !fromMagnetUri) {
// Set torrent fast resume data
p.resume_data = buf;
p.resume_data = {fastresumeData.constData(), fastresumeData.constData() + fastresumeData.size()};
p.flags |= libt::add_torrent_params::flag_use_resume_save_path;
}
else {
@ -3061,14 +3198,14 @@ void Session::recursiveTorrentDownload(const InfoHash &hash)
}
}
SessionStatus Session::status() const
const SessionStatus &Session::status() const
{
return m_nativeSession->status();
return m_status;
}
CacheStatus Session::cacheStatus() const
const CacheStatus &Session::cacheStatus() const
{
return m_nativeSession->get_cache_status();
return m_cacheStatus;
}
// Will resume torrents in backup directory
@ -3169,6 +3306,9 @@ quint64 Session::getAlltimeUL() const
void Session::refresh()
{
m_nativeSession->post_torrent_updates();
#if LIBTORRENT_VERSION_NUM >= 10100
m_nativeSession->post_session_stats();
#endif
}
void Session::handleIPFilterParsed(int ruleCount)
@ -3277,6 +3417,11 @@ void Session::handleAlert(libt::alert *a)
case libt::state_update_alert::alert_type:
handleStateUpdateAlert(static_cast<libt::state_update_alert*>(a));
break;
#if LIBTORRENT_VERSION_NUM >= 10100
case libt::session_stats_alert::alert_type:
handleSessionStatsAlert(static_cast<libt::session_stats_alert*>(a));
break;
#endif
case libt::file_error_alert::alert_type:
handleFileErrorAlert(static_cast<libt::file_error_alert*>(a));
break;
@ -3502,7 +3647,13 @@ void Session::handlePeerBanAlert(libt::peer_ban_alert *p)
void Session::handleUrlSeedAlert(libt::url_seed_alert *p)
{
Logger::instance()->addMessage(tr("URL seed lookup failed for URL: '%1', message: %2").arg(QString::fromStdString(p->url)).arg(QString::fromStdString(p->message())), Log::CRITICAL);
Logger::instance()->addMessage(tr("URL seed lookup failed for URL: '%1', message: %2")
#if LIBTORRENT_VERSION_NUM >= 10100
.arg(QString::fromStdString(p->server_url()))
#else
.arg(QString::fromStdString(p->url))
#endif
.arg(QString::fromStdString(p->message())), Log::CRITICAL);
}
void Session::handleListenSucceededAlert(libt::listen_succeeded_alert *p)
@ -3554,8 +3705,113 @@ void Session::handleExternalIPAlert(libt::external_ip_alert *p)
Logger::instance()->addMessage(tr("External IP: %1", "e.g. External IP: 192.168.0.1").arg(p->external_address.to_string(ec).c_str()), Log::INFO);
}
#if LIBTORRENT_VERSION_NUM >= 10100
void Session::handleSessionStatsAlert(libt::session_stats_alert *p)
{
qreal interval = m_statsUpdateTimer.restart() / 1000.;
m_status.hasIncomingConnections = static_cast<bool>(p->values[m_metricIndices.net.hasIncomingConnections]);
const auto ipOverheadDownload = p->values[m_metricIndices.net.recvIPOverheadBytes];
const auto ipOverheadUpload = p->values[m_metricIndices.net.sentIPOverheadBytes];
const auto totalDownload = p->values[m_metricIndices.net.recvBytes] + ipOverheadDownload;
const auto totalUpload = p->values[m_metricIndices.net.sentBytes] + ipOverheadUpload;
const auto totalPayloadDownload = p->values[m_metricIndices.net.recvPayloadBytes];
const auto totalPayloadUpload = p->values[m_metricIndices.net.sentPayloadBytes];
const auto trackerDownload = p->values[m_metricIndices.net.recvTrackerBytes];
const auto trackerUpload = p->values[m_metricIndices.net.sentTrackerBytes];
const auto dhtDownload = p->values[m_metricIndices.dht.dhtBytesIn];
const auto dhtUpload = p->values[m_metricIndices.dht.dhtBytesOut];
auto calcRate = [interval](quint64 previous, quint64 current)
{
Q_ASSERT(current >= previous);
return static_cast<quint64>((current - previous) / interval);
};
m_status.payloadDownloadRate = calcRate(m_status.totalPayloadDownload, totalPayloadDownload);
m_status.payloadUploadRate = calcRate(m_status.totalPayloadUpload, totalPayloadUpload);
m_status.downloadRate = calcRate(m_status.totalDownload, totalDownload);
m_status.uploadRate = calcRate(m_status.totalUpload, totalUpload);
m_status.ipOverheadDownloadRate = calcRate(m_status.ipOverheadDownload, ipOverheadDownload);
m_status.ipOverheadUploadRate = calcRate(m_status.ipOverheadUpload, ipOverheadUpload);
m_status.dhtDownloadRate = calcRate(m_status.dhtDownload, dhtDownload);
m_status.dhtUploadRate = calcRate(m_status.dhtUpload, dhtUpload);
m_status.trackerDownloadRate = calcRate(m_status.trackerDownload, trackerDownload);
m_status.trackerUploadRate = calcRate(m_status.trackerUpload, trackerUpload);
m_status.totalDownload = totalDownload;
m_status.totalUpload = totalUpload;
m_status.totalPayloadDownload = totalPayloadDownload;
m_status.totalPayloadUpload = totalPayloadUpload;
m_status.ipOverheadDownload = ipOverheadDownload;
m_status.ipOverheadUpload = ipOverheadUpload;
m_status.trackerDownload = trackerDownload;
m_status.trackerUpload = trackerUpload;
m_status.dhtDownload = dhtDownload;
m_status.dhtUpload = dhtUpload;
m_status.totalWasted = p->values[m_metricIndices.net.recvRedundantBytes]
+ p->values[m_metricIndices.net.recvFailedBytes];
m_status.dhtNodes = p->values[m_metricIndices.dht.dhtNodes];
m_status.diskReadQueue = p->values[m_metricIndices.peer.numPeersUpDisk];
m_status.diskWriteQueue = p->values[m_metricIndices.peer.numPeersDownDisk];
m_status.peersCount = p->values[m_metricIndices.peer.numPeersConnected];
const auto numBlocksRead = p->values[m_metricIndices.disk.numBlocksRead];
m_cacheStatus.totalUsedBuffers = p->values[m_metricIndices.disk.diskBlocksInUse];
m_cacheStatus.readRatio = numBlocksRead > 0
? static_cast<qreal>(p->values[m_metricIndices.disk.numBlocksCacheHits]) / numBlocksRead
: -1;
m_cacheStatus.jobQueueLength = p->values[m_metricIndices.disk.queuedDiskJobs];
m_cacheStatus.averageJobTime = p->values[m_metricIndices.disk.diskJobTime];
emit statsUpdated();
}
#else
void Session::updateStats()
{
libt::session_status ss = m_nativeSession->status();
m_status.hasIncomingConnections = ss.has_incoming_connections;
m_status.payloadDownloadRate = ss.payload_download_rate;
m_status.payloadUploadRate = ss.payload_upload_rate;
m_status.downloadRate = ss.download_rate;
m_status.uploadRate = ss.upload_rate;
m_status.ipOverheadDownloadRate = ss.ip_overhead_download_rate;
m_status.ipOverheadUploadRate = ss.ip_overhead_upload_rate;
m_status.dhtDownloadRate = ss.dht_download_rate;
m_status.dhtUploadRate = ss.dht_upload_rate;
m_status.trackerDownloadRate = ss.tracker_download_rate;
m_status.trackerUploadRate = ss.tracker_upload_rate;
m_status.totalDownload = ss.total_download;
m_status.totalUpload = ss.total_upload;
m_status.totalPayloadDownload = ss.total_payload_download;
m_status.totalPayloadUpload = ss.total_payload_upload;
m_status.totalWasted = ss.total_redundant_bytes + ss.total_failed_bytes;
m_status.diskReadQueue = ss.disk_read_queue;
m_status.diskWriteQueue = ss.disk_write_queue;
m_status.dhtNodes = ss.dht_nodes;
m_status.peersCount = ss.num_peers;
libt::cache_status cs = m_nativeSession->get_cache_status();
m_cacheStatus.totalUsedBuffers = cs.total_used_buffers;
m_cacheStatus.readRatio = cs.blocks_read > 0
? static_cast<qreal>(cs.blocks_read_hit) / cs.blocks_read
: -1;
m_cacheStatus.jobQueueLength = cs.job_queue_length;
m_cacheStatus.averageJobTime = cs.average_job_time;
m_cacheStatus.queuedBytes = cs.queued_bytes; // it seems that it is constantly equal to zero
emit statsUpdated();
}
#endif
void Session::handleStateUpdateAlert(libt::state_update_alert *p)
{
#if LIBTORRENT_VERSION_NUM < 10100
updateStats();
#endif
foreach (const libt::torrent_status &status, p->status) {
TorrentHandle *const torrent = m_torrents.value(status.info_hash);
if (torrent)