@@ -17,7 +17,7 @@ pub use adapter::{SendStatus};
17
17
pub use resource_id:: { ResourceId , ResourceType } ;
18
18
pub use endpoint:: { Endpoint } ;
19
19
pub use remote_addr:: { RemoteAddr , ToRemoteAddr } ;
20
- pub use transport:: { Transport } ;
20
+ pub use transport:: { Transport , TransportConnect , TransportListen } ;
21
21
pub use driver:: { NetEvent } ;
22
22
pub use poll:: { Readiness } ;
23
23
@@ -101,12 +101,33 @@ impl NetworkController {
101
101
& self ,
102
102
transport : Transport ,
103
103
addr : impl ToRemoteAddr ,
104
+ ) -> io:: Result < ( Endpoint , SocketAddr ) > {
105
+ self . connect_with ( transport. into ( ) , addr)
106
+ }
107
+
108
+ /// Creates a connection to the specified address with custom transport options for transports
109
+ /// that support it.
110
+ /// The endpoint, an identifier of the new connection, will be returned.
111
+ /// This function will generate a [`NetEvent::Connected`] event with the result of the
112
+ /// connection. This call will **NOT** block to perform the connection.
113
+ ///
114
+ /// Note that this function can return an error in the case the internal socket
115
+ /// could not be binded or open in the OS, but never will return an error regarding
116
+ /// the connection itself.
117
+ /// If you want to check if the connection has been established or not you have to read the
118
+ /// boolean indicator in the [`NetEvent::Connected`] event.
119
+ pub fn connect_with (
120
+ & self ,
121
+ transport_connect : TransportConnect ,
122
+ addr : impl ToRemoteAddr ,
104
123
) -> io:: Result < ( Endpoint , SocketAddr ) > {
105
124
let addr = addr. to_remote_addr ( ) . unwrap ( ) ;
106
- self . controllers [ transport. id ( ) as usize ] . connect ( addr) . map ( |( endpoint, addr) | {
107
- log:: trace!( "Connect to {}" , endpoint) ;
108
- ( endpoint, addr)
109
- } )
125
+ self . controllers [ transport_connect. id ( ) as usize ] . connect_with ( transport_connect, addr) . map (
126
+ |( endpoint, addr) | {
127
+ log:: trace!( "Connect to {}" , endpoint) ;
128
+ ( endpoint, addr)
129
+ } ,
130
+ )
110
131
}
111
132
112
133
/// Creates a connection to the specified address.
@@ -147,7 +168,28 @@ impl NetworkController {
147
168
transport : Transport ,
148
169
addr : impl ToRemoteAddr ,
149
170
) -> io:: Result < ( Endpoint , SocketAddr ) > {
150
- let ( endpoint, addr) = self . connect ( transport, addr) ?;
171
+ self . connect_sync_with ( transport. into ( ) , addr)
172
+ }
173
+
174
+ /// Creates a connection to the specified address with custom transport options for transports
175
+ /// that support it.
176
+ /// This function is similar to [`NetworkController::connect_with()`] but will block
177
+ /// until for the connection is ready.
178
+ /// If the connection can not be established, a `ConnectionRefused` error will be returned.
179
+ ///
180
+ /// Note that the `Connect` event will be also generated.
181
+ ///
182
+ /// Since this function blocks the current thread, it must NOT be used inside
183
+ /// the network callback because the internal event could not be processed.
184
+ ///
185
+ /// In order to get the best scalability and performance, use the non-blocking
186
+ /// [`NetworkController::connect_with()`] version.
187
+ pub fn connect_sync_with (
188
+ & self ,
189
+ transport_connect : TransportConnect ,
190
+ addr : impl ToRemoteAddr ,
191
+ ) -> io:: Result < ( Endpoint , SocketAddr ) > {
192
+ let ( endpoint, addr) = self . connect_with ( transport_connect, addr) ?;
151
193
loop {
152
194
std:: thread:: sleep ( Duration :: from_millis ( 1 ) ) ;
153
195
match self . is_ready ( endpoint. resource_id ( ) ) {
@@ -164,7 +206,7 @@ impl NetworkController {
164
206
}
165
207
166
208
/// Listen messages from specified transport.
167
- /// The giver address will be used as interface and listening port.
209
+ /// The given address will be used as interface and listening port.
168
210
/// If the port can be opened, a [ResourceId] identifying the listener is returned
169
211
/// along with the local address, or an error if not.
170
212
/// The address is returned despite you passed as parameter because
@@ -173,12 +215,29 @@ impl NetworkController {
173
215
& self ,
174
216
transport : Transport ,
175
217
addr : impl ToSocketAddrs ,
218
+ ) -> io:: Result < ( ResourceId , SocketAddr ) > {
219
+ self . listen_with ( transport. into ( ) , addr)
220
+ }
221
+
222
+ /// Listen messages from specified transport with custom transport options for transports that
223
+ /// support it.
224
+ /// The given address will be used as interface and listening port.
225
+ /// If the port can be opened, a [ResourceId] identifying the listener is returned
226
+ /// along with the local address, or an error if not.
227
+ /// The address is returned despite you passed as parameter because
228
+ /// when a `0` port is specified, the OS will give choose the value.
229
+ pub fn listen_with (
230
+ & self ,
231
+ transport_listen : TransportListen ,
232
+ addr : impl ToSocketAddrs ,
176
233
) -> io:: Result < ( ResourceId , SocketAddr ) > {
177
234
let addr = addr. to_socket_addrs ( ) . unwrap ( ) . next ( ) . unwrap ( ) ;
178
- self . controllers [ transport. id ( ) as usize ] . listen ( addr) . map ( |( resource_id, addr) | {
179
- log:: trace!( "Listening at {} by {}" , addr, resource_id) ;
180
- ( resource_id, addr)
181
- } )
235
+ self . controllers [ transport_listen. id ( ) as usize ] . listen_with ( transport_listen, addr) . map (
236
+ |( resource_id, addr) | {
237
+ log:: trace!( "Listening at {} by {}" , addr, resource_id) ;
238
+ ( resource_id, addr)
239
+ } ,
240
+ )
182
241
}
183
242
184
243
/// Send the data message thought the connection represented by the given endpoint.
0 commit comments