1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::time::Instant;
mod meta;
#[cfg(feature = "socket-raw")]
mod raw;
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
mod icmp;
#[cfg(feature = "socket-udp")]
mod udp;
#[cfg(feature = "socket-tcp")]
mod tcp;
mod set;
mod ref_;
#[cfg(feature = "async")]
mod waker;
pub(crate) use self::meta::Meta as SocketMeta;
#[cfg(feature = "async")]
pub(crate) use self::waker::WakerRegistration;
#[cfg(feature = "socket-raw")]
pub use self::raw::{RawPacketMetadata,
RawSocketBuffer,
RawSocket};
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
pub use self::icmp::{IcmpPacketMetadata,
IcmpSocketBuffer,
Endpoint as IcmpEndpoint,
IcmpSocket};
#[cfg(feature = "socket-udp")]
pub use self::udp::{UdpPacketMetadata,
UdpSocketBuffer,
UdpSocket};
#[cfg(feature = "socket-tcp")]
pub use self::tcp::{SocketBuffer as TcpSocketBuffer,
State as TcpState,
TcpSocket};
pub use self::set::{Set as SocketSet, Item as SocketSetItem, Handle as SocketHandle};
pub use self::set::{Iter as SocketSetIter, IterMut as SocketSetIterMut};
pub use self::ref_::Ref as SocketRef;
pub(crate) use self::ref_::Session as SocketSession;
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy)]
pub(crate) enum PollAt {
Now,
Time(Instant),
Ingress,
}
#[derive(Debug)]
pub enum Socket<'a> {
#[cfg(feature = "socket-raw")]
Raw(RawSocket<'a>),
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
Icmp(IcmpSocket<'a>),
#[cfg(feature = "socket-udp")]
Udp(UdpSocket<'a>),
#[cfg(feature = "socket-tcp")]
Tcp(TcpSocket<'a>),
}
macro_rules! dispatch_socket {
($self_:expr, |$socket:ident| $code:expr) => {
dispatch_socket!(@inner $self_, |$socket| $code);
};
(mut $self_:expr, |$socket:ident| $code:expr) => {
dispatch_socket!(@inner mut $self_, |$socket| $code);
};
(@inner $( $mut_:ident )* $self_:expr, |$socket:ident| $code:expr) => {
match $self_ {
#[cfg(feature = "socket-raw")]
&$( $mut_ )* Socket::Raw(ref $( $mut_ )* $socket) => $code,
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
&$( $mut_ )* Socket::Icmp(ref $( $mut_ )* $socket) => $code,
#[cfg(feature = "socket-udp")]
&$( $mut_ )* Socket::Udp(ref $( $mut_ )* $socket) => $code,
#[cfg(feature = "socket-tcp")]
&$( $mut_ )* Socket::Tcp(ref $( $mut_ )* $socket) => $code,
}
};
}
impl<'a> Socket<'a> {
#[inline]
pub fn handle(&self) -> SocketHandle {
self.meta().handle
}
pub(crate) fn meta(&self) -> &SocketMeta {
dispatch_socket!(self, |socket| &socket.meta)
}
pub(crate) fn meta_mut(&mut self) -> &mut SocketMeta {
dispatch_socket!(mut self, |socket| &mut socket.meta)
}
pub(crate) fn poll_at(&self) -> PollAt {
dispatch_socket!(self, |socket| socket.poll_at())
}
}
impl<'a> SocketSession for Socket<'a> {
fn finish(&mut self) {
dispatch_socket!(mut self, |socket| socket.finish())
}
}
pub trait AnySocket<'a>: SocketSession + Sized {
fn downcast<'c>(socket_ref: SocketRef<'c, Socket<'a>>) ->
Option<SocketRef<'c, Self>>;
}
macro_rules! from_socket {
($socket:ty, $variant:ident) => {
impl<'a> AnySocket<'a> for $socket {
fn downcast<'c>(ref_: SocketRef<'c, Socket<'a>>) ->
Option<SocketRef<'c, Self>> {
if let Socket::$variant(ref mut socket) = SocketRef::into_inner(ref_) {
Some(SocketRef::new(socket))
} else {
None
}
}
}
}
}
#[cfg(feature = "socket-raw")]
from_socket!(RawSocket<'a>, Raw);
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
from_socket!(IcmpSocket<'a>, Icmp);
#[cfg(feature = "socket-udp")]
from_socket!(UdpSocket<'a>, Udp);
#[cfg(feature = "socket-tcp")]
from_socket!(TcpSocket<'a>, Tcp);