@@ -26,6 +26,11 @@ pub const MAX_LOCAL_PAYLOAD_LEN: usize = 65535 - 20 - 8;
26
26
#[ cfg( target_os = "macos" ) ]
27
27
pub const MAX_LOCAL_PAYLOAD_LEN : usize = 9216 - 20 - 8 ;
28
28
29
+ #[ derive( Clone , Copy , PartialEq , Eq , Hash , Debug , Default ) ]
30
+ pub struct UdpConfig {
31
+ pub set_broadcast : bool ,
32
+ }
33
+
29
34
pub ( crate ) struct UdpAdapter ;
30
35
impl Adapter for UdpAdapter {
31
36
type Remote = RemoteResource ;
@@ -44,11 +49,21 @@ impl Resource for RemoteResource {
44
49
45
50
impl Remote for RemoteResource {
46
51
fn connect_with (
47
- _ : TransportConnect ,
52
+ config : TransportConnect ,
48
53
remote_addr : RemoteAddr ,
49
54
) -> io:: Result < ConnectionInfo < Self > > {
55
+ let config = match config {
56
+ TransportConnect :: Udp ( config) => config,
57
+ _ => unreachable ! ( ) ,
58
+ } ;
59
+
50
60
let socket = UdpSocket :: bind ( "0.0.0.0:0" . parse ( ) . unwrap ( ) ) ?;
51
61
let peer_addr = * remote_addr. socket_addr ( ) ;
62
+
63
+ if config. set_broadcast {
64
+ socket. set_broadcast ( true ) ?;
65
+ }
66
+
52
67
socket. connect ( peer_addr) ?;
53
68
let local_addr = socket. local_addr ( ) ?;
54
69
Ok ( ConnectionInfo { remote : RemoteResource { socket } , local_addr, peer_addr } )
@@ -98,7 +113,12 @@ impl Resource for LocalResource {
98
113
impl Local for LocalResource {
99
114
type Remote = RemoteResource ;
100
115
101
- fn listen_with ( _: TransportListen , addr : SocketAddr ) -> io:: Result < ListeningInfo < Self > > {
116
+ fn listen_with ( config : TransportListen , addr : SocketAddr ) -> io:: Result < ListeningInfo < Self > > {
117
+ let config = match config {
118
+ TransportListen :: Udp ( config) => config,
119
+ _ => unreachable ! ( ) ,
120
+ } ;
121
+
102
122
let socket = match addr {
103
123
SocketAddr :: V4 ( addr) if addr. ip ( ) . is_multicast ( ) => {
104
124
let listening_addr = SocketAddrV4 :: new ( Ipv4Addr :: UNSPECIFIED , addr. port ( ) ) ;
@@ -115,6 +135,10 @@ impl Local for LocalResource {
115
135
_ => UdpSocket :: bind ( addr) ?,
116
136
} ;
117
137
138
+ if config. set_broadcast {
139
+ socket. set_broadcast ( true ) ?;
140
+ }
141
+
118
142
let local_addr = socket. local_addr ( ) . unwrap ( ) ;
119
143
Ok ( ListeningInfo { local : { LocalResource { socket } } , local_addr } )
120
144
}
0 commit comments