Skip to content

Commit

Permalink
feat: the transport layers of udp and tcp are optional features
Browse files Browse the repository at this point in the history
  • Loading branch information
mycrl committed Jan 4, 2025
1 parent 61d8a72 commit f085e4e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
3 changes: 3 additions & 0 deletions turn-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ default-features = false
features = ["json", "rustls-tls", "http2", "gzip"]

[features]
default = ["udp"]
udp = []
tcp = []
hooks = []
api = []
mimalloc = []
Expand Down
29 changes: 22 additions & 7 deletions turn-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::net::SocketAddr;

use turn::{Observer, Service};

#[allow(unused)]
struct ServerStartOptions<T> {
bind: SocketAddr,
external: SocketAddr,
Expand All @@ -16,12 +17,14 @@ struct ServerStartOptions<T> {
statistics: Statistics,
}

#[allow(unused)]
trait Server {
async fn start<T>(options: ServerStartOptions<T>) -> Result<(), anyhow::Error>
where
T: Clone + Observer + 'static;
}

#[cfg(feature = "udp")]
mod udp {
use super::{Server as ServerExt, ServerStartOptions};
use crate::statistics::Stats;
Expand Down Expand Up @@ -151,11 +154,18 @@ mod udp {
log::error!("udp server close: interface={:?}", local_addr);
});

log::info!(
"turn server listening: bind={}, external={}, transport=UDP",
bind,
external,
);

Ok(())
}
}
}

#[cfg(feature = "tcp")]
mod tcp {
use super::{Server as ServerExt, ServerStartOptions};
use crate::statistics::Stats;
Expand Down Expand Up @@ -440,6 +450,12 @@ mod tcp {
log::error!("tcp server close: interface={:?}", local_addr);
});

log::info!(
"turn server listening: bind={}, external={}, transport=TCP",
bind,
external,
);

Ok(())
}
}
Expand All @@ -453,6 +469,7 @@ pub async fn start<T>(config: &Config, statistics: &Statistics, service: &Servic
where
T: Clone + Observer + 'static,
{
#[allow(unused)]
use crate::config::Transport;

let router = Router::default();
Expand All @@ -462,6 +479,7 @@ where
bind,
} in config.turn.interfaces.iter().cloned()
{
#[allow(unused)]
let options = ServerStartOptions {
statistics: statistics.clone(),
service: service.clone(),
Expand All @@ -471,16 +489,13 @@ where
};

match transport {
#[cfg(feature = "udp")]
Transport::UDP => udp::Server::start(options).await?,
#[cfg(feature = "tcp")]
Transport::TCP => tcp::Server::start(options).await?,
#[allow(unreachable_patterns)]
_ => (),
};

log::info!(
"turn server listening: bind={}, external={}, transport={:?}",
bind,
external,
transport,
);
}

Ok(())
Expand Down

0 comments on commit f085e4e

Please sign in to comment.