Run UPnP discovery in the background

This commit is contained in:
SChernykh
2023-03-20 10:56:18 +01:00
parent a7db46d69b
commit bb4c7f0298
5 changed files with 66 additions and 12 deletions
+12
View File
@@ -60,6 +60,12 @@ p2pool::p2pool(int argc, char* argv[])
{
LOGINFO(1, log::LightCyan() << VERSION);
#ifdef WITH_UPNP
if (m_params->m_upnp) {
init_upnp();
}
#endif
if (!m_params->m_wallet.valid()) {
LOGERR(1, "Invalid wallet address. Try \"p2pool --help\".");
throw std::exception();
@@ -189,6 +195,12 @@ p2pool::p2pool(int argc, char* argv[])
p2pool::~p2pool()
{
#ifdef WITH_UPNP
if (m_params->m_upnp) {
destroy_upnp();
}
#endif
uv_rwlock_destroy(&m_mainchainLock);
uv_rwlock_destroy(&m_minerDataLock);
uv_mutex_destroy(&m_foundBlocksLock);
+44 -3
View File
@@ -568,17 +568,58 @@ UV_LoopUserData* GetLoopUserData(uv_loop_t* loop, bool create)
#ifdef WITH_UPNP
static struct UPnP_Discover
{
UPnP_Discover() { devlist = upnpDiscover(1000, nullptr, nullptr, UPNP_LOCAL_PORT_ANY, 0, 2, &error); }
~UPnP_Discover() { freeUPNPDevlist(devlist); }
uv_mutex_t lock;
int error;
UPNPDev* devlist;
} upnp_discover;
void init_upnp()
{
uv_mutex_init_checked(&upnp_discover.lock);
uv_work_t* req = new uv_work_t{};
const int err = uv_queue_work(uv_default_loop_checked(), req,
[](uv_work_t* /*req*/)
{
BACKGROUND_JOB_START(init_upnp);
LOGINFO(1, "UPnP: Started scanning for UPnP IGD devices");
{
MutexLock lock(upnp_discover.lock);
upnp_discover.devlist = upnpDiscover(1000, nullptr, nullptr, UPNP_LOCAL_PORT_ANY, 0, 2, &upnp_discover.error);
}
LOGINFO(1, "UPnP: Finished scanning for UPnP IGD devices");
},
[](uv_work_t* req, int /*status*/)
{
delete req;
BACKGROUND_JOB_STOP(init_upnp);
}
);
if (err) {
LOGERR(0, "init_upnp: uv_queue_work failed, error " << uv_err_name(err));
delete req;
}
}
void destroy_upnp()
{
{
MutexLock lock(upnp_discover.lock);
freeUPNPDevlist(upnp_discover.devlist);
upnp_discover.devlist = nullptr;
}
uv_mutex_destroy(&upnp_discover.lock);
}
void add_portmapping(int external_port, int internal_port)
{
LOGINFO(1, "UPnP: trying to map WAN:" << external_port << " to LAN:" << internal_port);
MutexLock lock(upnp_discover.lock);
if (!upnp_discover.devlist) {
LOGWARN(1, "upnpDiscover: no UPnP IGD devices found, error " << upnp_discover.error);
return;
+2
View File
@@ -247,6 +247,8 @@ bool str_to_ip(bool is_v6, const char* ip, raw_ip& result);
bool is_localhost(const std::string& host);
#ifdef WITH_UPNP
void init_upnp();
void destroy_upnp();
void add_portmapping(int external_port, int internal_port);
#endif