From bb4c7f0298cc7ee1e104e7932a85060cfc76e361 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 20 Mar 2023 10:56:18 +0100 Subject: [PATCH] Run UPnP discovery in the background --- .github/workflows/c-cpp.yml | 11 ++++---- .github/workflows/test-sync.yml | 6 ++--- src/p2pool.cpp | 12 +++++++++ src/util.cpp | 47 ++++++++++++++++++++++++++++++--- src/util.h | 2 ++ 5 files changed, 66 insertions(+), 12 deletions(-) diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index 71c3c94..81f22f0 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -251,10 +251,9 @@ jobs: strategy: matrix: config: - - {vs: Visual Studio 16 2019, os: 2019, msbuild: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin\\amd64\\", rx: "ON"} - - {vs: Visual Studio 16 2019, os: 2019, msbuild: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin\\amd64\\", rx: "OFF"} - #- {vs: Visual Studio 17 2022, os: 2022, msbuild: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\Msbuild\\Current\\Bin\\amd64\\", rx: "ON"} - #- {vs: Visual Studio 17 2022, os: 2022, msbuild: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\Msbuild\\Current\\Bin\\amd64\\", rx: "OFF"} + - {vs: Visual Studio 16 2019, os: 2019, msbuild: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin\\amd64\\", rx: "ON", upnp: "ON"} + - {vs: Visual Studio 16 2019, os: 2019, msbuild: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin\\amd64\\", rx: "OFF", upnp: "ON"} + - {vs: Visual Studio 16 2019, os: 2019, msbuild: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin\\amd64\\", rx: "OFF", upnp: "OFF"} steps: - name: Checkout repository @@ -269,7 +268,7 @@ jobs: run: | mkdir build cd build - cmake .. -G "${{ matrix.config.vs }}" -DWITH_RANDOMX=${{ matrix.config.rx }} + cmake .. -G "${{ matrix.config.vs }}" -DWITH_RANDOMX=${{ matrix.config.rx }} -DWITH_UPNP=${{ matrix.config.upnp }} & "${{ matrix.config.msbuild }}msbuild" /m /p:Configuration=Release p2pool.vcxproj - name: Build tests @@ -288,7 +287,7 @@ jobs: - name: Archive binary uses: actions/upload-artifact@v3 with: - name: p2pool-msbuild-${{ matrix.config.os }}-randomx-${{ matrix.config.rx }}.exe + name: p2pool-msbuild-${{ matrix.config.os }}-randomx-${{ matrix.config.rx }}-upnp-${{ matrix.config.upnp }}.exe path: build/Release/p2pool.exe build-macos: diff --git a/.github/workflows/test-sync.yml b/.github/workflows/test-sync.yml index f8ffc4e..03f30ea 100644 --- a/.github/workflows/test-sync.yml +++ b/.github/workflows/test-sync.yml @@ -24,7 +24,7 @@ jobs: run: | mkdir build cd build - cmake .. -DWITH_UPNP=OFF -DDEV_TEST_SYNC=ON -DCMAKE_C_COMPILER=gcc-12 -DCMAKE_CXX_COMPILER=g++-12 + cmake .. -DDEV_TEST_SYNC=ON -DCMAKE_C_COMPILER=gcc-12 -DCMAKE_CXX_COMPILER=g++-12 make -j$(nproc) - name: Run p2pool @@ -60,7 +60,7 @@ jobs: run: | mkdir build cd build - cmake .. -DWITH_UPNP=OFF -DDEV_TEST_SYNC=ON + cmake .. -DDEV_TEST_SYNC=ON make -j3 - name: Run p2pool @@ -96,7 +96,7 @@ jobs: run: | mkdir build cd build - cmake .. -G "Visual Studio 17 2022" -DWITH_UPNP=OFF -DDEV_TEST_SYNC=ON + cmake .. -G "Visual Studio 17 2022" -DDEV_TEST_SYNC=ON & "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\Msbuild\\Current\\Bin\\amd64\\msbuild" /m /p:Configuration=Debug p2pool.vcxproj - name: Run p2pool diff --git a/src/p2pool.cpp b/src/p2pool.cpp index e4b2351..acabc25 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -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); diff --git a/src/util.cpp b/src/util.cpp index d30cfe1..569d88f 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -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; diff --git a/src/util.h b/src/util.h index 6eb15b4..aa274b6 100644 --- a/src/util.h +++ b/src/util.h @@ -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