123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- use Result;
- use phy::DeviceLimits;
- use wire::IpRepr;
- mod raw;
- mod udp;
- mod tcp;
- mod set;
- pub use self::raw::PacketBuffer as RawPacketBuffer;
- pub use self::raw::SocketBuffer as RawSocketBuffer;
- pub use self::raw::RawSocket;
- pub use self::udp::PacketBuffer as UdpPacketBuffer;
- pub use self::udp::SocketBuffer as UdpSocketBuffer;
- pub use self::udp::UdpSocket;
- pub use self::tcp::SocketBuffer as TcpSocketBuffer;
- pub use self::tcp::State as TcpState;
- pub use self::tcp::TcpSocket;
- pub use self::set::{Set as SocketSet, Item as SocketSetItem, Handle as SocketHandle};
- pub use self::set::{Iter as SocketSetIter, IterMut as SocketSetIterMut};
- #[derive(Debug)]
- pub enum Socket<'a, 'b: 'a> {
- Raw(RawSocket<'a, 'b>),
- Udp(UdpSocket<'a, 'b>),
- Tcp(TcpSocket<'a>),
- #[doc(hidden)]
- __Nonexhaustive
- }
- macro_rules! dispatch_socket {
- ($self_:expr, |$socket:ident [$( $mut_:tt )*]| $code:expr) => ({
- match $self_ {
- &$( $mut_ )* Socket::Raw(ref $( $mut_ )* $socket) => $code,
- &$( $mut_ )* Socket::Udp(ref $( $mut_ )* $socket) => $code,
- &$( $mut_ )* Socket::Tcp(ref $( $mut_ )* $socket) => $code,
- &$( $mut_ )* Socket::__Nonexhaustive => unreachable!()
- }
- })
- }
- impl<'a, 'b> Socket<'a, 'b> {
-
- pub fn debug_id(&self) -> usize {
- dispatch_socket!(self, |socket []| socket.debug_id())
- }
-
-
-
-
- pub fn set_debug_id(&mut self, id: usize) {
- dispatch_socket!(self, |socket [mut]| socket.set_debug_id(id))
- }
- pub(crate) fn dispatch<F, R>(&mut self, timestamp: u64, limits: &DeviceLimits,
- emit: &mut F) -> Result<R>
- where F: FnMut(&IpRepr, &IpPayload) -> Result<R> {
- dispatch_socket!(self, |socket [mut]| socket.dispatch(timestamp, limits, emit))
- }
- }
- pub trait IpPayload {
-
- fn buffer_len(&self) -> usize;
-
- fn emit(&self, ip_repr: &IpRepr, payload: &mut [u8]);
- }
- pub trait AsSocket<T> {
- fn as_socket(&mut self) -> &mut T;
- fn try_as_socket(&mut self) -> Option<&mut T>;
- }
- macro_rules! as_socket {
- ($socket:ty, $variant:ident) => {
- impl<'a, 'b> AsSocket<$socket> for Socket<'a, 'b> {
- fn as_socket(&mut self) -> &mut $socket {
- match self {
- &mut Socket::$variant(ref mut socket) => socket,
- _ => panic!(concat!(".as_socket::<",
- stringify!($socket),
- "> called on wrong socket type"))
- }
- }
- fn try_as_socket(&mut self) -> Option<&mut $socket> {
- match self {
- &mut Socket::$variant(ref mut socket) => Some(socket),
- _ => None,
- }
- }
- }
- }
- }
- as_socket!(RawSocket<'a, 'b>, Raw);
- as_socket!(UdpSocket<'a, 'b>, Udp);
- as_socket!(TcpSocket<'a>, Tcp);
|