Skip to content

Commit 56e7542

Browse files
committed
udp: Add optional broadcast capabilities
Signed-off-by: Konrad Gräfe <[email protected]>
1 parent 779db91 commit 56e7542

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

src/adapters/udp.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ pub const MAX_LOCAL_PAYLOAD_LEN: usize = 65535 - 20 - 8;
2626
#[cfg(target_os = "macos")]
2727
pub const MAX_LOCAL_PAYLOAD_LEN: usize = 9216 - 20 - 8;
2828

29+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
30+
pub struct UdpConnectConfig {
31+
pub set_broadcast: bool,
32+
}
33+
34+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
35+
pub struct UdpListenConfig {
36+
pub set_broadcast: bool,
37+
}
38+
2939
pub(crate) struct UdpAdapter;
3040
impl Adapter for UdpAdapter {
3141
type Remote = RemoteResource;
@@ -44,11 +54,21 @@ impl Resource for RemoteResource {
4454

4555
impl Remote for RemoteResource {
4656
fn connect_with(
47-
_: TransportConnect,
57+
config: TransportConnect,
4858
remote_addr: RemoteAddr,
4959
) -> io::Result<ConnectionInfo<Self>> {
60+
let config = match config {
61+
TransportConnect::Udp(config) => config,
62+
_ => unreachable!(),
63+
};
64+
5065
let socket = UdpSocket::bind("0.0.0.0:0".parse().unwrap())?;
5166
let peer_addr = *remote_addr.socket_addr();
67+
68+
if config.set_broadcast {
69+
socket.set_broadcast(true)?;
70+
}
71+
5272
socket.connect(peer_addr)?;
5373
let local_addr = socket.local_addr()?;
5474
Ok(ConnectionInfo { remote: RemoteResource { socket }, local_addr, peer_addr })
@@ -98,7 +118,12 @@ impl Resource for LocalResource {
98118
impl Local for LocalResource {
99119
type Remote = RemoteResource;
100120

101-
fn listen_with(_: TransportListen, addr: SocketAddr) -> io::Result<ListeningInfo<Self>> {
121+
fn listen_with(config: TransportListen, addr: SocketAddr) -> io::Result<ListeningInfo<Self>> {
122+
let config = match config {
123+
TransportListen::Udp(config) => config,
124+
_ => unreachable!(),
125+
};
126+
102127
let socket = match addr {
103128
SocketAddr::V4(addr) if addr.ip().is_multicast() => {
104129
let listening_addr = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, addr.port());
@@ -115,6 +140,10 @@ impl Local for LocalResource {
115140
_ => UdpSocket::bind(addr)?,
116141
};
117142

143+
if config.set_broadcast {
144+
socket.set_broadcast(true)?;
145+
}
146+
118147
let local_addr = socket.local_addr().unwrap();
119148
Ok(ListeningInfo { local: { LocalResource { socket } }, local_addr })
120149
}

src/network/transport.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::adapters::tcp::{TcpAdapter};
55
#[cfg(feature = "tcp")]
66
use crate::adapters::framed_tcp::{FramedTcpAdapter};
77
#[cfg(feature = "udp")]
8-
use crate::adapters::udp::{self, UdpAdapter};
8+
use crate::adapters::udp::{self, UdpAdapter, UdpConnectConfig, UdpListenConfig};
99
#[cfg(feature = "websocket")]
1010
use crate::adapters::ws::{self, WsAdapter};
1111

@@ -163,7 +163,7 @@ pub enum TransportConnect {
163163
#[cfg(feature = "tcp")]
164164
FramedTcp,
165165
#[cfg(feature = "udp")]
166-
Udp,
166+
Udp(UdpConnectConfig),
167167
#[cfg(feature = "websocket")]
168168
Ws,
169169
}
@@ -176,7 +176,7 @@ impl TransportConnect {
176176
#[cfg(feature = "tcp")]
177177
Self::FramedTcp => Transport::FramedTcp,
178178
#[cfg(feature = "udp")]
179-
Self::Udp => Transport::Udp,
179+
Self::Udp(_) => Transport::Udp,
180180
#[cfg(feature = "websocket")]
181181
Self::Ws => Transport::Ws,
182182
};
@@ -193,7 +193,7 @@ impl From<Transport> for TransportConnect {
193193
#[cfg(feature = "tcp")]
194194
Transport::FramedTcp => Self::FramedTcp,
195195
#[cfg(feature = "udp")]
196-
Transport::Udp => Self::Udp,
196+
Transport::Udp => Self::Udp(UdpConnectConfig::default()),
197197
#[cfg(feature = "websocket")]
198198
Transport::Ws => Self::Ws,
199199
}
@@ -206,7 +206,7 @@ pub enum TransportListen {
206206
#[cfg(feature = "tcp")]
207207
FramedTcp,
208208
#[cfg(feature = "udp")]
209-
Udp,
209+
Udp(UdpListenConfig),
210210
#[cfg(feature = "websocket")]
211211
Ws,
212212
}
@@ -219,7 +219,7 @@ impl TransportListen {
219219
#[cfg(feature = "tcp")]
220220
Self::FramedTcp => Transport::FramedTcp,
221221
#[cfg(feature = "udp")]
222-
Self::Udp => Transport::Udp,
222+
Self::Udp(_) => Transport::Udp,
223223
#[cfg(feature = "websocket")]
224224
Self::Ws => Transport::Ws,
225225
};
@@ -236,7 +236,7 @@ impl From<Transport> for TransportListen {
236236
#[cfg(feature = "tcp")]
237237
Transport::FramedTcp => Self::FramedTcp,
238238
#[cfg(feature = "udp")]
239-
Transport::Udp => Self::Udp,
239+
Transport::Udp => Self::Udp(UdpListenConfig::default()),
240240
#[cfg(feature = "websocket")]
241241
Transport::Ws => Self::Ws,
242242
}

0 commit comments

Comments
 (0)