Skip to content

Commit caf8782

Browse files
cleanup socket creation wrt ip mode (#150)
1 parent e6f84a6 commit caf8782

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/handler/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,24 +228,17 @@ impl Handler {
228228
local_node_id: node_id,
229229
expected_responses: filter_expected_responses.clone(),
230230
ban_duration: config.ban_duration,
231+
ip_mode: config.ip_mode,
231232
};
232233

233234
// Attempt to bind to the socket before spinning up the send/recv tasks.
234-
let socket = Socket::new_socket(&socket_config.socket_addr, config.ip_mode).await?;
235+
let socket = Socket::new(socket_config).await?;
235236

236237
config
237238
.executor
238239
.clone()
239240
.expect("Executor must be present")
240241
.spawn(Box::pin(async move {
241-
let socket = match Socket::new(socket, socket_config) {
242-
Ok(v) => v,
243-
Err(e) => {
244-
error!("Could not bind UDP socket. {}", e);
245-
return;
246-
}
247-
};
248-
249242
let mut handler = Handler {
250243
request_retries: config.request_retries,
251244
node_id,

src/socket/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub use filter::{
2222
};
2323
pub use recv::InboundPacket;
2424
pub use send::OutboundPacket;
25+
2526
/// Convenience objects for setting up the recv handler.
2627
pub struct SocketConfig {
2728
/// The executor to spawn the tasks.
@@ -30,6 +31,8 @@ pub struct SocketConfig {
3031
pub socket_addr: SocketAddr,
3132
/// Configuration details for the packet filter.
3233
pub filter_config: FilterConfig,
34+
/// Type of socket to create.
35+
pub ip_mode: IpMode,
3336
/// If the filter is enabled this sets the default timeout for bans enacted by the filter.
3437
pub ban_duration: Option<Duration>,
3538
/// The expected responses reference.
@@ -49,7 +52,7 @@ pub struct Socket {
4952
impl Socket {
5053
/// This creates and binds a new UDP socket.
5154
// In general this function can be expanded to handle more advanced socket creation.
52-
pub(crate) async fn new_socket(
55+
async fn new_socket(
5356
socket_addr: &SocketAddr,
5457
ip_mode: IpMode,
5558
) -> Result<tokio::net::UdpSocket, Error> {
@@ -84,7 +87,9 @@ impl Socket {
8487
/// Creates a UDP socket, spawns a send/recv task and returns the channels.
8588
/// If this struct is dropped, the send/recv tasks will shutdown.
8689
/// This needs to be run inside of a tokio executor.
87-
pub(crate) fn new(socket: tokio::net::UdpSocket, config: SocketConfig) -> Result<Self, Error> {
90+
pub(crate) async fn new(config: SocketConfig) -> Result<Self, Error> {
91+
let socket = Socket::new_socket(&config.socket_addr, config.ip_mode).await?;
92+
8893
// Arc the udp socket for the send/recv tasks.
8994
let recv_udp = Arc::new(socket);
9095
let send_udp = recv_udp.clone();

0 commit comments

Comments
 (0)