SaiTLS/src/set.rs

84 lines
2.0 KiB
Rust
Raw Permalink Normal View History

2020-11-23 17:16:07 +08:00
use smoltcp as net;
use managed::ManagedSlice;
use crate::tls::TlsSocket;
2020-12-02 11:20:31 +08:00
use net::phy::Device;
use net::iface::EthernetInterface;
use net::time::Instant;
2020-11-23 17:16:07 +08:00
2021-07-15 17:36:32 +08:00
pub struct TlsSocketSet<'a> {
tls_sockets: ManagedSlice<'a, Option<TlsSocket<'a>>>
2020-11-23 17:16:07 +08:00
}
#[derive(Clone, Copy, Debug)]
pub struct TlsSocketHandle(usize);
2020-12-04 15:50:37 +08:00
impl TlsSocketHandle {
pub(crate) fn new(index: usize) -> Self {
Self(index)
}
}
2021-07-15 17:36:32 +08:00
impl<'a> TlsSocketSet<'a> {
2020-11-23 17:16:07 +08:00
pub fn new<T>(tls_sockets: T) -> Self
where
2021-07-15 17:36:32 +08:00
T: Into<ManagedSlice<'a, Option<TlsSocket<'a>>>>
2020-11-23 17:16:07 +08:00
{
Self {
tls_sockets: tls_sockets.into()
}
}
2021-07-15 17:36:32 +08:00
pub fn add(&mut self, socket: TlsSocket<'a>) -> TlsSocketHandle
2020-11-23 17:16:07 +08:00
{
for (index, slot) in self.tls_sockets.iter_mut().enumerate() {
if slot.is_none() {
*slot = Some(socket);
return TlsSocketHandle(index);
}
}
match self.tls_sockets {
ManagedSlice::Borrowed(_) => {
panic!("adding a socket to a full array")
}
ManagedSlice::Owned(ref mut sockets) => {
sockets.push(Some(socket));
let index = sockets.len() - 1;
return TlsSocketHandle(index);
}
}
}
2021-07-15 17:36:32 +08:00
pub fn get(&mut self, handle: TlsSocketHandle) -> &mut TlsSocket<'a> {
2020-11-23 17:16:07 +08:00
self.tls_sockets[handle.0].as_mut().unwrap()
}
2020-12-04 15:50:37 +08:00
pub fn len(&self) -> usize {
self.tls_sockets.len()
}
2020-12-02 11:20:31 +08:00
pub(crate) fn polled_by<DeviceT>(
2020-11-23 17:16:07 +08:00
&mut self,
2020-12-02 11:20:31 +08:00
iface: &mut EthernetInterface<DeviceT>,
now: Instant
2020-11-23 17:16:07 +08:00
) -> smoltcp::Result<bool>
2020-12-02 11:20:31 +08:00
where
DeviceT: for<'d> Device<'d>
2020-11-23 17:16:07 +08:00
{
2020-12-04 15:50:37 +08:00
let mut changed = false;
2020-11-23 17:16:07 +08:00
for socket in self.tls_sockets.iter_mut() {
if socket.is_some() {
2020-12-04 15:50:37 +08:00
if socket.as_mut().unwrap().update_handshake(iface, now)?
{
changed = true;
}
2020-11-23 17:16:07 +08:00
}
}
2020-12-04 15:50:37 +08:00
Ok(changed)
2020-11-23 17:16:07 +08:00
}
}