33
44# # NAT port mapper backed by libplum (PCP / NAT-PMP / UPnP-IGD).
55# #
6- # # libplum owns a single process-wide client with its own internal thread that
7- # # discovers the gateway, selects a working protocol, and keeps mappings
8- # # refreshed on its own — so there is no per-mapping lease and no refresh loop
9- # # on our side. The C `plum_init`/`plum_cleanup` pair is a global singleton
10- # # (a second `plum_init` fails while a client is live), so this module
11- # # ref-counts init across every live `PlumMapper` and keeps the protocol filter
12- # # of the first mapper that initialized it.
13- # #
14- # # Precondition: every `PlumMapper` is created and closed from the same chronos
15- # # event-loop thread. The ref-count and active-filter globals below are plain
16- # # (unsynchronized) vars, so driving mappers from multiple threads would race
17- # # them and double-init libplum.
6+ # # `plum_init`/`plum_cleanup` is a process-global singleton, so this module
7+ # # ref-counts init across every live `PlumMapper` and keeps the first mapper's
8+ # # protocol filter. The ref-count/filter globals are unsynchronized: create and
9+ # # close every `PlumMapper` from the same chronos event-loop thread.
1810
1911{.push raises : [].}
2012
@@ -29,8 +21,7 @@ logScope:
2921 topics = " libp2p natservice plum"
3022
3123const
32- # Module-private fallbacks for PlumMapper.new()'s own default args; the public
33- # NAT timeout knobs live in natservice (DefaultDiscoveryTimeout/MappingTimeout).
24+ # Private defaults for new(); the public NAT knobs live in natservice.
3425 DefaultDiscoverTimeout = 10 .seconds
3526 DefaultMappingTimeout = 10 .seconds
3627
4031 PlumMapper * = ref object of PortMapper
4132 filter: ProtocolFilter
4233 closed: bool
43- mappings: Table [MappingKey , cint ]
44- # # (externalPort, proto) -> libplum mapping id, recorded on a successful
45- # # map() so unmap()/close() can find the opaque handle to destroy.
34+ mappings: Table [MappingKey , cint ] # # -> libplum mapping id, to destroy later
4635
4736var
4837 plumRefCount = 0
@@ -103,8 +92,7 @@ method map*(
10392 externalPort = externalPort.uint16 ,
10493 )
10594
106- # close() may have run during the await above and torn libplum down; drop the
107- # mapping we just created rather than recording an id for a dead handle.
95+ # close() may have torn libplum down during the await; drop the new mapping.
10896 if self.closed:
10997 destroyMapping (res.id)
11098 return err (" PlumMapper closed" )
0 commit comments