2021-02-03 05:09:37 +01:00
|
|
|
use core::marker::PhantomData;
|
|
|
|
use core::mem;
|
2022-05-04 20:48:37 +02:00
|
|
|
use core::task::Poll;
|
2021-11-26 04:12:14 +01:00
|
|
|
use smoltcp::iface::{Context as SmolContext, SocketHandle};
|
2021-02-03 05:09:37 +01:00
|
|
|
use smoltcp::socket::TcpSocket as SyncTcpSocket;
|
|
|
|
use smoltcp::socket::{TcpSocketBuffer, TcpState};
|
|
|
|
use smoltcp::time::Duration;
|
|
|
|
use smoltcp::wire::IpEndpoint;
|
|
|
|
|
2022-05-04 20:48:37 +02:00
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
mod io_impl;
|
|
|
|
|
2021-02-03 05:09:37 +01:00
|
|
|
use super::stack::Stack;
|
2022-05-04 20:48:37 +02:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum Error {
|
|
|
|
ConnectionReset,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum ConnectError {
|
|
|
|
/// The socket is already connected or listening.
|
|
|
|
InvalidState,
|
|
|
|
/// The remote host rejected the connection with a RST packet.
|
|
|
|
ConnectionReset,
|
|
|
|
/// Connect timed out.
|
|
|
|
TimedOut,
|
|
|
|
/// No route to host.
|
|
|
|
NoRoute,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum AcceptError {
|
|
|
|
/// The socket is already connected or listening.
|
|
|
|
InvalidState,
|
|
|
|
/// Invalid listen port
|
|
|
|
InvalidPort,
|
|
|
|
/// The remote host rejected the connection with a RST packet.
|
|
|
|
ConnectionReset,
|
|
|
|
}
|
2021-02-03 05:09:37 +01:00
|
|
|
|
|
|
|
pub struct TcpSocket<'a> {
|
|
|
|
handle: SocketHandle,
|
|
|
|
ghost: PhantomData<&'a mut [u8]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Unpin for TcpSocket<'a> {}
|
|
|
|
|
2022-05-13 22:15:27 +02:00
|
|
|
pub struct TcpReader<'a> {
|
|
|
|
handle: SocketHandle,
|
|
|
|
ghost: PhantomData<&'a mut [u8]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Unpin for TcpReader<'a> {}
|
|
|
|
|
|
|
|
pub struct TcpWriter<'a> {
|
|
|
|
handle: SocketHandle,
|
|
|
|
ghost: PhantomData<&'a mut [u8]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Unpin for TcpWriter<'a> {}
|
|
|
|
|
2021-02-03 05:09:37 +01:00
|
|
|
impl<'a> TcpSocket<'a> {
|
|
|
|
pub fn new(rx_buffer: &'a mut [u8], tx_buffer: &'a mut [u8]) -> Self {
|
|
|
|
let handle = Stack::with(|stack| {
|
|
|
|
let rx_buffer: &'static mut [u8] = unsafe { mem::transmute(rx_buffer) };
|
|
|
|
let tx_buffer: &'static mut [u8] = unsafe { mem::transmute(tx_buffer) };
|
2021-11-26 04:12:14 +01:00
|
|
|
stack.iface.add_socket(SyncTcpSocket::new(
|
2021-02-03 05:09:37 +01:00
|
|
|
TcpSocketBuffer::new(rx_buffer),
|
|
|
|
TcpSocketBuffer::new(tx_buffer),
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
Self {
|
|
|
|
handle,
|
|
|
|
ghost: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 22:15:27 +02:00
|
|
|
pub fn split(&mut self) -> (TcpReader<'_>, TcpWriter<'_>) {
|
|
|
|
(
|
|
|
|
TcpReader {
|
|
|
|
handle: self.handle,
|
|
|
|
ghost: PhantomData,
|
|
|
|
},
|
|
|
|
TcpWriter {
|
|
|
|
handle: self.handle,
|
|
|
|
ghost: PhantomData,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-04 20:48:37 +02:00
|
|
|
pub async fn connect<T>(&mut self, remote_endpoint: T) -> Result<(), ConnectError>
|
2021-02-03 05:09:37 +01:00
|
|
|
where
|
|
|
|
T: Into<IpEndpoint>,
|
|
|
|
{
|
|
|
|
let local_port = Stack::with(|stack| stack.get_local_port());
|
2022-05-13 22:15:27 +02:00
|
|
|
match with_socket(self.handle, |s, cx| {
|
|
|
|
s.connect(cx, remote_endpoint, local_port)
|
|
|
|
}) {
|
2022-05-04 20:48:37 +02:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(smoltcp::Error::Illegal) => return Err(ConnectError::InvalidState),
|
|
|
|
Err(smoltcp::Error::Unaddressable) => return Err(ConnectError::NoRoute),
|
|
|
|
// smoltcp returns no errors other than the above.
|
|
|
|
Err(_) => unreachable!(),
|
|
|
|
}
|
2021-02-03 05:09:37 +01:00
|
|
|
|
|
|
|
futures::future::poll_fn(|cx| {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| match s.state() {
|
2022-05-04 20:48:37 +02:00
|
|
|
TcpState::Closed | TcpState::TimeWait => {
|
|
|
|
Poll::Ready(Err(ConnectError::ConnectionReset))
|
|
|
|
}
|
|
|
|
TcpState::Listen => unreachable!(),
|
2021-02-03 05:09:37 +01:00
|
|
|
TcpState::SynSent | TcpState::SynReceived => {
|
|
|
|
s.register_send_waker(cx.waker());
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
_ => Poll::Ready(Ok(())),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2022-05-04 20:48:37 +02:00
|
|
|
pub async fn accept<T>(&mut self, local_endpoint: T) -> Result<(), AcceptError>
|
2021-11-04 13:34:13 +01:00
|
|
|
where
|
|
|
|
T: Into<IpEndpoint>,
|
|
|
|
{
|
2022-05-13 22:15:27 +02:00
|
|
|
match with_socket(self.handle, |s, _| s.listen(local_endpoint)) {
|
2022-05-04 20:48:37 +02:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(smoltcp::Error::Illegal) => return Err(AcceptError::InvalidState),
|
|
|
|
Err(smoltcp::Error::Unaddressable) => return Err(AcceptError::InvalidPort),
|
|
|
|
// smoltcp returns no errors other than the above.
|
|
|
|
Err(_) => unreachable!(),
|
|
|
|
}
|
2021-11-04 13:34:13 +01:00
|
|
|
|
|
|
|
futures::future::poll_fn(|cx| {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| match s.state() {
|
2022-04-23 05:14:44 +02:00
|
|
|
TcpState::Listen | TcpState::SynSent | TcpState::SynReceived => {
|
2021-11-04 13:34:13 +01:00
|
|
|
s.register_send_waker(cx.waker());
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
_ => Poll::Ready(Ok(())),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2021-02-03 05:09:37 +01:00
|
|
|
pub fn set_timeout(&mut self, duration: Option<Duration>) {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| s.set_timeout(duration))
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_keep_alive(&mut self, interval: Option<Duration>) {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| s.set_keep_alive(interval))
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| s.set_hop_limit(hop_limit))
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn local_endpoint(&self) -> IpEndpoint {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| s.local_endpoint())
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remote_endpoint(&self) -> IpEndpoint {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| s.remote_endpoint())
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn state(&self) -> TcpState {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| s.state())
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(&mut self) {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| s.close())
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn abort(&mut self) {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| s.abort())
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn may_send(&self) -> bool {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| s.may_send())
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn may_recv(&self) -> bool {
|
2022-05-13 22:15:27 +02:00
|
|
|
with_socket(self.handle, |s, _| s.may_recv())
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
2022-05-13 22:15:27 +02:00
|
|
|
}
|
2021-02-03 05:09:37 +01:00
|
|
|
|
2022-05-13 22:15:27 +02:00
|
|
|
fn with_socket<R>(
|
|
|
|
handle: SocketHandle,
|
|
|
|
f: impl FnOnce(&mut SyncTcpSocket, &mut SmolContext) -> R,
|
|
|
|
) -> R {
|
|
|
|
Stack::with(|stack| {
|
|
|
|
let res = {
|
|
|
|
let (s, cx) = stack.iface.get_socket_and_context::<SyncTcpSocket>(handle);
|
|
|
|
f(s, cx)
|
|
|
|
};
|
|
|
|
stack.wake();
|
|
|
|
res
|
|
|
|
})
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Drop for TcpSocket<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
Stack::with(|stack| {
|
2021-11-26 04:12:14 +01:00
|
|
|
stack.iface.remove_socket(self.handle);
|
2021-02-03 05:09:37 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 20:48:37 +02:00
|
|
|
impl embedded_io::Error for Error {
|
|
|
|
fn kind(&self) -> embedded_io::ErrorKind {
|
|
|
|
embedded_io::ErrorKind::Other
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 20:48:37 +02:00
|
|
|
impl<'d> embedded_io::Io for TcpSocket<'d> {
|
|
|
|
type Error = Error;
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
2022-05-13 22:15:27 +02:00
|
|
|
|
|
|
|
impl<'d> embedded_io::Io for TcpReader<'d> {
|
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d> embedded_io::Io for TcpWriter<'d> {
|
|
|
|
type Error = Error;
|
|
|
|
}
|