Browse Source

Avoid redundant lookups in port forwarder

adaptive-webui-19844
Chocobo1 2 years ago
parent
commit
1a782a9362
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 49
      src/base/bittorrent/portforwarderimpl.cpp
  2. 2
      src/base/bittorrent/portforwarderimpl.h

49
src/base/bittorrent/portforwarderimpl.cpp

@ -30,8 +30,6 @@
#include <libtorrent/session.hpp> #include <libtorrent/session.hpp>
#include <QDebug>
#include "base/logger.h" #include "base/logger.h"
PortForwarderImpl::PortForwarderImpl(lt::session *provider, QObject *parent) PortForwarderImpl::PortForwarderImpl(lt::session *provider, QObject *parent)
@ -55,61 +53,70 @@ bool PortForwarderImpl::isEnabled() const
void PortForwarderImpl::setEnabled(const bool enabled) void PortForwarderImpl::setEnabled(const bool enabled)
{ {
if (m_storeActive != enabled) if (m_storeActive == enabled)
{ return;
if (enabled) if (enabled)
start(); start();
else else
stop(); stop();
m_storeActive = enabled; m_storeActive = enabled;
} }
}
void PortForwarderImpl::addPort(const quint16 port) void PortForwarderImpl::addPort(const quint16 port)
{ {
if (!m_mappedPorts.contains(port)) if (m_mappedPorts.contains(port))
{ return;
m_mappedPorts.insert(port, {});
if (isEnabled()) if (isEnabled())
m_mappedPorts[port] = {m_provider->add_port_mapping(lt::session::tcp, port, port)}; m_mappedPorts.insert(port, m_provider->add_port_mapping(lt::session::tcp, port, port));
} else
m_mappedPorts.insert(port, {});
} }
void PortForwarderImpl::deletePort(const quint16 port) void PortForwarderImpl::deletePort(const quint16 port)
{ {
if (m_mappedPorts.contains(port)) const auto iter = m_mappedPorts.find(port);
{ if (iter == m_mappedPorts.end())
return;
if (isEnabled()) if (isEnabled())
{ {
for (const lt::port_mapping_t &portMapping : m_mappedPorts[port]) for (const lt::port_mapping_t &portMapping : *iter)
m_provider->delete_port_mapping(portMapping); m_provider->delete_port_mapping(portMapping);
} }
m_mappedPorts.remove(port);
} m_mappedPorts.erase(iter);
} }
void PortForwarderImpl::start() void PortForwarderImpl::start()
{ {
qDebug("Enabling UPnP / NAT-PMP");
lt::settings_pack settingsPack = m_provider->get_settings(); lt::settings_pack settingsPack = m_provider->get_settings();
settingsPack.set_bool(lt::settings_pack::enable_upnp, true); settingsPack.set_bool(lt::settings_pack::enable_upnp, true);
settingsPack.set_bool(lt::settings_pack::enable_natpmp, true); settingsPack.set_bool(lt::settings_pack::enable_natpmp, true);
m_provider->apply_settings(settingsPack); m_provider->apply_settings(settingsPack);
for (auto i = m_mappedPorts.begin(); i != m_mappedPorts.end(); ++i)
for (auto iter = m_mappedPorts.begin(); iter != m_mappedPorts.end(); ++iter)
{ {
// quint16 port = i.key(); Q_ASSERT(iter.value().empty());
i.value() = {m_provider->add_port_mapping(lt::session::tcp, i.key(), i.key())};
const quint16 port = iter.key();
iter.value() = m_provider->add_port_mapping(lt::session::tcp, port, port);
} }
LogMsg(tr("UPnP/NAT-PMP support: ON"), Log::INFO); LogMsg(tr("UPnP/NAT-PMP support: ON"), Log::INFO);
} }
void PortForwarderImpl::stop() void PortForwarderImpl::stop()
{ {
qDebug("Disabling UPnP / NAT-PMP");
lt::settings_pack settingsPack = m_provider->get_settings(); lt::settings_pack settingsPack = m_provider->get_settings();
settingsPack.set_bool(lt::settings_pack::enable_upnp, false); settingsPack.set_bool(lt::settings_pack::enable_upnp, false);
settingsPack.set_bool(lt::settings_pack::enable_natpmp, false); settingsPack.set_bool(lt::settings_pack::enable_natpmp, false);
m_provider->apply_settings(settingsPack); m_provider->apply_settings(settingsPack);
// don't clear m_mappedPorts so a later `start()` call can restore the port forwarding
for (auto iter = m_mappedPorts.begin(); iter != m_mappedPorts.end(); ++iter)
iter.value().clear();
LogMsg(tr("UPnP/NAT-PMP support: OFF"), Log::INFO); LogMsg(tr("UPnP/NAT-PMP support: OFF"), Log::INFO);
} }

2
src/base/bittorrent/portforwarderimpl.h

@ -58,6 +58,6 @@ private:
void stop(); void stop();
CachedSettingValue<bool> m_storeActive; CachedSettingValue<bool> m_storeActive;
lt::session *m_provider = nullptr; lt::session *const m_provider = nullptr;
QHash<quint16, std::vector<lt::port_mapping_t>> m_mappedPorts; QHash<quint16, std::vector<lt::port_mapping_t>> m_mappedPorts;
}; };

Loading…
Cancel
Save