Skip to content

Commit c10f37f

Browse files
committedJan 17, 2017
Properly document TCP state machine query methods.
1 parent 1682662 commit c10f37f

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed
 

Diff for: ‎src/socket/tcp.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ impl<'a> TcpSocket<'a> {
303303
self.remote_endpoint
304304
}
305305

306+
/// Return the connection state, in terms of the TCP state machine.
307+
pub fn state(&self) -> State {
308+
self.state
309+
}
310+
306311
/// Start listening on the given endpoint.
307312
///
308313
/// This function returns an error if the socket was open; see [is_open](#method.is_open).
@@ -350,6 +355,8 @@ impl<'a> TcpSocket<'a> {
350355
}
351356

352357
/// 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.
353360
pub fn is_listening(&self) -> bool {
354361
match self.state {
355362
State::Listen => true,
@@ -362,6 +369,8 @@ impl<'a> TcpSocket<'a> {
362369
/// This function returns true if the socket will process incoming or dispatch outgoing
363370
/// packets. Note that this does not mean that it is possible to send or receive data through
364371
/// 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.
365374
pub fn is_open(&self) -> bool {
366375
match self.state {
367376
State::Closed => false,
@@ -379,6 +388,9 @@ impl<'a> TcpSocket<'a> {
379388
///
380389
/// If a connection is established, [abort](#method.close) will send a reset to
381390
/// 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.
382394
pub fn is_active(&self) -> bool {
383395
match self.state {
384396
State::Closed => false,
@@ -394,6 +406,9 @@ impl<'a> TcpSocket<'a> {
394406
/// to the remote endpoint. However, it does not make any guarantees about the state
395407
/// of the transmit buffer, and even if it returns true, [send](#method.send) may
396408
/// 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.
397412
pub fn may_send(&self) -> bool {
398413
match self.state {
399414
State::Established => true,
@@ -409,6 +424,9 @@ impl<'a> TcpSocket<'a> {
409424
/// This function returns true if it's possible to receive data from the remote endpoint.
410425
/// It will return true while there is data in the receive buffer, and if there isn't,
411426
/// 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.
412430
pub fn may_recv(&self) -> bool {
413431
match self.state {
414432
State::Established => true,
@@ -421,16 +439,16 @@ impl<'a> TcpSocket<'a> {
421439
}
422440
}
423441

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.
426444
pub fn can_send(&self) -> bool {
427445
if !self.may_send() { return false }
428446

429447
!self.tx_buffer.full()
430448
}
431449

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.
434452
pub fn can_recv(&self) -> bool {
435453
if !self.may_recv() { return false }
436454

@@ -506,13 +524,6 @@ impl<'a> TcpSocket<'a> {
506524
Ok(buffer.len())
507525
}
508526

509-
/// Return the connection state.
510-
///
511-
/// This function is provided for debugging.
512-
pub fn state(&self) -> State {
513-
self.state
514-
}
515-
516527
fn set_state(&mut self, state: State) {
517528
if self.state != state {
518529
if self.remote_endpoint.addr.is_unspecified() {

0 commit comments

Comments
 (0)
Please sign in to comment.