2021-10-08 12:01:13 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
|
|
|
|
|
pub mod api;
|
2021-10-18 09:04:16 +00:00
|
|
|
use xous::CID;
|
2021-10-08 12:01:13 +00:00
|
|
|
|
2021-10-30 18:07:55 +00:00
|
|
|
pub mod protocols;
|
|
|
|
|
pub use protocols::*;
|
2021-10-17 21:55:42 +00:00
|
|
|
pub use smoltcp::time::Duration;
|
2021-10-19 21:18:54 +00:00
|
|
|
pub use api::*;
|
|
|
|
|
pub use smoltcp::wire::IpEndpoint;
|
2021-10-17 20:02:55 +00:00
|
|
|
|
|
|
|
|
/// NetConn is a crate-level structure that just counts the number of connections from this process to
|
|
|
|
|
/// the Net server. It's not mean to be created by user-facing code, so the visibility is (crate).
|
|
|
|
|
pub(crate) struct NetConn {
|
2021-10-08 12:01:13 +00:00
|
|
|
conn: CID,
|
|
|
|
|
}
|
2021-10-17 20:02:55 +00:00
|
|
|
impl NetConn {
|
|
|
|
|
pub(crate) fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
2021-10-08 12:01:13 +00:00
|
|
|
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
|
|
|
|
let conn = xns.request_connection_blocking(api::SERVER_NAME_NET).expect("Can't connect to Net server");
|
2021-10-17 20:02:55 +00:00
|
|
|
Ok(NetConn {
|
|
|
|
|
conn,
|
2021-10-08 12:01:13 +00:00
|
|
|
})
|
|
|
|
|
}
|
2021-10-17 20:02:55 +00:00
|
|
|
pub(crate) fn conn(&self) -> CID {
|
|
|
|
|
self.conn
|
|
|
|
|
}
|
2021-10-08 12:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use core::sync::atomic::{AtomicU32, Ordering};
|
|
|
|
|
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
2021-10-17 20:02:55 +00:00
|
|
|
impl Drop for NetConn {
|
2021-10-08 12:01:13 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
|
// the connection to the server side must be reference counted, so that multiple instances of this object within
|
|
|
|
|
// a single process do not end up de-allocating the CID on other threads before they go out of scope.
|
|
|
|
|
// Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object".
|
|
|
|
|
if REFCOUNT.load(Ordering::Relaxed) == 0 {
|
|
|
|
|
unsafe{xous::disconnect(self.conn).unwrap();}
|
|
|
|
|
}
|
|
|
|
|
// if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance),
|
|
|
|
|
// de-allocate those items here. They don't need a reference count because they are object-specific
|
|
|
|
|
}
|
2021-10-17 20:02:55 +00:00
|
|
|
}
|