net: Add flags for port mapping protocols

This commit is contained in:
Hennadii Stepanov
2020-02-23 00:10:48 +02:00
parent 8b50d1b5bb
commit 4e91b1e24d
4 changed files with 37 additions and 15 deletions

View File

@@ -25,6 +25,7 @@
static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed");
#endif
#include <atomic>
#include <cassert>
#include <chrono>
#include <functional>
@@ -34,6 +35,7 @@ static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed"
#ifdef USE_UPNP
static CThreadInterrupt g_upnp_interrupt;
static std::thread g_upnp_thread;
static std::atomic_uint g_mapport_target_proto{MapPortProtoFlag::NONE};
using namespace std::chrono_literals;
static constexpr auto PORT_MAPPING_REANNOUNCE_PERIOD{20min};
@@ -117,7 +119,7 @@ static void ThreadMapPort()
} while (g_upnp_interrupt.sleep_for(PORT_MAPPING_RETRY_PERIOD));
}
void StartMapPort()
void StartThreadMapPort()
{
if (!g_upnp_thread.joinable()) {
assert(!g_upnp_interrupt);
@@ -125,6 +127,31 @@ void StartMapPort()
}
}
static void DispatchMapPort()
{
if (g_mapport_target_proto == MapPortProtoFlag::UPNP) {
StartThreadMapPort();
} else {
InterruptMapPort();
StopMapPort();
}
}
static void MapPortProtoSetEnabled(MapPortProtoFlag proto, bool enabled)
{
if (enabled) {
g_mapport_target_proto |= proto;
} else {
g_mapport_target_proto &= ~proto;
}
}
void StartMapPort(bool use_upnp)
{
MapPortProtoSetEnabled(MapPortProtoFlag::UPNP, use_upnp);
DispatchMapPort();
}
void InterruptMapPort()
{
if(g_upnp_thread.joinable()) {
@@ -141,7 +168,7 @@ void StopMapPort()
}
#else
void StartMapPort()
void StartMapPort(bool use_upnp)
{
// Intentionally left blank.
}