Windows: a new thread is created for the signal handler

This commit is contained in:
Ștefan Talpalaru 2019-04-25 02:30:17 +02:00
parent 12f2a87de3
commit ecc1a995aa
No known key found for this signature in database
GPG Key ID: CBF7934204F1B6F9
1 changed files with 14 additions and 9 deletions

View File

@ -37,7 +37,7 @@ logScope:
## Also does threadvar initialisation. ## Also does threadvar initialisation.
## Must be called before redirectPorts() in each thread. ## Must be called before redirectPorts() in each thread.
proc getExternalIP*(natStrategy: NatStrategy): Option[IpAddress] = proc getExternalIP*(natStrategy: NatStrategy, quiet = false): Option[IpAddress] =
if natStrategy == NatAny or natStrategy == NatUpnp: if natStrategy == NatAny or natStrategy == NatUpnp:
upnp = newMiniupnp() upnp = newMiniupnp()
upnp.discoverDelay = UPNP_TIMEOUT upnp.discoverDelay = UPNP_TIMEOUT
@ -58,7 +58,8 @@ proc getExternalIP*(natStrategy: NatStrategy): Option[IpAddress] =
msg = "Internet Gateway Device found but it's not connected. Trying anyway." msg = "Internet Gateway Device found but it's not connected. Trying anyway."
of NotAnIGD: of NotAnIGD:
msg = "Some device found, but it's not recognised as an Internet Gateway Device. Trying anyway." msg = "Some device found, but it's not recognised as an Internet Gateway Device. Trying anyway."
debug "UPnP", msg if not quiet:
debug "UPnP", msg
if canContinue: if canContinue:
let ires = upnp.externalIPAddress() let ires = upnp.externalIPAddress()
if ires.isErr: if ires.isErr:
@ -162,7 +163,7 @@ proc repeatPortMapping(args: PortMappingArgs) {.thread.} =
# We can't use copies of Miniupnp and NatPmp objects in this thread, because they share # We can't use copies of Miniupnp and NatPmp objects in this thread, because they share
# C pointers with other instances that have already been garbage collected, so # C pointers with other instances that have already been garbage collected, so
# we use threadvars instead and initialise them again with getExternalIP(). # we use threadvars instead and initialise them again with getExternalIP().
let ipres = getExternalIP(strategy) let ipres = getExternalIP(strategy, quiet = true)
if ipres.isSome: if ipres.isSome:
externalIP = ipres.get() externalIP = ipres.get()
while true: while true:
@ -181,12 +182,16 @@ proc repeatPortMapping(args: PortMappingArgs) {.thread.} =
var mainThreadId = getThreadId() var mainThreadId = getThreadId()
proc stopNatThread() {.noconv.} = proc stopNatThread() {.noconv.} =
if getThreadId() == mainThreadId: # stop the thread
# stop the thread natCloseChan.send(true)
natCloseChan.send(true) natThread.joinThread()
natThread.joinThread() natCloseChan.close()
natCloseChan.close() # delete our port mappings
# delete our port mappings
# In Windows, a new thread is created for the signal handler, so we need to
# initialise our threadvars again.
let ipres = getExternalIP(strategy, quiet = true)
if ipres.isSome:
if strategy == NatUpnp: if strategy == NatUpnp:
for t in [(externalTcpPort, internalTcpPort, UPNPProtocol.TCP), (externalUdpPort, internalUdpPort, UPNPProtocol.UDP)]: for t in [(externalTcpPort, internalTcpPort, UPNPProtocol.TCP), (externalUdpPort, internalUdpPort, UPNPProtocol.UDP)]:
let let