forked from M-Labs/artiq
runtime: remove UDP-related code.
This commit is contained in:
parent
cdba330845
commit
6bdeddd201
|
@ -39,4 +39,4 @@ features = ["alloc"]
|
|||
git = "https://github.com/m-labs/smoltcp"
|
||||
rev = "f64a99a"
|
||||
default-features = false
|
||||
features = ["alloc", "log", "proto-udp", "proto-tcp"]
|
||||
features = ["alloc", "log", "proto-tcp"]
|
||||
|
|
|
@ -253,80 +253,6 @@ use ::smoltcp::Error as ErrorLower;
|
|||
// https://github.com/rust-lang/rust/issues/44057
|
||||
// type ErrorLower = ::smoltcp::Error;
|
||||
|
||||
type UdpPacketBuffer = ::smoltcp::socket::UdpPacketBuffer<'static>;
|
||||
type UdpSocketBuffer = ::smoltcp::socket::UdpSocketBuffer<'static, 'static>;
|
||||
type UdpSocketLower = ::smoltcp::socket::UdpSocket<'static, 'static>;
|
||||
|
||||
pub struct UdpSocket<'a> {
|
||||
io: &'a Io<'a>,
|
||||
handle: SocketHandle
|
||||
}
|
||||
|
||||
impl<'a> UdpSocket<'a> {
|
||||
pub fn new(io: &'a Io<'a>, buffer_depth: usize, buffer_width: usize) -> UdpSocket<'a> {
|
||||
let mut rx_buffer = vec![];
|
||||
let mut tx_buffer = vec![];
|
||||
for _ in 0..buffer_depth {
|
||||
rx_buffer.push(UdpPacketBuffer::new(vec![0; buffer_width]));
|
||||
tx_buffer.push(UdpPacketBuffer::new(vec![0; buffer_width]));
|
||||
}
|
||||
let handle = borrow_mut!(io.sockets)
|
||||
.add(UdpSocketLower::new(
|
||||
UdpSocketBuffer::new(rx_buffer),
|
||||
UdpSocketBuffer::new(tx_buffer)));
|
||||
UdpSocket {
|
||||
io: io,
|
||||
handle: handle
|
||||
}
|
||||
}
|
||||
|
||||
fn with_lower<F, R>(&self, f: F) -> R
|
||||
where F: FnOnce(SocketRef<UdpSocketLower>) -> R {
|
||||
let mut sockets = borrow_mut!(self.io.sockets);
|
||||
let result = f(sockets.get(self.handle));
|
||||
result
|
||||
}
|
||||
|
||||
pub fn bind<T: Into<IpEndpoint>>(&self, endpoint: T) -> Result<()> {
|
||||
self.with_lower(|mut s| s.bind(endpoint))
|
||||
.map_err(|err| {
|
||||
match err {
|
||||
ErrorLower::Illegal =>
|
||||
Error::new(ErrorKind::Other, "already listening"),
|
||||
ErrorLower::Unaddressable =>
|
||||
Error::new(ErrorKind::AddrNotAvailable, "port cannot be zero"),
|
||||
_ => unreachable!()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint)> {
|
||||
until!(self, UdpSocketLower, |s| s.can_recv())?;
|
||||
self.with_lower(|mut s| s.recv_slice(buf))
|
||||
.map_err(|_| unreachable!())
|
||||
}
|
||||
|
||||
pub fn send_to(&self, buf: &[u8], addr: IpEndpoint) -> Result<()> {
|
||||
until!(self, UdpSocketLower, |s| s.can_send())?;
|
||||
self.with_lower(|mut s| s.send_slice(buf, addr))
|
||||
.map_err(|err| {
|
||||
match err {
|
||||
ErrorLower::Unaddressable =>
|
||||
Error::new(ErrorKind::AddrNotAvailable, "unaddressable destination"),
|
||||
ErrorLower::Truncated =>
|
||||
Error::new(ErrorKind::Other, "packet does not fit in buffer"),
|
||||
_ => unreachable!()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for UdpSocket<'a> {
|
||||
fn drop(&mut self) {
|
||||
borrow_mut!(self.io.sockets).release(self.handle)
|
||||
}
|
||||
}
|
||||
|
||||
type TcpSocketBuffer = ::smoltcp::socket::TcpSocketBuffer<'static>;
|
||||
type TcpSocketLower = ::smoltcp::socket::TcpSocket<'static>;
|
||||
|
||||
|
|
Loading…
Reference in New Issue