|
@@ -1,50 +1,46 @@
|
|
|
-use core::marker::PhantomData;
|
|
|
use core::borrow::BorrowMut;
|
|
|
|
|
|
use Error;
|
|
|
+use Managed;
|
|
|
use wire::{InternetAddress as Address, InternetProtocolType as ProtocolType};
|
|
|
use wire::{InternetEndpoint as Endpoint};
|
|
|
use wire::{UdpPacket, UdpRepr};
|
|
|
use socket::{Socket, PacketRepr};
|
|
|
|
|
|
/// A buffered UDP packet.
|
|
|
-#[derive(Debug, Default)]
|
|
|
-pub struct BufferElem<T: BorrowMut<[u8]>> {
|
|
|
+#[derive(Debug)]
|
|
|
+pub struct BufferElem<'a> {
|
|
|
endpoint: Endpoint,
|
|
|
size: usize,
|
|
|
- payload: T
|
|
|
+ payload: Managed<'a, [u8]>
|
|
|
}
|
|
|
|
|
|
-impl<T: BorrowMut<[u8]>> BufferElem<T> {
|
|
|
+impl<'a> BufferElem<'a> {
|
|
|
/// Create a buffered packet.
|
|
|
- pub fn new(payload: T) -> BufferElem<T> {
|
|
|
+ pub fn new<T>(payload: T) -> BufferElem<'a>
|
|
|
+ where T: Into<Managed<'a, [u8]>> {
|
|
|
BufferElem {
|
|
|
endpoint: Endpoint::INVALID,
|
|
|
size: 0,
|
|
|
- payload: payload
|
|
|
+ payload: payload.into()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// An UDP packet buffer.
|
|
|
#[derive(Debug)]
|
|
|
-pub struct Buffer<
|
|
|
- T: BorrowMut<[u8]>,
|
|
|
- U: BorrowMut<[BufferElem<T>]>
|
|
|
-> {
|
|
|
- storage: U,
|
|
|
+pub struct Buffer<'a> {
|
|
|
+ storage: Managed<'a, [BufferElem<'a>]>,
|
|
|
read_at: usize,
|
|
|
- length: usize,
|
|
|
- phantom: PhantomData<T>
|
|
|
+ length: usize
|
|
|
}
|
|
|
|
|
|
-impl<
|
|
|
- T: BorrowMut<[u8]>,
|
|
|
- U: BorrowMut<[BufferElem<T>]>
|
|
|
-> Buffer<T, U> {
|
|
|
+impl<'a> Buffer<'a> {
|
|
|
/// Create a packet buffer with the given storage.
|
|
|
- pub fn new(mut storage: U) -> Buffer<T, U> {
|
|
|
- for elem in storage.borrow_mut() {
|
|
|
+ pub fn new<T>(storage: T) -> Buffer<'a>
|
|
|
+ where T: Into<Managed<'a, [BufferElem<'a>]>> {
|
|
|
+ let mut storage = storage.into();
|
|
|
+ for elem in storage.iter_mut() {
|
|
|
elem.endpoint = Default::default();
|
|
|
elem.size = 0;
|
|
|
}
|
|
@@ -52,13 +48,12 @@ impl<
|
|
|
Buffer {
|
|
|
storage: storage,
|
|
|
read_at: 0,
|
|
|
- length: 0,
|
|
|
- phantom: PhantomData
|
|
|
+ length: 0
|
|
|
}
|
|
|
}
|
|
|
|
|
|
fn mask(&self, index: usize) -> usize {
|
|
|
- index % self.storage.borrow().len()
|
|
|
+ index % self.storage.len()
|
|
|
}
|
|
|
|
|
|
fn incr(&self, index: usize) -> usize {
|
|
@@ -70,12 +65,12 @@ impl<
|
|
|
}
|
|
|
|
|
|
fn full(&self) -> bool {
|
|
|
- self.length == self.storage.borrow().len()
|
|
|
+ self.length == self.storage.len()
|
|
|
}
|
|
|
|
|
|
/// Enqueue an element into the buffer, and return a pointer to it, or return
|
|
|
/// `Err(Error::Exhausted)` if the buffer is full.
|
|
|
- pub fn enqueue(&mut self) -> Result<&mut BufferElem<T>, Error> {
|
|
|
+ pub fn enqueue(&mut self) -> Result<&mut BufferElem<'a>, Error> {
|
|
|
if self.full() {
|
|
|
Err(Error::Exhausted)
|
|
|
} else {
|
|
@@ -88,12 +83,12 @@ impl<
|
|
|
|
|
|
/// Dequeue an element from the buffer, and return a pointer to it, or return
|
|
|
/// `Err(Error::Exhausted)` if the buffer is empty.
|
|
|
- pub fn dequeue(&mut self) -> Result<&BufferElem<T>, Error> {
|
|
|
+ pub fn dequeue(&mut self) -> Result<&BufferElem<'a>, Error> {
|
|
|
if self.empty() {
|
|
|
Err(Error::Exhausted)
|
|
|
} else {
|
|
|
self.length -= 1;
|
|
|
- let result = &self.storage.borrow()[self.read_at];
|
|
|
+ let result = &self.storage[self.read_at];
|
|
|
self.read_at = self.incr(self.read_at);
|
|
|
Ok(result)
|
|
|
}
|
|
@@ -104,22 +99,16 @@ impl<
|
|
|
///
|
|
|
/// An UDP socket is bound to a specific endpoint, and owns transmit and receive
|
|
|
/// packet buffers.
|
|
|
-pub struct UdpSocket<
|
|
|
- T: BorrowMut<[u8]>,
|
|
|
- U: BorrowMut<[BufferElem<T>]>
|
|
|
-> {
|
|
|
+pub struct UdpSocket<'a> {
|
|
|
endpoint: Endpoint,
|
|
|
- rx_buffer: Buffer<T, U>,
|
|
|
- tx_buffer: Buffer<T, U>
|
|
|
+ rx_buffer: Buffer<'a>,
|
|
|
+ tx_buffer: Buffer<'a>
|
|
|
}
|
|
|
|
|
|
-impl<
|
|
|
- T: BorrowMut<[u8]>,
|
|
|
- U: BorrowMut<[BufferElem<T>]>
|
|
|
-> UdpSocket<T, U> {
|
|
|
+impl<'a> UdpSocket<'a> {
|
|
|
/// Create an UDP socket with the given buffers.
|
|
|
- pub fn new(endpoint: Endpoint, rx_buffer: Buffer<T, U>, tx_buffer: Buffer<T, U>)
|
|
|
- -> UdpSocket<T, U> {
|
|
|
+ pub fn new(endpoint: Endpoint, rx_buffer: Buffer<'a>, tx_buffer: Buffer<'a>)
|
|
|
+ -> UdpSocket<'a> {
|
|
|
UdpSocket {
|
|
|
endpoint: endpoint,
|
|
|
rx_buffer: rx_buffer,
|
|
@@ -145,14 +134,11 @@ impl<
|
|
|
/// This function returns `Err(Error::Exhausted)` if the receive buffer is empty.
|
|
|
pub fn recv<R, F>(&mut self) -> Result<(Endpoint, &[u8]), Error> {
|
|
|
let packet_buf = try!(self.rx_buffer.dequeue());
|
|
|
- Ok((packet_buf.endpoint, &packet_buf.payload.borrow()[..packet_buf.size]))
|
|
|
+ Ok((packet_buf.endpoint, &packet_buf.payload[..packet_buf.size]))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl<
|
|
|
- T: BorrowMut<[u8]>,
|
|
|
- U: BorrowMut<[BufferElem<T>]>
|
|
|
-> Socket for UdpSocket<T, U> {
|
|
|
+impl<'a> Socket for UdpSocket<'a> {
|
|
|
fn collect(&mut self, src_addr: &Address, dst_addr: &Address,
|
|
|
protocol: ProtocolType, payload: &[u8])
|
|
|
-> Result<(), Error> {
|
|
@@ -183,7 +169,7 @@ impl<
|
|
|
&UdpRepr {
|
|
|
src_port: self.endpoint.port,
|
|
|
dst_port: packet_buf.endpoint.port,
|
|
|
- payload: packet_buf.payload.borrow()
|
|
|
+ payload: &packet_buf.payload[..]
|
|
|
})
|
|
|
}
|
|
|
}
|