Skip to content

Commit

Permalink
Try to get TCP state query methods into a saner state.
Browse files Browse the repository at this point in the history
whitequark committed Jan 14, 2017
1 parent a5c05f2 commit b860505
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 4 additions & 4 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ fn main() {
let tcp1_handle = sockets.add(tcp1_socket);
let tcp2_handle = sockets.add(tcp2_socket);

let mut tcp_6969_connected = false;
let mut tcp_6969_active = false;
loop {
// udp:6969: respond "yo dawg"
{
@@ -139,12 +139,12 @@ fn main() {
socket.listen(6970).unwrap()
}

if socket.is_connected() && !tcp_6969_connected {
if socket.is_active() && !tcp_6969_active {
debug!("tcp:6970 connected");
} else if !socket.is_connected() && tcp_6969_connected {
} else if !socket.is_active() && tcp_6969_active {
debug!("tcp:6970 disconnected");
}
tcp_6969_connected = socket.is_connected();
tcp_6969_active = socket.is_active();

if socket.may_recv() {
let data = {
12 changes: 10 additions & 2 deletions src/socket/tcp.rs
Original file line number Diff line number Diff line change
@@ -344,7 +344,15 @@ impl<'a> TcpSocket<'a> {
}
}

/// Return whether a connection is established.
/// Return whether the socket is passively listening for incoming connections.
pub fn is_listening(&self) -> bool {
match self.state {
State::Listen => true,
_ => false
}
}

/// Return whether a connection is active.
///
/// This function returns true if the socket is actively exchanging packets with
/// a remote endpoint. Note that this does not mean that it is possible to send or receive
@@ -353,7 +361,7 @@ impl<'a> TcpSocket<'a> {
///
/// If a connection is established, [abort](#method.close) will send a reset to
/// the remote endpoint.
pub fn is_connected(&self) -> bool {
pub fn is_active(&self) -> bool {
match self.state {
State::Closed => false,
State::TimeWait => false,

0 comments on commit b860505

Please sign in to comment.