SaiTLS/src/set.rs

74 lines
1.9 KiB
Rust
Raw Normal View History

2020-11-23 17:16:07 +08:00
use smoltcp as net;
use managed::ManagedSlice;
use crate::tls::TlsSocket;
use net::socket::SocketSet;
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
2020-12-02 11:20:31 +08:00
pub struct TlsSocketSet<'a, 'b, 'c> {
tls_sockets: ManagedSlice<'a, Option<TlsSocket<'a, 'b, 'c>>>
2020-11-23 17:16:07 +08:00
}
#[derive(Clone, Copy, Debug)]
pub struct TlsSocketHandle(usize);
2020-12-02 11:20:31 +08:00
impl<'a, 'b, 'c> TlsSocketSet<'a, 'b, 'c> {
2020-11-23 17:16:07 +08:00
pub fn new<T>(tls_sockets: T) -> Self
where
2020-12-02 11:20:31 +08:00
T: Into<ManagedSlice<'a, Option<TlsSocket<'a, 'b, 'c>>>>
2020-11-23 17:16:07 +08:00
{
Self {
tls_sockets: tls_sockets.into()
}
}
2020-12-02 11:20:31 +08:00
pub fn add(&mut self, socket: TlsSocket<'a, 'b, 'c>) -> 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);
}
}
}
2020-12-02 11:20:31 +08:00
pub fn get(&mut self, handle: TlsSocketHandle) -> &mut TlsSocket<'a, 'b, 'c> {
2020-11-23 17:16:07 +08:00
self.tls_sockets[handle.0].as_mut().unwrap()
}
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
sockets: &mut SocketSet,
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
{
for socket in self.tls_sockets.iter_mut() {
if socket.is_some() {
socket.as_mut()
.unwrap()
2020-12-02 11:20:31 +08:00
.update_handshake(iface, now)?;
2020-11-23 17:16:07 +08:00
}
}
Ok(true)
}
}