Update TcpSocket::{can,may}_{send,recv} APIs.

This commit is contained in:
whitequark 2017-01-14 06:51:29 +00:00
parent 9fb6d6f4b2
commit cca835a45a
3 changed files with 27 additions and 9 deletions

View File

@ -146,7 +146,7 @@ fn main() {
}
tcp_6969_connected = socket.is_connected();
if socket.can_recv() {
if socket.may_recv() {
let data = {
let mut data = socket.recv(128).unwrap().to_owned();
if data.len() > 0 {
@ -163,7 +163,7 @@ fn main() {
str::from_utf8(data.as_ref()).unwrap_or("(invalid utf8)"));
socket.send_slice(&data[..]).unwrap();
}
} else if socket.can_send() {
} else if socket.may_send() {
debug!("tcp:6970 close");
socket.close();
}

View File

@ -37,6 +37,14 @@ impl<'a> SocketBuffer<'a> {
self.capacity() - self.len()
}
fn empty(&self) -> bool {
self.length != 0
}
fn full(&self) -> bool {
self.window() == 0
}
fn clamp_writer(&self, mut size: usize) -> (usize, usize) {
let write_at = (self.read_at + self.length) % self.storage.len();
// We can't enqueue more than there is free space.
@ -360,7 +368,7 @@ impl<'a> TcpSocket<'a> {
/// to the remote endpoint. However, it does not make any guarantees about the state
/// of the transmit buffer, and even if it returns true, [send](#method.send) may
/// not be able to enqueue any octets.
pub fn can_send(&self) -> bool {
pub fn may_send(&self) -> bool {
match self.state {
State::Established => true,
// In CLOSE-WAIT, the remote endpoint has closed our receive half of the connection
@ -375,7 +383,7 @@ impl<'a> TcpSocket<'a> {
/// This function returns true if it's possible to receive data from the remote endpoint.
/// It will return true while there is data in the receive buffer, and if there isn't,
/// as long as the remote endpoint has not closed the connection.
pub fn can_recv(&self) -> bool {
pub fn may_recv(&self) -> bool {
match self.state {
State::Established => true,
// In FIN-WAIT-1/2, we have closed our transmit half of the connection but
@ -387,6 +395,16 @@ impl<'a> TcpSocket<'a> {
}
}
/// Check whether the transmit buffer is full.
pub fn can_send(&self) -> bool {
!self.tx_buffer.full()
}
/// Check whether the receive buffer is not empty.
pub fn can_recv(&self) -> bool {
!self.rx_buffer.empty()
}
/// Enqueue a sequence of octets to be sent, and return a pointer to it.
///
/// This function may return a slice smaller than the requested size in case

View File

@ -141,6 +141,11 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
!self.tx_buffer.full()
}
/// Check whether the receive buffer is not empty.
pub fn can_recv(&self) -> bool {
!self.rx_buffer.empty()
}
/// Enqueue a packet to be sent to a given remote endpoint, and return a pointer
/// to its payload.
///
@ -165,11 +170,6 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
Ok(data.len())
}
/// Check whether the receive buffer is full.
pub fn can_recv(&self) -> bool {
!self.rx_buffer.empty()
}
/// Dequeue a packet received from a remote endpoint, and return the endpoint as well
/// as a pointer to the payload.
///