@@ -26,6 +26,16 @@ 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 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
+
29
39
pub ( crate ) struct UdpAdapter ;
30
40
impl Adapter for UdpAdapter {
31
41
type Remote = RemoteResource ;
@@ -44,11 +54,21 @@ impl Resource for RemoteResource {
44
54
45
55
impl Remote for RemoteResource {
46
56
fn connect_with (
47
- _ : TransportConnect ,
57
+ config : TransportConnect ,
48
58
remote_addr : RemoteAddr ,
49
59
) -> io:: Result < ConnectionInfo < Self > > {
60
+ let config = match config {
61
+ TransportConnect :: Udp ( config) => config,
62
+ _ => unreachable ! ( ) ,
63
+ } ;
64
+
50
65
let socket = UdpSocket :: bind ( "0.0.0.0:0" . parse ( ) . unwrap ( ) ) ?;
51
66
let peer_addr = * remote_addr. socket_addr ( ) ;
67
+
68
+ if config. set_broadcast {
69
+ socket. set_broadcast ( true ) ?;
70
+ }
71
+
52
72
socket. connect ( peer_addr) ?;
53
73
let local_addr = socket. local_addr ( ) ?;
54
74
Ok ( ConnectionInfo { remote : RemoteResource { socket } , local_addr, peer_addr } )
@@ -98,7 +118,12 @@ impl Resource for LocalResource {
98
118
impl Local for LocalResource {
99
119
type Remote = RemoteResource ;
100
120
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
+
102
127
let socket = match addr {
103
128
SocketAddr :: V4 ( addr) if addr. ip ( ) . is_multicast ( ) => {
104
129
let listening_addr = SocketAddrV4 :: new ( Ipv4Addr :: UNSPECIFIED , addr. port ( ) ) ;
@@ -115,6 +140,10 @@ impl Local for LocalResource {
115
140
_ => UdpSocket :: bind ( addr) ?,
116
141
} ;
117
142
143
+ if config. set_broadcast {
144
+ socket. set_broadcast ( true ) ?;
145
+ }
146
+
118
147
let local_addr = socket. local_addr ( ) . unwrap ( ) ;
119
148
Ok ( ListeningInfo { local : { LocalResource { socket } } , local_addr } )
120
149
}
0 commit comments