123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- use Error;
- use wire::IpRepr;
- mod udp;
- mod tcp;
- 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 enum Socket<'a, 'b: 'a> {
- Udp(UdpSocket<'a, 'b>),
- Tcp(TcpSocket<'a>),
- #[doc(hidden)]
- __Nonexhaustive
- }
- impl<'a, 'b> Socket<'a, 'b> {
-
-
-
-
-
-
-
- pub fn process(&mut self, ip_repr: &IpRepr, payload: &[u8]) -> Result<(), Error> {
- match self {
- &mut Socket::Udp(ref mut socket) =>
- socket.process(ip_repr, payload),
- &mut Socket::Tcp(ref mut socket) =>
- socket.process(ip_repr, payload),
- &mut Socket::__Nonexhaustive => unreachable!()
- }
- }
-
-
-
-
-
-
-
- pub fn dispatch<F, R>(&mut self, emit: &mut F) -> Result<R, Error>
- where F: FnMut(&IpRepr, &IpPayload) -> Result<R, Error> {
- match self {
- &mut Socket::Udp(ref mut socket) =>
- socket.dispatch(emit),
- &mut Socket::Tcp(ref mut socket) =>
- socket.dispatch(emit),
- &mut Socket::__Nonexhaustive => unreachable!()
- }
- }
- }
- 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;
- }
- impl<'a, 'b> AsSocket<UdpSocket<'a, 'b>> for Socket<'a, 'b> {
- fn as_socket(&mut self) -> &mut UdpSocket<'a, 'b> {
- match self {
- &mut Socket::Udp(ref mut socket) => socket,
- _ => panic!(".as_socket::<UdpSocket> called on wrong socket type")
- }
- }
- }
- impl<'a, 'b> AsSocket<TcpSocket<'a>> for Socket<'a, 'b> {
- fn as_socket(&mut self) -> &mut TcpSocket<'a> {
- match self {
- &mut Socket::Tcp(ref mut socket) => socket,
- _ => panic!(".as_socket::<TcpSocket> called on wrong socket type")
- }
- }
- }
|