1use std::net::SocketAddrV4;
7
8use tokio::sync::watch;
9
10#[derive(Debug, Clone)]
19#[non_exhaustive]
20pub enum PortmapperConfig {
21 #[non_exhaustive]
25 Enabled {},
26 Disabled,
33}
34
35impl Default for PortmapperConfig {
36 fn default() -> Self {
37 PortmapperConfig::Enabled {}
38 }
39}
40
41pub(crate) fn create_client(config: &PortmapperConfig) -> Client {
42 match config {
43 #[cfg(all(not(wasm_browser), feature = "portmapper"))]
44 PortmapperConfig::Enabled {} => Client::Enabled(::portmapper::Client::default()),
45 _ => {
46 let (tx, rx) = watch::channel(None);
47 Client::Disabled { _tx: tx, rx }
48 }
49 }
50}
51
52#[derive(Debug)]
57pub(crate) enum Client {
58 #[cfg(all(not(wasm_browser), feature = "portmapper"))]
60 Enabled(::portmapper::Client),
61 Disabled {
63 _tx: watch::Sender<Option<SocketAddrV4>>,
64 rx: watch::Receiver<Option<SocketAddrV4>>,
65 },
66}
67
68impl Client {
69 pub(crate) fn procure_mapping(&self) {
70 match self {
71 #[cfg(all(not(wasm_browser), feature = "portmapper"))]
72 Client::Enabled(c) => c.procure_mapping(),
73 Client::Disabled { .. } => {}
74 }
75 }
76
77 pub(crate) fn update_local_port(&self, _port: std::num::NonZeroU16) {
78 match self {
79 #[cfg(all(not(wasm_browser), feature = "portmapper"))]
80 Client::Enabled(c) => c.update_local_port(_port),
81 Client::Disabled { .. } => {}
82 }
83 }
84
85 pub(crate) fn deactivate(&self) {
86 match self {
87 #[cfg(all(not(wasm_browser), feature = "portmapper"))]
88 Client::Enabled(c) => c.deactivate(),
89 Client::Disabled { .. } => {}
90 }
91 }
92
93 pub(crate) fn watch_external_address(&self) -> watch::Receiver<Option<SocketAddrV4>> {
94 match self {
95 #[cfg(all(not(wasm_browser), feature = "portmapper"))]
96 Client::Enabled(c) => c.watch_external_address(),
97 Client::Disabled { rx, .. } => rx.clone(),
98 }
99 }
100}