Skip to content

Commit

Permalink
Use custom job queue
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Oct 27, 2024
1 parent 608f50c commit ca0d9cb
Show file tree
Hide file tree
Showing 14 changed files with 631 additions and 333 deletions.
3 changes: 3 additions & 0 deletions src/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ add_library(qbt_base STATIC
bittorrent/torrentdescriptor.h
bittorrent/torrentimpl.h
bittorrent/torrentinfo.h
bittorrent/torrentoperatingmode.h
bittorrent/tracker.h
bittorrent/trackerentry.h
bittorrent/trackerentrystatus.h
concepts/explicitlyconvertibleto.h
concepts/stringable.h
digest32.h
exceptions.h
executor.h
global.h
http/connection.h
http/httperror.h
Expand Down Expand Up @@ -162,6 +164,7 @@ add_library(qbt_base STATIC
bittorrent/trackerentry.cpp
bittorrent/trackerentrystatus.cpp
exceptions.cpp
executor.cpp
http/connection.cpp
http/httperror.cpp
http/requestparser.cpp
Expand Down
1 change: 0 additions & 1 deletion src/base/bittorrent/dbresumedatastorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <QReadWriteLock>

#include "base/pathfwd.h"
#include "base/utils/thread.h"
#include "resumedatastorage.h"

namespace BitTorrent
Expand Down
119 changes: 81 additions & 38 deletions src/base/bittorrent/sessionbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
#include <libtorrent/session.hpp>

#include "base/algorithm.h"
#include "base/executor.h"
#include "nativesessionextension.h"
#include "torrentbackend.h"

BitTorrent::SessionBackend::SessionBackend(lt::session *ltSession, QObject *parent)
BitTorrent::SessionBackend::SessionBackend(Executor *executor, lt::session *ltSession, QObject *parent)
: QObject(parent)
, m_executor {executor}
, m_ltSession {ltSession}
{
auto nativeSessionExtension = std::make_shared<NativeSessionExtension>();
Expand All @@ -48,7 +50,7 @@ BitTorrent::SessionBackend::SessionBackend(lt::session *ltSession, QObject *pare

BitTorrent::TorrentBackend *BitTorrent::SessionBackend::createTorrentBackend(lt::torrent_handle ltTorrentHandle) const
{
auto *torrentBackend = new TorrentBackend(m_ltSession, std::move(ltTorrentHandle));
auto *torrentBackend = new TorrentBackend(m_executor, m_ltSession, std::move(ltTorrentHandle));
torrentBackend->moveToThread(thread());
return torrentBackend;
}
Expand Down Expand Up @@ -77,40 +79,62 @@ lt::session_proxy *BitTorrent::SessionBackend::abort()

void BitTorrent::SessionBackend::pause()
{
m_ltSession->pause();
m_executor->addJob([this]
{
m_ltSession->pause();
});
}

void BitTorrent::SessionBackend::resume()
{
m_ltSession->resume();
m_executor->addJob([this]
{
m_ltSession->resume();
});
}

void BitTorrent::SessionBackend::addTorrentAsync(lt::add_torrent_params ltAddTorrentParams)
{
m_ltSession->async_add_torrent(std::move(ltAddTorrentParams));
m_executor->addJob([this, ltAddTorrentParams = std::move(ltAddTorrentParams)]() mutable
{
m_ltSession->async_add_torrent(std::move(ltAddTorrentParams));
});
}

void BitTorrent::SessionBackend::removeTorrent(const lt::torrent_handle &ltTorrentHandle)
void BitTorrent::SessionBackend::removeTorrent(lt::torrent_handle ltTorrentHandle)
{
m_ltSession->remove_torrent(ltTorrentHandle, lt::session::delete_partfile);
m_executor->addJob([this, ltTorrentHandle = std::move(ltTorrentHandle)]() mutable
{
m_ltSession->remove_torrent(std::move(ltTorrentHandle), lt::session::delete_partfile);
});
}

void BitTorrent::SessionBackend::blockIP(const boost::asio::ip::address &addr)
void BitTorrent::SessionBackend::blockIP(boost::asio::ip::address addr)
{
lt::ip_filter filter = m_ltSession->get_ip_filter();
filter.add_rule(addr, addr, lt::ip_filter::blocked);
m_ltSession->set_ip_filter(std::move(filter));
m_executor->addJob([this, addr = std::move(addr)]
{
lt::ip_filter filter = m_ltSession->get_ip_filter();
filter.add_rule(addr, addr, lt::ip_filter::blocked);
m_ltSession->set_ip_filter(std::move(filter));
});
}

void BitTorrent::SessionBackend::setIPFilter(const libtorrent::ip_filter &ipFilter)
void BitTorrent::SessionBackend::setIPFilter(lt::ip_filter ipFilter)
{
m_ltSession->set_ip_filter(ipFilter);
m_executor->addJob([this, ipFilter = std::move(ipFilter)]() mutable
{
m_ltSession->set_ip_filter(std::move(ipFilter));
});
}

void BitTorrent::SessionBackend::setPeerFilters(const lt::ip_filter &classFilter, const lt::peer_class_type_filter &classTypeFilter)
void BitTorrent::SessionBackend::setPeerFilters(lt::ip_filter classFilter, lt::peer_class_type_filter classTypeFilter)
{
m_ltSession->set_peer_class_filter(classFilter);
m_ltSession->set_peer_class_type_filter(classTypeFilter);
m_executor->addJob([this, classFilter = std::move(classFilter)
, classTypeFilter = std::move(classTypeFilter)]() mutable
{
m_ltSession->set_peer_class_filter(std::move(classFilter));
m_ltSession->set_peer_class_type_filter(std::move(classTypeFilter));
});
}

void BitTorrent::SessionBackend::setPortMappingEnabled(const bool enabled)
Expand All @@ -119,56 +143,75 @@ void BitTorrent::SessionBackend::setPortMappingEnabled(const bool enabled)
return;

m_isPortMappingEnabled = enabled;
if (!m_isPortMappingEnabled)
m_mappedPorts.clear();

lt::settings_pack settingsPack;
settingsPack.set_bool(lt::settings_pack::enable_upnp, enabled);
settingsPack.set_bool(lt::settings_pack::enable_natpmp, enabled);
m_ltSession->apply_settings(std::move(settingsPack));
m_executor->addJob([this, enabled]
{
if (!enabled)
m_mappedPorts.clear();

lt::settings_pack settingsPack;
settingsPack.set_bool(lt::settings_pack::enable_upnp, enabled);
settingsPack.set_bool(lt::settings_pack::enable_natpmp, enabled);
m_ltSession->apply_settings(std::move(settingsPack));
});
}

void BitTorrent::SessionBackend::addMappedPorts(const QSet<quint16> &ports)
void BitTorrent::SessionBackend::addMappedPorts(QSet<quint16> ports)
{
if (!m_isPortMappingEnabled)
return;

for (const quint16 port : ports)
m_executor->addJob([this, ports = std::move(ports)]
{
if (!m_mappedPorts.contains(port))
m_mappedPorts.insert(port, m_ltSession->add_port_mapping(lt::session::tcp, port, port));
}
for (const quint16 port : ports)
{
if (!m_mappedPorts.contains(port))
m_mappedPorts.insert(port, m_ltSession->add_port_mapping(lt::session::tcp, port, port));
}
});
}

void BitTorrent::SessionBackend::removeMappedPorts(const QSet<quint16> &ports)
void BitTorrent::SessionBackend::removeMappedPorts(QSet<quint16> ports)
{
if (!m_isPortMappingEnabled)
return;

Algorithm::removeIf(m_mappedPorts
, [this, ports](const quint16 port, const std::vector<lt::port_mapping_t> &handles)
m_executor->addJob([this, ports = std::move(ports)]
{
if (!ports.contains(port))
return false;
Algorithm::removeIf(m_mappedPorts
, [this, &ports](const quint16 port, const std::vector<lt::port_mapping_t> &handles)
{
if (!ports.contains(port))
return false;

for (const lt::port_mapping_t &handle : handles)
m_ltSession->delete_port_mapping(handle);
for (const lt::port_mapping_t &handle : handles)
m_ltSession->delete_port_mapping(handle);

return true;
return true;
});
});
}

void BitTorrent::SessionBackend::applySettings(lt::settings_pack settingsPack)
{
m_ltSession->apply_settings(std::move(settingsPack));
m_executor->addJob([this, settingsPack = std::move(settingsPack)]() mutable
{
m_ltSession->apply_settings(std::move(settingsPack));
});
}

void BitTorrent::SessionBackend::postTorrentUpdates(const lt::status_flags_t flags)
{
m_ltSession->post_torrent_updates(flags);
m_executor->addJob([this, flags]
{
m_ltSession->post_torrent_updates(flags);
});
}

void BitTorrent::SessionBackend::postSessionStats()
{
m_ltSession->post_session_stats();
m_executor->addJob([this]
{
m_ltSession->post_session_stats();
});
}
16 changes: 9 additions & 7 deletions src/base/bittorrent/sessionbackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <QPromise>
#include <QSet>

class Executor;
class NativeSessionExtension;

namespace BitTorrent
Expand All @@ -53,7 +54,7 @@ namespace BitTorrent
Q_DISABLE_COPY_MOVE(SessionBackend)

public:
SessionBackend(lt::session *ltSession, QObject *parent = nullptr);
SessionBackend(Executor *executor, lt::session *ltSession, QObject *parent = nullptr);

// Sync API
TorrentBackend *createTorrentBackend(lt::torrent_handle ltTorrentHandle) const;
Expand All @@ -65,13 +66,13 @@ namespace BitTorrent
void pause();
void resume();
void addTorrentAsync(lt::add_torrent_params ltAddTorrentParams);
void removeTorrent(const lt::torrent_handle &ltTorrentHandle);
void blockIP(const lt::address &addr);
void setIPFilter(const lt::ip_filter &ipFilter);
void setPeerFilters(const lt::ip_filter &classFilter, const lt::peer_class_type_filter &classTypeFilter);
void removeTorrent(lt::torrent_handle ltTorrentHandle);
void blockIP(boost::asio::ip::address addr);
void setIPFilter(lt::ip_filter ipFilter);
void setPeerFilters(lt::ip_filter classFilter, lt::peer_class_type_filter classTypeFilter);
void setPortMappingEnabled(bool enabled);
void addMappedPorts(const QSet<quint16> &ports);
void removeMappedPorts(const QSet<quint16> &ports);
void addMappedPorts(QSet<quint16> ports);
void removeMappedPorts(QSet<quint16> ports);
void applySettings(lt::settings_pack settingsPack);
void postTorrentUpdates(lt::status_flags_t flags = lt::status_flags_t::all());
void postSessionStats();
Expand All @@ -80,6 +81,7 @@ namespace BitTorrent
void alertsReady();

private:
Executor *m_executor = nullptr;
lt::session *m_ltSession = nullptr;
NativeSessionExtension *m_nativeSessionExtension = nullptr;

Expand Down
Loading

0 comments on commit ca0d9cb

Please sign in to comment.