@@ -303,6 +303,11 @@ impl<'a> TcpSocket<'a> {
303
303
self . remote_endpoint
304
304
}
305
305
306
+ /// Return the connection state, in terms of the TCP state machine.
307
+ pub fn state ( & self ) -> State {
308
+ self . state
309
+ }
310
+
306
311
/// Start listening on the given endpoint.
307
312
///
308
313
/// This function returns an error if the socket was open; see [is_open](#method.is_open).
@@ -350,6 +355,8 @@ impl<'a> TcpSocket<'a> {
350
355
}
351
356
352
357
/// Return whether the socket is passively listening for incoming connections.
358
+ ///
359
+ /// In terms of the TCP state machine, the socket must be in the `LISTEN` state.
353
360
pub fn is_listening ( & self ) -> bool {
354
361
match self . state {
355
362
State :: Listen => true ,
@@ -362,6 +369,8 @@ impl<'a> TcpSocket<'a> {
362
369
/// This function returns true if the socket will process incoming or dispatch outgoing
363
370
/// packets. Note that this does not mean that it is possible to send or receive data through
364
371
/// the socket; for that, use [can_send](#method.can_send) or [can_recv](#method.can_recv).
372
+ ///
373
+ /// In terms of the TCP state machine, the socket must be in the `CLOSED` or `TIME-WAIT` state.
365
374
pub fn is_open ( & self ) -> bool {
366
375
match self . state {
367
376
State :: Closed => false ,
@@ -379,6 +388,9 @@ impl<'a> TcpSocket<'a> {
379
388
///
380
389
/// If a connection is established, [abort](#method.close) will send a reset to
381
390
/// the remote endpoint.
391
+ ///
392
+ /// In terms of the TCP state machine, the socket must be in the `CLOSED`, `TIME-WAIT`,
393
+ /// or `LISTEN` state.
382
394
pub fn is_active ( & self ) -> bool {
383
395
match self . state {
384
396
State :: Closed => false ,
@@ -394,6 +406,9 @@ impl<'a> TcpSocket<'a> {
394
406
/// to the remote endpoint. However, it does not make any guarantees about the state
395
407
/// of the transmit buffer, and even if it returns true, [send](#method.send) may
396
408
/// not be able to enqueue any octets.
409
+ ///
410
+ /// In terms of the TCP state machine, the socket must be in the `ESTABLISHED` or
411
+ /// `CLOSE-WAIT` state.
397
412
pub fn may_send ( & self ) -> bool {
398
413
match self . state {
399
414
State :: Established => true ,
@@ -409,6 +424,9 @@ impl<'a> TcpSocket<'a> {
409
424
/// This function returns true if it's possible to receive data from the remote endpoint.
410
425
/// It will return true while there is data in the receive buffer, and if there isn't,
411
426
/// as long as the remote endpoint has not closed the connection.
427
+ ///
428
+ /// In terms of the TCP state machine, the socket must be in the `ESTABLISHED`,
429
+ /// `FIN-WAIT-1`, or `FIN-WAIT-2` state, or have data in the receive buffer instead.
412
430
pub fn may_recv ( & self ) -> bool {
413
431
match self . state {
414
432
State :: Established => true ,
@@ -421,16 +439,16 @@ impl<'a> TcpSocket<'a> {
421
439
}
422
440
}
423
441
424
- /// Check whether the transmit half of the full-duplex connection is open, and
425
- /// the transmit buffer is not full.
442
+ /// Check whether the transmit half of the full-duplex connection is open
443
+ /// (see [may_send](#method.may_send), and the transmit buffer is not full.
426
444
pub fn can_send ( & self ) -> bool {
427
445
if !self . may_send ( ) { return false }
428
446
429
447
!self . tx_buffer . full ( )
430
448
}
431
449
432
- /// Check whether the receive half of the full-duplex connection buffer is open,
433
- /// and the receive buffer is not empty.
450
+ /// Check whether the receive half of the full-duplex connection buffer is open
451
+ /// (see [may_recv](#method.may_recv), and the receive buffer is not empty.
434
452
pub fn can_recv ( & self ) -> bool {
435
453
if !self . may_recv ( ) { return false }
436
454
@@ -506,13 +524,6 @@ impl<'a> TcpSocket<'a> {
506
524
Ok ( buffer. len ( ) )
507
525
}
508
526
509
- /// Return the connection state.
510
- ///
511
- /// This function is provided for debugging.
512
- pub fn state ( & self ) -> State {
513
- self . state
514
- }
515
-
516
527
fn set_state ( & mut self , state : State ) {
517
528
if self . state != state {
518
529
if self . remote_endpoint . addr . is_unspecified ( ) {
0 commit comments