boost: drop boost threads for upnp
This commit is contained in:
parent
0277173b1d
commit
f26866b9ca
4 changed files with 61 additions and 43 deletions
|
@ -165,6 +165,7 @@ void Interrupt()
|
|||
InterruptRPC();
|
||||
InterruptREST();
|
||||
InterruptTorControl();
|
||||
InterruptMapPort();
|
||||
if (g_connman)
|
||||
g_connman->Interrupt();
|
||||
}
|
||||
|
@ -191,7 +192,7 @@ void Shutdown()
|
|||
#ifdef ENABLE_WALLET
|
||||
FlushWallets();
|
||||
#endif
|
||||
MapPort(false);
|
||||
StopMapPort();
|
||||
|
||||
// Because these depend on each-other, we make sure that neither can be
|
||||
// using the other before destroying them.
|
||||
|
@ -1669,7 +1670,9 @@ bool AppInitMain()
|
|||
Discover(threadGroup);
|
||||
|
||||
// Map ports with UPnP
|
||||
MapPort(gArgs.GetBoolArg("-upnp", DEFAULT_UPNP));
|
||||
if (gArgs.GetBoolArg("-upnp", DEFAULT_UPNP)) {
|
||||
StartMapPort();
|
||||
}
|
||||
|
||||
CConnman::Options connOptions;
|
||||
connOptions.nLocalServices = nLocalServices;
|
||||
|
|
50
src/net.cpp
50
src/net.cpp
|
@ -1459,6 +1459,8 @@ void CConnman::WakeMessageHandler()
|
|||
|
||||
|
||||
#ifdef USE_UPNP
|
||||
static CThreadInterrupt g_upnp_interrupt;
|
||||
static std::thread g_upnp_thread;
|
||||
void ThreadMapPort()
|
||||
{
|
||||
std::string port = strprintf("%u", GetListenPort());
|
||||
|
@ -1509,8 +1511,7 @@ void ThreadMapPort()
|
|||
|
||||
std::string strDesc = "Bitcoin " + FormatFullVersion();
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
do {
|
||||
#ifndef UPNPDISCOVER_SUCCESS
|
||||
/* miniupnpc 1.5 */
|
||||
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
|
||||
|
@ -1526,18 +1527,13 @@ void ThreadMapPort()
|
|||
port, port, lanaddr, r, strupnperror(r));
|
||||
else
|
||||
LogPrintf("UPnP Port Mapping successful.\n");
|
||||
}
|
||||
while(g_upnp_interrupt.sleep_for(std::chrono::minutes(20)));
|
||||
|
||||
MilliSleep(20*60*1000); // Refresh every 20 minutes
|
||||
}
|
||||
}
|
||||
catch (const boost::thread_interrupted&)
|
||||
{
|
||||
r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
|
||||
LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
|
||||
freeUPNPDevlist(devlist); devlist = nullptr;
|
||||
FreeUPNPUrls(&urls);
|
||||
throw;
|
||||
}
|
||||
} else {
|
||||
LogPrintf("No valid UPnP IGDs found\n");
|
||||
freeUPNPDevlist(devlist); devlist = nullptr;
|
||||
|
@ -1546,27 +1542,39 @@ void ThreadMapPort()
|
|||
}
|
||||
}
|
||||
|
||||
void MapPort(bool fUseUPnP)
|
||||
void StartMapPort()
|
||||
{
|
||||
static std::unique_ptr<boost::thread> upnp_thread;
|
||||
if (!g_upnp_thread.joinable()) {
|
||||
assert(!g_upnp_interrupt);
|
||||
g_upnp_thread = std::thread((std::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
|
||||
}
|
||||
}
|
||||
|
||||
if (fUseUPnP)
|
||||
void InterruptMapPort()
|
||||
{
|
||||
if (upnp_thread) {
|
||||
upnp_thread->interrupt();
|
||||
upnp_thread->join();
|
||||
if(g_upnp_thread.joinable()) {
|
||||
g_upnp_interrupt();
|
||||
}
|
||||
upnp_thread.reset(new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
|
||||
}
|
||||
else if (upnp_thread) {
|
||||
upnp_thread->interrupt();
|
||||
upnp_thread->join();
|
||||
upnp_thread.reset();
|
||||
|
||||
void StopMapPort()
|
||||
{
|
||||
if(g_upnp_thread.joinable()) {
|
||||
g_upnp_thread.join();
|
||||
g_upnp_interrupt.reset();
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
void MapPort(bool)
|
||||
void StartMapPort()
|
||||
{
|
||||
// Intentionally left blank.
|
||||
}
|
||||
void InterruptMapPort()
|
||||
{
|
||||
// Intentionally left blank.
|
||||
}
|
||||
void StopMapPort()
|
||||
{
|
||||
// Intentionally left blank.
|
||||
}
|
||||
|
|
|
@ -442,7 +442,9 @@ private:
|
|||
};
|
||||
extern std::unique_ptr<CConnman> g_connman;
|
||||
void Discover(boost::thread_group& threadGroup);
|
||||
void MapPort(bool fUseUPnP);
|
||||
void StartMapPort();
|
||||
void InterruptMapPort();
|
||||
void StopMapPort();
|
||||
unsigned short GetListenPort();
|
||||
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
|
||||
|
||||
|
|
|
@ -315,7 +315,12 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
|
|||
break;
|
||||
case MapPortUPnP: // core option - can be changed on-the-fly
|
||||
settings.setValue("fUseUPnP", value.toBool());
|
||||
MapPort(value.toBool());
|
||||
if (value.toBool()) {
|
||||
StartMapPort();
|
||||
} else {
|
||||
InterruptMapPort();
|
||||
StopMapPort();
|
||||
}
|
||||
break;
|
||||
case MinimizeOnClose:
|
||||
fMinimizeOnClose = value.toBool();
|
||||
|
|
Loading…
Reference in a new issue