@@ -180,6 +180,117 @@ impl TcpStream {
180
180
pub fn write_timeout ( & self ) -> io:: Result < Option < Duration > > {
181
181
self . 0 . write_timeout ( )
182
182
}
183
+
184
+ /// Sets the value of the `TCP_NODELAY` option on this socket.
185
+ ///
186
+ /// If set, this option disables the Nagle algorithm. This means that
187
+ /// segments are always sent as soon as possible, even if there is only a
188
+ /// small amount of data. When not set, data is buffered until there is a
189
+ /// sufficient amount to send out, thereby avoiding the frequent sending of
190
+ /// small packets.
191
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
192
+ pub fn set_nodelay ( & self , nodelay : bool ) -> io:: Result < ( ) > {
193
+ self . 0 . set_nodelay ( nodelay)
194
+ }
195
+
196
+ /// Gets the value of the `TCP_NODELAY` option on this socket.
197
+ ///
198
+ /// For more information about this option, see [`set_nodelay`][link].
199
+ ///
200
+ /// [link]: #tymethod.set_nodelay
201
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
202
+ pub fn nodelay ( & self ) -> io:: Result < bool > {
203
+ self . 0 . nodelay ( )
204
+ }
205
+
206
+ /// Sets whether keepalive messages are enabled to be sent on this socket.
207
+ ///
208
+ /// On Unix, this option will set the `SO_KEEPALIVE` as well as the
209
+ /// `TCP_KEEPALIVE` or `TCP_KEEPIDLE` option (depending on your platform).
210
+ /// On Windows, this will set the `SIO_KEEPALIVE_VALS` option.
211
+ ///
212
+ /// If `None` is specified then keepalive messages are disabled, otherwise
213
+ /// the duration specified will be the time to remain idle before sending a
214
+ /// TCP keepalive probe.
215
+ ///
216
+ /// Some platforms specify this value in seconds, so sub-second
217
+ /// specifications may be omitted.
218
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
219
+ pub fn set_keepalive ( & self , keepalive : Option < Duration > ) -> io:: Result < ( ) > {
220
+ self . 0 . set_keepalive ( keepalive)
221
+ }
222
+
223
+ /// Returns whether keepalive messages are enabled on this socket, and if so
224
+ /// the duration of time between them.
225
+ ///
226
+ /// For more information about this option, see [`set_keepalive`][link].
227
+ ///
228
+ /// [link]: #tymethod.set_keepalive
229
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
230
+ pub fn keepalive ( & self ) -> io:: Result < Option < Duration > > {
231
+ self . 0 . keepalive ( )
232
+ }
233
+
234
+ /// Sets the value for the `IP_TTL` option on this socket.
235
+ ///
236
+ /// This value sets the time-to-live field that is used in every packet sent
237
+ /// from this socket.
238
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
239
+ pub fn set_ttl ( & self , ttl : u32 ) -> io:: Result < ( ) > {
240
+ self . 0 . set_ttl ( ttl)
241
+ }
242
+
243
+ /// Gets the value of the `IP_TTL` option for this socket.
244
+ ///
245
+ /// For more information about this option, see [`set_ttl`][link].
246
+ ///
247
+ /// [link]: #tymethod.set_ttl
248
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
249
+ pub fn ttl ( & self ) -> io:: Result < u32 > {
250
+ self . 0 . ttl ( )
251
+ }
252
+
253
+ /// Sets the value for the `IPV6_V6ONLY` option on this socket.
254
+ ///
255
+ /// If this is set to `true` then the socket is restricted to sending and
256
+ /// receiving IPv6 packets only. If this is the case, an IPv4 and an IPv6
257
+ /// application can each bind the same port at the same time.
258
+ ///
259
+ /// If this is set to `false` then the socket can be used to send and
260
+ /// receive packets from an IPv4-mapped IPv6 address.
261
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
262
+ pub fn set_only_v6 ( & self , only_v6 : bool ) -> io:: Result < ( ) > {
263
+ self . 0 . set_only_v6 ( only_v6)
264
+ }
265
+
266
+ /// Gets the value of the `IPV6_V6ONLY` option for this socket.
267
+ ///
268
+ /// For more information about this option, see [`set_only_v6`][link].
269
+ ///
270
+ /// [link]: #tymethod.set_only_v6
271
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
272
+ pub fn only_v6 ( & self ) -> io:: Result < bool > {
273
+ self . 0 . only_v6 ( )
274
+ }
275
+
276
+ /// Get the value of the `SO_ERROR` option on this socket.
277
+ ///
278
+ /// This will retrieve the stored error in the underlying socket, clearing
279
+ /// the field in the process. This can be useful for checking errors between
280
+ /// calls.
281
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
282
+ pub fn take_error ( & self ) -> io:: Result < Option < io:: Error > > {
283
+ self . 0 . take_error ( )
284
+ }
285
+
286
+ /// Moves this TCP stream into or out of nonblocking mode.
287
+ ///
288
+ /// On Unix this corresponds to calling fcntl, and on Windows this
289
+ /// corresponds to calling ioctlsocket.
290
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
291
+ pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
292
+ self . 0 . set_nonblocking ( nonblocking)
293
+ }
183
294
}
184
295
185
296
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -278,6 +389,67 @@ impl TcpListener {
278
389
pub fn incoming ( & self ) -> Incoming {
279
390
Incoming { listener : self }
280
391
}
392
+
393
+ /// Sets the value for the `IP_TTL` option on this socket.
394
+ ///
395
+ /// This value sets the time-to-live field that is used in every packet sent
396
+ /// from this socket.
397
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
398
+ pub fn set_ttl ( & self , ttl : u32 ) -> io:: Result < ( ) > {
399
+ self . 0 . set_ttl ( ttl)
400
+ }
401
+
402
+ /// Gets the value of the `IP_TTL` option for this socket.
403
+ ///
404
+ /// For more information about this option, see [`set_ttl`][link].
405
+ ///
406
+ /// [link]: #tymethod.set_ttl
407
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
408
+ pub fn ttl ( & self ) -> io:: Result < u32 > {
409
+ self . 0 . ttl ( )
410
+ }
411
+
412
+ /// Sets the value for the `IPV6_V6ONLY` option on this socket.
413
+ ///
414
+ /// If this is set to `true` then the socket is restricted to sending and
415
+ /// receiving IPv6 packets only. In this case two IPv4 and IPv6 applications
416
+ /// can bind the same port at the same time.
417
+ ///
418
+ /// If this is set to `false` then the socket can be used to send and
419
+ /// receive packets from an IPv4-mapped IPv6 address.
420
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
421
+ pub fn set_only_v6 ( & self , only_v6 : bool ) -> io:: Result < ( ) > {
422
+ self . 0 . set_only_v6 ( only_v6)
423
+ }
424
+
425
+ /// Gets the value of the `IPV6_V6ONLY` option for this socket.
426
+ ///
427
+ /// For more information about this option, see [`set_only_v6`][link].
428
+ ///
429
+ /// [link]: #tymethod.set_only_v6
430
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
431
+ pub fn only_v6 ( & self ) -> io:: Result < bool > {
432
+ self . 0 . only_v6 ( )
433
+ }
434
+
435
+ /// Get the value of the `SO_ERROR` option on this socket.
436
+ ///
437
+ /// This will retrieve the stored error in the underlying socket, clearing
438
+ /// the field in the process. This can be useful for checking errors between
439
+ /// calls.
440
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
441
+ pub fn take_error ( & self ) -> io:: Result < Option < io:: Error > > {
442
+ self . 0 . take_error ( )
443
+ }
444
+
445
+ /// Moves this TCP stream into or out of nonblocking mode.
446
+ ///
447
+ /// On Unix this corresponds to calling fcntl, and on Windows this
448
+ /// corresponds to calling ioctlsocket.
449
+ #[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
450
+ pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
451
+ self . 0 . set_nonblocking ( nonblocking)
452
+ }
281
453
}
282
454
283
455
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -969,4 +1141,63 @@ mod tests {
969
1141
assert ! ( start. elapsed( ) > Duration :: from_millis( 400 ) ) ;
970
1142
drop ( listener) ;
971
1143
}
1144
+
1145
+ #[ test]
1146
+ fn nodelay ( ) {
1147
+ let addr = next_test_ip4 ( ) ;
1148
+ let _listener = t ! ( TcpListener :: bind( & addr) ) ;
1149
+
1150
+ let stream = t ! ( TcpStream :: connect( & ( "localhost" , addr. port( ) ) ) ) ;
1151
+
1152
+ assert_eq ! ( false , t!( stream. nodelay( ) ) ) ;
1153
+ t ! ( stream. set_nodelay( true ) ) ;
1154
+ assert_eq ! ( true , t!( stream. nodelay( ) ) ) ;
1155
+ t ! ( stream. set_nodelay( false ) ) ;
1156
+ assert_eq ! ( false , t!( stream. nodelay( ) ) ) ;
1157
+ }
1158
+
1159
+ #[ test]
1160
+ fn keepalive ( ) {
1161
+ let addr = next_test_ip4 ( ) ;
1162
+ let _listener = t ! ( TcpListener :: bind( & addr) ) ;
1163
+
1164
+ let stream = t ! ( TcpStream :: connect( & ( "localhost" , addr. port( ) ) ) ) ;
1165
+ let dur = Duration :: new ( 15410 , 0 ) ;
1166
+
1167
+ assert_eq ! ( None , t!( stream. keepalive( ) ) ) ;
1168
+ t ! ( stream. set_keepalive( Some ( dur) ) ) ;
1169
+ assert_eq ! ( Some ( dur) , t!( stream. keepalive( ) ) ) ;
1170
+ t ! ( stream. set_keepalive( None ) ) ;
1171
+ assert_eq ! ( None , t!( stream. keepalive( ) ) ) ;
1172
+ }
1173
+
1174
+ #[ test]
1175
+ fn ttl ( ) {
1176
+ let ttl = 100 ;
1177
+
1178
+ let addr = next_test_ip4 ( ) ;
1179
+ let listener = t ! ( TcpListener :: bind( & addr) ) ;
1180
+
1181
+ t ! ( listener. set_ttl( ttl) ) ;
1182
+ assert_eq ! ( ttl, t!( listener. ttl( ) ) ) ;
1183
+
1184
+ let stream = t ! ( TcpStream :: connect( & ( "localhost" , addr. port( ) ) ) ) ;
1185
+
1186
+ t ! ( stream. set_ttl( ttl) ) ;
1187
+ assert_eq ! ( ttl, t!( stream. ttl( ) ) ) ;
1188
+ }
1189
+
1190
+ #[ test]
1191
+ fn set_nonblocking ( ) {
1192
+ let addr = next_test_ip4 ( ) ;
1193
+ let listener = t ! ( TcpListener :: bind( & addr) ) ;
1194
+
1195
+ t ! ( listener. set_nonblocking( true ) ) ;
1196
+ t ! ( listener. set_nonblocking( false ) ) ;
1197
+
1198
+ let stream = t ! ( TcpStream :: connect( & ( "localhost" , addr. port( ) ) ) ) ;
1199
+
1200
+ t ! ( stream. set_nonblocking( true ) ) ;
1201
+ t ! ( stream. set_nonblocking( false ) ) ;
1202
+ }
972
1203
}
0 commit comments