apply TcpStream fixes

- allow for zero-length tx/rx returns
- implement "shutdown" function, which is different from close:
shutdown requires any pending connections to return Ok(0) immediately,
but the socket is not dropped.
This commit is contained in:
bunnie 2022-04-17 04:06:05 +08:00
parent 9eb35ddd49
commit 806b09ec0d

View file

@ -141,7 +141,12 @@ pub(crate) fn std_tcp_tx(
log::trace!("sent {}", sent_octets);
let response_data = body.buf.as_slice_mut::<u32>();
body.valid = xous::MemorySize::new(sent_octets);
if sent_octets == 0 {
// map u32::MAX to 0, because this `valid` is a NonZeroUsize type but 0 is a valid amount of data to send
body.valid = xous::MemorySize::new(u32::MAX as usize);
} else {
body.valid = xous::MemorySize::new(sent_octets);
}
response_data[0] = 0;
response_data[1] = sent_octets as u32;
}
@ -178,7 +183,16 @@ pub(crate) fn std_tcp_rx(
log::trace!("receiving data right away");
match socket.recv_slice(body.buf.as_slice_mut()) {
Ok(bytes) => {
body.valid = xous::MemorySize::new(bytes);
if bytes == 0 {
// it's actually valid to receive 0 bytes, but the encoding of this field doesn't allow it.
// so, we re-use the "MAX" value to indicate 0. I don't like this, but the other option is to
// bit-stuff a length field at the front of the body.buf slice, and that's also ugly for other reasons.
// u32::MAX is a fairly "safe" number to use, because it's well beyond the maximum length
// of a TCP packet (64k)
body.valid = xous::MemorySize::new(u32::MAX as usize);
} else {
body.valid = xous::MemorySize::new(bytes);
}
log::trace!("set body.valid = {:?}", body.valid);
}
Err(e) => {
@ -236,7 +250,16 @@ pub(crate) fn std_tcp_peek(
log::trace!("receiving data right away");
match socket.peek_slice(body.buf.as_slice_mut()) {
Ok(bytes) => {
body.valid = xous::MemorySize::new(bytes);
if bytes == 0 {
// it's actually valid to receive 0 bytes, but the encoding of this field doesn't allow it.
// so, we re-use the "MAX" value to indicate 0. I don't like this, but the other option is to
// bit-stuff a length field at the front of the body.buf slice, and that's also ugly for other reasons.
// u32::MAX is a fairly "safe" number to use, because it's well beyond the maximum length
// of a TCP packet (64k)
body.valid = xous::MemorySize::new(u32::MAX as usize);
} else {
body.valid = xous::MemorySize::new(bytes);
}
log::trace!("set body.valid = {:?}", body.valid);
}
Err(e) => {