mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-10 15:32:48 -07:00
Revamp implementation of port forwarder
This commit is contained in:
parent
1f3f96f7aa
commit
3563bad5fc
4 changed files with 108 additions and 80 deletions
|
@ -577,7 +577,7 @@ SessionImpl::SessionImpl(QObject *parent)
|
|||
loadStatistics();
|
||||
|
||||
// initialize PortForwarder instance
|
||||
new PortForwarderImpl(m_nativeSession);
|
||||
new PortForwarderImpl(this);
|
||||
|
||||
// start embedded tracker
|
||||
enableTracker(isTrackerEnabled());
|
||||
|
@ -2822,6 +2822,78 @@ void SessionImpl::findIncompleteFiles(const TorrentInfo &torrentInfo, const Path
|
|||
});
|
||||
}
|
||||
|
||||
void SessionImpl::enablePortMapping()
|
||||
{
|
||||
invokeAsync([this]
|
||||
{
|
||||
if (m_isPortMappingEnabled)
|
||||
return;
|
||||
|
||||
lt::settings_pack settingsPack;
|
||||
settingsPack.set_bool(lt::settings_pack::enable_upnp, true);
|
||||
settingsPack.set_bool(lt::settings_pack::enable_natpmp, true);
|
||||
m_nativeSession->apply_settings(std::move(settingsPack));
|
||||
|
||||
m_isPortMappingEnabled = true;
|
||||
|
||||
LogMsg(tr("UPnP/NAT-PMP support: ON"), Log::INFO);
|
||||
});
|
||||
}
|
||||
|
||||
void SessionImpl::disablePortMapping()
|
||||
{
|
||||
invokeAsync([this]
|
||||
{
|
||||
if (!m_isPortMappingEnabled)
|
||||
return;
|
||||
|
||||
lt::settings_pack settingsPack;
|
||||
settingsPack.set_bool(lt::settings_pack::enable_upnp, false);
|
||||
settingsPack.set_bool(lt::settings_pack::enable_natpmp, false);
|
||||
m_nativeSession->apply_settings(std::move(settingsPack));
|
||||
|
||||
m_mappedPorts.clear();
|
||||
m_isPortMappingEnabled = false;
|
||||
|
||||
LogMsg(tr("UPnP/NAT-PMP support: OFF"), Log::INFO);
|
||||
});
|
||||
}
|
||||
|
||||
void SessionImpl::addMappedPorts(const QSet<quint16> &ports)
|
||||
{
|
||||
invokeAsync([this, ports]
|
||||
{
|
||||
if (!m_isPortMappingEnabled)
|
||||
return;
|
||||
|
||||
for (const quint16 port : ports)
|
||||
{
|
||||
if (!m_mappedPorts.contains(port))
|
||||
m_mappedPorts.insert(port, m_nativeSession->add_port_mapping(lt::session::tcp, port, port));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void SessionImpl::removeMappedPorts(const QSet<quint16> &ports)
|
||||
{
|
||||
invokeAsync([this, ports]
|
||||
{
|
||||
if (!m_isPortMappingEnabled)
|
||||
return;
|
||||
|
||||
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_nativeSession->delete_port_mapping(handle);
|
||||
|
||||
return true;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void SessionImpl::invokeAsync(std::function<void ()> func)
|
||||
{
|
||||
m_asyncWorker->start(std::move(func));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue