mirror of https://github.com/status-im/nim-eth.git
Windows: a new thread is created for the signal handler
This commit is contained in:
parent
12f2a87de3
commit
ecc1a995aa
|
@ -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,6 +58,7 @@ 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."
|
||||||
|
if not quiet:
|
||||||
debug "UPnP", msg
|
debug "UPnP", msg
|
||||||
if canContinue:
|
if canContinue:
|
||||||
let ires = upnp.externalIPAddress()
|
let ires = upnp.externalIPAddress()
|
||||||
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue