mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
split up the main.rs file
it's going to get unwieldy if we stick all of std::net into just main.rs
This commit is contained in:
parent
d026102d41
commit
9dc75071ad
3 changed files with 372 additions and 354 deletions
|
|
@ -3,9 +3,13 @@
|
|||
|
||||
mod api;
|
||||
use api::*;
|
||||
mod std_tcpstream;
|
||||
use std_tcpstream::*;
|
||||
mod std_glue;
|
||||
use std_glue::*;
|
||||
|
||||
use com::api::{ComIntSources, Ipv4Conf, NET_MTU};
|
||||
use num_traits::*;
|
||||
use ticktimer_server::Ticktimer;
|
||||
|
||||
mod connection_manager;
|
||||
mod device;
|
||||
|
|
@ -112,359 +116,6 @@ fn set_com_ints(com_int_list: &mut Vec<ComIntSources>) {
|
|||
com_int_list.push(ComIntSources::WfxErr);
|
||||
}
|
||||
|
||||
fn parse_address(data: &[u8]) -> Option<smoltcp::wire::IpAddress> {
|
||||
let mut i = data.iter();
|
||||
match i.next() {
|
||||
Some(&4) => Some(smoltcp::wire::IpAddress::v4(
|
||||
*i.next()?,
|
||||
*i.next()?,
|
||||
*i.next()?,
|
||||
*i.next()?,
|
||||
)),
|
||||
Some(&6) => {
|
||||
let mut new_addr = [0u8; 16];
|
||||
for octet in new_addr.iter_mut() {
|
||||
*octet = *i.next()?;
|
||||
}
|
||||
let v6: std::net::Ipv6Addr = new_addr.into();
|
||||
Some(v6.into())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn write_address(address: IpAddress, data: &mut [u8]) -> Option<usize> {
|
||||
let mut i = data.iter_mut();
|
||||
match address {
|
||||
IpAddress::Ipv4(a) => {
|
||||
*i.next()? = 4;
|
||||
for (dest, src) in i.zip(a.as_bytes().iter()) {
|
||||
*dest = *src;
|
||||
}
|
||||
Some(5)
|
||||
}
|
||||
IpAddress::Ipv6(a) => {
|
||||
*i.next()? = 6;
|
||||
for (dest, src) in i.zip(a.as_bytes().iter()) {
|
||||
*dest = *src;
|
||||
}
|
||||
Some(16)
|
||||
}
|
||||
_ => {
|
||||
*i.next()? = 0;
|
||||
Some(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn respond_with_error(mut env: xous::MessageEnvelope, code: NetError) -> Option<()> {
|
||||
// If it's not a memory message, don't fill in the return information.
|
||||
let body = match env.body.memory_message_mut() {
|
||||
None => {
|
||||
// But do respond to the scalar message, if it's a BlockingScalar
|
||||
if env.body.scalar_message().is_some() && env.body.is_blocking() {
|
||||
xous::return_scalar(env.sender, code as usize).ok();
|
||||
}
|
||||
return None;
|
||||
}
|
||||
Some(b) => b,
|
||||
};
|
||||
|
||||
body.valid = None;
|
||||
let s: &mut [u8] = body.buf.as_slice_mut();
|
||||
let mut i = s.iter_mut();
|
||||
|
||||
// Duplicate error to ensure it's seen as an error regardless of byte order
|
||||
*i.next()? = 1;
|
||||
*i.next()? = 1;
|
||||
*i.next()? = 1;
|
||||
*i.next()? = 1;
|
||||
*i.next()? = code as u8;
|
||||
*i.next()? = 0;
|
||||
*i.next()? = 0;
|
||||
*i.next()? = 0;
|
||||
None
|
||||
}
|
||||
|
||||
fn respond_with_connected(
|
||||
mut env: xous::MessageEnvelope,
|
||||
idx: u16,
|
||||
local_port: u16,
|
||||
remote_port: u16,
|
||||
) {
|
||||
let body = env.body.memory_message_mut().unwrap();
|
||||
let bfr = body.buf.as_slice_mut::<u16>();
|
||||
|
||||
log::trace!("successfully connected: {}", idx);
|
||||
bfr[0] = 0;
|
||||
bfr[1] = idx;
|
||||
bfr[2] = local_port;
|
||||
bfr[3] = remote_port;
|
||||
}
|
||||
|
||||
/// Insert `Some(value)` into the first slot in the Vec that is `None`,
|
||||
/// or append it to the end if there is no free slot
|
||||
fn insert_or_append<T>(arr: &mut Vec<Option<T>>, val: T) -> usize {
|
||||
// Look for a free index, or add it onto the end.
|
||||
let mut idx = None;
|
||||
for (i, elem) in arr.iter_mut().enumerate() {
|
||||
if elem.is_none() {
|
||||
idx = Some(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if let Some(idx) = idx {
|
||||
arr[idx] = Some(val);
|
||||
idx
|
||||
} else {
|
||||
let idx = arr.len();
|
||||
arr.push(Some(val));
|
||||
idx
|
||||
}
|
||||
}
|
||||
|
||||
fn std_tcp_connect(
|
||||
mut msg: xous::MessageEnvelope,
|
||||
local_port: u16,
|
||||
sockets: &mut SocketSet,
|
||||
tcp_connect_waiting: &mut Vec<Option<(xous::MessageEnvelope, SocketHandle, u16, u16, u16)>>,
|
||||
our_sockets: &mut Vec<Option<SocketHandle>>,
|
||||
) {
|
||||
// Ignore nonblocking and scalar messages
|
||||
let body = match msg.body.memory_message_mut() {
|
||||
Some(b) => b,
|
||||
None => {
|
||||
log::trace!("invalid message type");
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let bytes = body.buf.as_slice::<u8>();
|
||||
let remote_port = u16::from_le_bytes([bytes[0], bytes[1]]);
|
||||
let timeout_ms = NonZeroU64::new(u64::from_le_bytes(bytes[2..10].try_into().unwrap()));
|
||||
let address = match parse_address(&bytes[10..]) {
|
||||
Some(addr) => addr,
|
||||
None => {
|
||||
log::trace!("couldn't parse address");
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// initiates a new connection to a remote server consisting of an (Address:Port) tuple.
|
||||
// multiple connections can exist to a server, and they are further differentiated by the return port
|
||||
let mut tcp_socket = TcpSocket::new(
|
||||
TcpSocketBuffer::new(vec![0; TCP_BUFFER_SIZE]),
|
||||
TcpSocketBuffer::new(vec![0; TCP_BUFFER_SIZE]),
|
||||
);
|
||||
|
||||
// Attempt to connect, returning the error if there is one
|
||||
if let Err(e) = tcp_socket
|
||||
.connect((address, remote_port), local_port)
|
||||
.map_err(|e| match e {
|
||||
smoltcp::Error::Illegal => NetError::SocketInUse,
|
||||
smoltcp::Error::Unaddressable => NetError::Unaddressable,
|
||||
_ => NetError::LibraryError,
|
||||
})
|
||||
{
|
||||
log::trace!("couldn't connect: {:?}", e);
|
||||
respond_with_error(msg, e);
|
||||
return;
|
||||
}
|
||||
|
||||
tcp_socket.set_timeout(timeout_ms.map(|t| Duration::from_millis(t.get())));
|
||||
|
||||
let handle = sockets.add(tcp_socket);
|
||||
|
||||
// Add the socket onto the list of sockets waiting to connect, since the connection will
|
||||
// take time.
|
||||
let idx = insert_or_append(our_sockets, handle) as u16;
|
||||
insert_or_append(
|
||||
tcp_connect_waiting,
|
||||
(msg, handle, idx, local_port, remote_port),
|
||||
);
|
||||
}
|
||||
|
||||
fn std_tcp_tx(
|
||||
mut msg: xous::MessageEnvelope,
|
||||
timer: &Ticktimer,
|
||||
sockets: &mut SocketSet,
|
||||
tcp_tx_waiting: &mut Vec<Option<WaitingSocket>>,
|
||||
our_sockets: &Vec<Option<SocketHandle>>,
|
||||
) {
|
||||
let connection_handle_index = (msg.body.id() >> 16) & 0xffff;
|
||||
let body = match msg.body.memory_message_mut() {
|
||||
Some(body) => body,
|
||||
None => {
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let handle = match our_sockets.get(connection_handle_index) {
|
||||
Some(Some(val)) => val,
|
||||
_ => {
|
||||
respond_with_error(msg, NetError::Invalid);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut socket = sockets.get::<TcpSocket>(*handle);
|
||||
if !socket.can_send() {
|
||||
log::trace!("tx can't send, will retry");
|
||||
let expiry = body
|
||||
.offset
|
||||
.map(|x| unsafe { NonZeroU64::new_unchecked(x.get() as u64 + timer.elapsed_ms()) });
|
||||
// Add the message to the TcpRxWaiting list, which will prevent it from getting
|
||||
// responded to right away.
|
||||
insert_or_append(
|
||||
tcp_tx_waiting,
|
||||
WaitingSocket {
|
||||
env: msg,
|
||||
handle: *handle,
|
||||
expiry,
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Perform the transfer
|
||||
let sent_octets = {
|
||||
let data = body.buf.as_slice::<u8>();
|
||||
let length = body
|
||||
.valid
|
||||
.map(|v| {
|
||||
if v.get() > data.len() {
|
||||
data.len()
|
||||
} else {
|
||||
v.get()
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| data.len());
|
||||
|
||||
match socket.send_slice(&data[..length]) {
|
||||
Ok(octets) => octets,
|
||||
Err(_) => {
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
log::trace!("sent {}", sent_octets);
|
||||
let response_data = body.buf.as_slice_mut::<u32>();
|
||||
body.valid = xous::MemorySize::new(sent_octets);
|
||||
response_data[0] = 0;
|
||||
response_data[1] = sent_octets as u32;
|
||||
}
|
||||
|
||||
fn std_tcp_rx(
|
||||
mut msg: xous::MessageEnvelope,
|
||||
timer: &Ticktimer,
|
||||
sockets: &mut SocketSet,
|
||||
tcp_rx_waiting: &mut Vec<Option<WaitingSocket>>,
|
||||
our_sockets: &Vec<Option<SocketHandle>>,
|
||||
) {
|
||||
let connection_handle_index = (msg.body.id() >> 16) & 0xffff;
|
||||
let body = match msg.body.memory_message_mut() {
|
||||
Some(body) => body,
|
||||
None => {
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Default to having no valid data upon return, indicating an error
|
||||
body.valid = None;
|
||||
|
||||
let handle = match our_sockets.get(connection_handle_index) {
|
||||
Some(Some(val)) => val,
|
||||
_ => {
|
||||
respond_with_error(msg, NetError::Invalid);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut socket = sockets.get::<TcpSocket>(*handle);
|
||||
if socket.can_recv() {
|
||||
log::trace!("receiving data right away");
|
||||
match socket.recv_slice(body.buf.as_slice_mut()) {
|
||||
Ok(bytes) => {
|
||||
body.valid = xous::MemorySize::new(bytes);
|
||||
log::trace!("set body.valid = {:?}", body.valid);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("unable to receive: {:?}", e);
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
log::trace!("socket was not able to receive, adding it to list of waiting messages");
|
||||
|
||||
// Add the message to the TcpRxWaiting list, which will prevent it from getting
|
||||
// responded to right away.
|
||||
let expiry = body
|
||||
.offset
|
||||
.map(|x| unsafe { NonZeroU64::new_unchecked(x.get() as u64 + timer.elapsed_ms()) });
|
||||
insert_or_append(
|
||||
tcp_rx_waiting,
|
||||
WaitingSocket {
|
||||
env: msg,
|
||||
handle: *handle,
|
||||
expiry,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn std_tcp_peek(
|
||||
mut msg: xous::MessageEnvelope,
|
||||
sockets: &mut SocketSet,
|
||||
our_sockets: &Vec<Option<SocketHandle>>,
|
||||
) {
|
||||
let connection_handle_index = (msg.body.id() >> 16) & 0xffff;
|
||||
let body = match msg.body.memory_message_mut() {
|
||||
Some(body) => body,
|
||||
None => {
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Default to having no valid data upon return, indicating an error
|
||||
body.valid = None;
|
||||
|
||||
let handle = match our_sockets.get(connection_handle_index) {
|
||||
Some(Some(val)) => val,
|
||||
_ => {
|
||||
respond_with_error(msg, NetError::Invalid);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut socket = sockets.get::<TcpSocket>(*handle);
|
||||
if socket.can_recv() {
|
||||
log::trace!("receiving data right away");
|
||||
match socket.peek_slice(body.buf.as_slice_mut()) {
|
||||
Ok(bytes) => {
|
||||
body.valid = xous::MemorySize::new(bytes);
|
||||
log::trace!("set body.valid = {:?}", body.valid);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("unable to receive: {:?}", e);
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No data available, so indicate `None`
|
||||
body.valid = None;
|
||||
// Also indicate no error
|
||||
body.buf.as_slice_mut::<u32>()[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#[xous::xous_main]
|
||||
fn xmain() -> ! {
|
||||
log_server::init_wait().unwrap();
|
||||
|
|
|
|||
114
services/net/src/std_glue.rs
Normal file
114
services/net/src/std_glue.rs
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
use smoltcp::wire::IpAddress;
|
||||
use crate::*;
|
||||
|
||||
|
||||
pub(crate) fn parse_address(data: &[u8]) -> Option<smoltcp::wire::IpAddress> {
|
||||
let mut i = data.iter();
|
||||
match i.next() {
|
||||
Some(&4) => Some(smoltcp::wire::IpAddress::v4(
|
||||
*i.next()?,
|
||||
*i.next()?,
|
||||
*i.next()?,
|
||||
*i.next()?,
|
||||
)),
|
||||
Some(&6) => {
|
||||
let mut new_addr = [0u8; 16];
|
||||
for octet in new_addr.iter_mut() {
|
||||
*octet = *i.next()?;
|
||||
}
|
||||
let v6: std::net::Ipv6Addr = new_addr.into();
|
||||
Some(v6.into())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn write_address(address: IpAddress, data: &mut [u8]) -> Option<usize> {
|
||||
let mut i = data.iter_mut();
|
||||
match address {
|
||||
IpAddress::Ipv4(a) => {
|
||||
*i.next()? = 4;
|
||||
for (dest, src) in i.zip(a.as_bytes().iter()) {
|
||||
*dest = *src;
|
||||
}
|
||||
Some(5)
|
||||
}
|
||||
IpAddress::Ipv6(a) => {
|
||||
*i.next()? = 6;
|
||||
for (dest, src) in i.zip(a.as_bytes().iter()) {
|
||||
*dest = *src;
|
||||
}
|
||||
Some(16)
|
||||
}
|
||||
_ => {
|
||||
*i.next()? = 0;
|
||||
Some(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn respond_with_error(mut env: xous::MessageEnvelope, code: NetError) -> Option<()> {
|
||||
// If it's not a memory message, don't fill in the return information.
|
||||
let body = match env.body.memory_message_mut() {
|
||||
None => {
|
||||
// But do respond to the scalar message, if it's a BlockingScalar
|
||||
if env.body.scalar_message().is_some() && env.body.is_blocking() {
|
||||
xous::return_scalar(env.sender, code as usize).ok();
|
||||
}
|
||||
return None;
|
||||
}
|
||||
Some(b) => b,
|
||||
};
|
||||
|
||||
body.valid = None;
|
||||
let s: &mut [u8] = body.buf.as_slice_mut();
|
||||
let mut i = s.iter_mut();
|
||||
|
||||
// Duplicate error to ensure it's seen as an error regardless of byte order
|
||||
*i.next()? = 1;
|
||||
*i.next()? = 1;
|
||||
*i.next()? = 1;
|
||||
*i.next()? = 1;
|
||||
*i.next()? = code as u8;
|
||||
*i.next()? = 0;
|
||||
*i.next()? = 0;
|
||||
*i.next()? = 0;
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn respond_with_connected(
|
||||
mut env: xous::MessageEnvelope,
|
||||
idx: u16,
|
||||
local_port: u16,
|
||||
remote_port: u16,
|
||||
) {
|
||||
let body = env.body.memory_message_mut().unwrap();
|
||||
let bfr = body.buf.as_slice_mut::<u16>();
|
||||
|
||||
log::trace!("successfully connected: {}", idx);
|
||||
bfr[0] = 0;
|
||||
bfr[1] = idx;
|
||||
bfr[2] = local_port;
|
||||
bfr[3] = remote_port;
|
||||
}
|
||||
|
||||
/// Insert `Some(value)` into the first slot in the Vec that is `None`,
|
||||
/// or append it to the end if there is no free slot
|
||||
pub(crate) fn insert_or_append<T>(arr: &mut Vec<Option<T>>, val: T) -> usize {
|
||||
// Look for a free index, or add it onto the end.
|
||||
let mut idx = None;
|
||||
for (i, elem) in arr.iter_mut().enumerate() {
|
||||
if elem.is_none() {
|
||||
idx = Some(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if let Some(idx) = idx {
|
||||
arr[idx] = Some(val);
|
||||
idx
|
||||
} else {
|
||||
let idx = arr.len();
|
||||
arr.push(Some(val));
|
||||
idx
|
||||
}
|
||||
}
|
||||
253
services/net/src/std_tcpstream.rs
Normal file
253
services/net/src/std_tcpstream.rs
Normal file
|
|
@ -0,0 +1,253 @@
|
|||
use ticktimer_server::Ticktimer;
|
||||
|
||||
use std::convert::TryInto;
|
||||
|
||||
use smoltcp::socket::SocketSet;
|
||||
|
||||
use core::num::NonZeroU64;
|
||||
use smoltcp::socket::{
|
||||
SocketHandle, TcpSocket, TcpSocketBuffer,
|
||||
};
|
||||
use crate::*;
|
||||
|
||||
pub(crate) fn std_tcp_connect(
|
||||
mut msg: xous::MessageEnvelope,
|
||||
local_port: u16,
|
||||
sockets: &mut SocketSet,
|
||||
tcp_connect_waiting: &mut Vec<Option<(xous::MessageEnvelope, SocketHandle, u16, u16, u16)>>,
|
||||
our_sockets: &mut Vec<Option<SocketHandle>>,
|
||||
) {
|
||||
// Ignore nonblocking and scalar messages
|
||||
let body = match msg.body.memory_message_mut() {
|
||||
Some(b) => b,
|
||||
None => {
|
||||
log::trace!("invalid message type");
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let bytes = body.buf.as_slice::<u8>();
|
||||
let remote_port = u16::from_le_bytes([bytes[0], bytes[1]]);
|
||||
let timeout_ms = NonZeroU64::new(u64::from_le_bytes(bytes[2..10].try_into().unwrap()));
|
||||
let address = match parse_address(&bytes[10..]) {
|
||||
Some(addr) => addr,
|
||||
None => {
|
||||
log::trace!("couldn't parse address");
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// initiates a new connection to a remote server consisting of an (Address:Port) tuple.
|
||||
// multiple connections can exist to a server, and they are further differentiated by the return port
|
||||
let mut tcp_socket = TcpSocket::new(
|
||||
TcpSocketBuffer::new(vec![0; TCP_BUFFER_SIZE]),
|
||||
TcpSocketBuffer::new(vec![0; TCP_BUFFER_SIZE]),
|
||||
);
|
||||
|
||||
// Attempt to connect, returning the error if there is one
|
||||
if let Err(e) = tcp_socket
|
||||
.connect((address, remote_port), local_port)
|
||||
.map_err(|e| match e {
|
||||
smoltcp::Error::Illegal => NetError::SocketInUse,
|
||||
smoltcp::Error::Unaddressable => NetError::Unaddressable,
|
||||
_ => NetError::LibraryError,
|
||||
})
|
||||
{
|
||||
log::trace!("couldn't connect: {:?}", e);
|
||||
respond_with_error(msg, e);
|
||||
return;
|
||||
}
|
||||
|
||||
tcp_socket.set_timeout(timeout_ms.map(|t| Duration::from_millis(t.get())));
|
||||
|
||||
let handle = sockets.add(tcp_socket);
|
||||
|
||||
// Add the socket onto the list of sockets waiting to connect, since the connection will
|
||||
// take time.
|
||||
let idx = insert_or_append(our_sockets, handle) as u16;
|
||||
insert_or_append(
|
||||
tcp_connect_waiting,
|
||||
(msg, handle, idx, local_port, remote_port),
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn std_tcp_tx(
|
||||
mut msg: xous::MessageEnvelope,
|
||||
timer: &Ticktimer,
|
||||
sockets: &mut SocketSet,
|
||||
tcp_tx_waiting: &mut Vec<Option<WaitingSocket>>,
|
||||
our_sockets: &Vec<Option<SocketHandle>>,
|
||||
) {
|
||||
let connection_handle_index = (msg.body.id() >> 16) & 0xffff;
|
||||
let body = match msg.body.memory_message_mut() {
|
||||
Some(body) => body,
|
||||
None => {
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let handle = match our_sockets.get(connection_handle_index) {
|
||||
Some(Some(val)) => val,
|
||||
_ => {
|
||||
respond_with_error(msg, NetError::Invalid);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut socket = sockets.get::<TcpSocket>(*handle);
|
||||
if !socket.can_send() {
|
||||
log::trace!("tx can't send, will retry");
|
||||
let expiry = body
|
||||
.offset
|
||||
.map(|x| unsafe { NonZeroU64::new_unchecked(x.get() as u64 + timer.elapsed_ms()) });
|
||||
// Add the message to the TcpRxWaiting list, which will prevent it from getting
|
||||
// responded to right away.
|
||||
insert_or_append(
|
||||
tcp_tx_waiting,
|
||||
WaitingSocket {
|
||||
env: msg,
|
||||
handle: *handle,
|
||||
expiry,
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Perform the transfer
|
||||
let sent_octets = {
|
||||
let data = body.buf.as_slice::<u8>();
|
||||
let length = body
|
||||
.valid
|
||||
.map(|v| {
|
||||
if v.get() > data.len() {
|
||||
data.len()
|
||||
} else {
|
||||
v.get()
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| data.len());
|
||||
|
||||
match socket.send_slice(&data[..length]) {
|
||||
Ok(octets) => octets,
|
||||
Err(_) => {
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
log::trace!("sent {}", sent_octets);
|
||||
let response_data = body.buf.as_slice_mut::<u32>();
|
||||
body.valid = xous::MemorySize::new(sent_octets);
|
||||
response_data[0] = 0;
|
||||
response_data[1] = sent_octets as u32;
|
||||
}
|
||||
|
||||
pub(crate) fn std_tcp_rx(
|
||||
mut msg: xous::MessageEnvelope,
|
||||
timer: &Ticktimer,
|
||||
sockets: &mut SocketSet,
|
||||
tcp_rx_waiting: &mut Vec<Option<WaitingSocket>>,
|
||||
our_sockets: &Vec<Option<SocketHandle>>,
|
||||
) {
|
||||
let connection_handle_index = (msg.body.id() >> 16) & 0xffff;
|
||||
let body = match msg.body.memory_message_mut() {
|
||||
Some(body) => body,
|
||||
None => {
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Default to having no valid data upon return, indicating an error
|
||||
body.valid = None;
|
||||
|
||||
let handle = match our_sockets.get(connection_handle_index) {
|
||||
Some(Some(val)) => val,
|
||||
_ => {
|
||||
respond_with_error(msg, NetError::Invalid);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut socket = sockets.get::<TcpSocket>(*handle);
|
||||
if socket.can_recv() {
|
||||
log::trace!("receiving data right away");
|
||||
match socket.recv_slice(body.buf.as_slice_mut()) {
|
||||
Ok(bytes) => {
|
||||
body.valid = xous::MemorySize::new(bytes);
|
||||
log::trace!("set body.valid = {:?}", body.valid);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("unable to receive: {:?}", e);
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
log::trace!("socket was not able to receive, adding it to list of waiting messages");
|
||||
|
||||
// Add the message to the TcpRxWaiting list, which will prevent it from getting
|
||||
// responded to right away.
|
||||
let expiry = body
|
||||
.offset
|
||||
.map(|x| unsafe { NonZeroU64::new_unchecked(x.get() as u64 + timer.elapsed_ms()) });
|
||||
insert_or_append(
|
||||
tcp_rx_waiting,
|
||||
WaitingSocket {
|
||||
env: msg,
|
||||
handle: *handle,
|
||||
expiry,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn std_tcp_peek(
|
||||
mut msg: xous::MessageEnvelope,
|
||||
sockets: &mut SocketSet,
|
||||
our_sockets: &Vec<Option<SocketHandle>>,
|
||||
) {
|
||||
let connection_handle_index = (msg.body.id() >> 16) & 0xffff;
|
||||
let body = match msg.body.memory_message_mut() {
|
||||
Some(body) => body,
|
||||
None => {
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Default to having no valid data upon return, indicating an error
|
||||
body.valid = None;
|
||||
|
||||
let handle = match our_sockets.get(connection_handle_index) {
|
||||
Some(Some(val)) => val,
|
||||
_ => {
|
||||
respond_with_error(msg, NetError::Invalid);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut socket = sockets.get::<TcpSocket>(*handle);
|
||||
if socket.can_recv() {
|
||||
log::trace!("receiving data right away");
|
||||
match socket.peek_slice(body.buf.as_slice_mut()) {
|
||||
Ok(bytes) => {
|
||||
body.valid = xous::MemorySize::new(bytes);
|
||||
log::trace!("set body.valid = {:?}", body.valid);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("unable to receive: {:?}", e);
|
||||
respond_with_error(msg, NetError::LibraryError);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No data available, so indicate `None`
|
||||
body.valid = None;
|
||||
// Also indicate no error
|
||||
body.buf.as_slice_mut::<u32>()[0] = 0;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue