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;
|
||||
|
|
86
src/net.cpp
86
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,35 +1511,29 @@ 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,
|
||||
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
|
||||
/* miniupnpc 1.5 */
|
||||
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
|
||||
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
|
||||
#else
|
||||
/* miniupnpc 1.6 */
|
||||
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
|
||||
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
|
||||
/* miniupnpc 1.6 */
|
||||
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
|
||||
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
|
||||
#endif
|
||||
|
||||
if(r!=UPNPCOMMAND_SUCCESS)
|
||||
LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
|
||||
port, port, lanaddr, r, strupnperror(r));
|
||||
else
|
||||
LogPrintf("UPnP Port Mapping successful.\n");
|
||||
if(r!=UPNPCOMMAND_SUCCESS)
|
||||
LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
|
||||
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;
|
||||
}
|
||||
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);
|
||||
} 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 (fUseUPnP)
|
||||
{
|
||||
if (upnp_thread) {
|
||||
upnp_thread->interrupt();
|
||||
upnp_thread->join();
|
||||
}
|
||||
upnp_thread.reset(new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
|
||||
if (!g_upnp_thread.joinable()) {
|
||||
assert(!g_upnp_interrupt);
|
||||
g_upnp_thread = std::thread((std::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
|
||||
}
|
||||
else if (upnp_thread) {
|
||||
upnp_thread->interrupt();
|
||||
upnp_thread->join();
|
||||
upnp_thread.reset();
|
||||
}
|
||||
|
||||
void InterruptMapPort()
|
||||
{
|
||||
if(g_upnp_thread.joinable()) {
|
||||
g_upnp_interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
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