@@ -26,6 +26,20 @@ 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 , PartialEq , Eq , Hash , Debug , Default ) ]
30
+ pub struct UdpConnectConfig {
31
+ /// Enables the socket capabilities to send broadcast messages.
32
+ pub broadcast : bool ,
33
+ }
34
+
35
+ #[ derive( Clone , PartialEq , Eq , Hash , Debug , Default ) ]
36
+ pub struct UdpListenConfig {
37
+ /// Enables the socket capabilities to send broadcast messages when the listening socket is
38
+ /// also used for sending with
39
+ /// [`Endpoint::from_listener`](crate::network::Endpoint::from_listener).
40
+ pub broadcast : bool ,
41
+ }
42
+
29
43
pub ( crate ) struct UdpAdapter ;
30
44
impl Adapter for UdpAdapter {
31
45
type Remote = RemoteResource ;
@@ -44,11 +58,21 @@ impl Resource for RemoteResource {
44
58
45
59
impl Remote for RemoteResource {
46
60
fn connect_with (
47
- _ : TransportConnect ,
61
+ config : TransportConnect ,
48
62
remote_addr : RemoteAddr ,
49
63
) -> io:: Result < ConnectionInfo < Self > > {
64
+ let config = match config {
65
+ TransportConnect :: Udp ( config) => config,
66
+ _ => panic ! ( "Internal error: Got wrong config" ) ,
67
+ } ;
68
+
50
69
let socket = UdpSocket :: bind ( "0.0.0.0:0" . parse ( ) . unwrap ( ) ) ?;
51
70
let peer_addr = * remote_addr. socket_addr ( ) ;
71
+
72
+ if config. broadcast {
73
+ socket. set_broadcast ( true ) ?;
74
+ }
75
+
52
76
socket. connect ( peer_addr) ?;
53
77
let local_addr = socket. local_addr ( ) ?;
54
78
Ok ( ConnectionInfo { remote : RemoteResource { socket } , local_addr, peer_addr } )
@@ -98,7 +122,12 @@ impl Resource for LocalResource {
98
122
impl Local for LocalResource {
99
123
type Remote = RemoteResource ;
100
124
101
- fn listen_with ( _: TransportListen , addr : SocketAddr ) -> io:: Result < ListeningInfo < Self > > {
125
+ fn listen_with ( config : TransportListen , addr : SocketAddr ) -> io:: Result < ListeningInfo < Self > > {
126
+ let config = match config {
127
+ TransportListen :: Udp ( config) => config,
128
+ _ => panic ! ( "Internal error: Got wrong config" ) ,
129
+ } ;
130
+
102
131
let socket = match addr {
103
132
SocketAddr :: V4 ( addr) if addr. ip ( ) . is_multicast ( ) => {
104
133
let listening_addr = SocketAddrV4 :: new ( Ipv4Addr :: UNSPECIFIED , addr. port ( ) ) ;
@@ -115,6 +144,10 @@ impl Local for LocalResource {
115
144
_ => UdpSocket :: bind ( addr) ?,
116
145
} ;
117
146
147
+ if config. broadcast {
148
+ socket. set_broadcast ( true ) ?;
149
+ }
150
+
118
151
let local_addr = socket. local_addr ( ) . unwrap ( ) ;
119
152
Ok ( ListeningInfo { local : { LocalResource { socket } } , local_addr } )
120
153
}
0 commit comments