diff --git a/services/net/src/std_tcpstream.rs b/services/net/src/std_tcpstream.rs index 479db3080..5a7d87b15 100644 --- a/services/net/src/std_tcpstream.rs +++ b/services/net/src/std_tcpstream.rs @@ -141,7 +141,12 @@ pub(crate) fn std_tcp_tx( log::trace!("sent {}", sent_octets); let response_data = body.buf.as_slice_mut::(); - 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) => {