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
use crate::{
io::{rx, tx},
path::{self, MaxMtu},
time::{Clock, Timestamp},
};
use core::{
fmt,
future::Future,
task::{Context, Poll},
};
pub mod limits;
pub use limits::Limiter;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Type {
Client,
Server,
}
impl Type {
pub fn is_client(self) -> bool {
self == Self::Client
}
pub fn is_server(self) -> bool {
self == Self::Server
}
#[must_use]
pub fn peer_type(self) -> Self {
match self {
Self::Client => Self::Server,
Self::Server => Self::Client,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Location {
Local,
Remote,
}
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Local => write!(f, "the local endpoint"),
Self::Remote => write!(f, "the remote endpoint"),
}
}
}
impl Location {
pub fn is_local(self) -> bool {
self == Self::Local
}
pub fn is_remote(self) -> bool {
self == Self::Remote
}
#[must_use]
pub fn peer_type(self) -> Self {
match self {
Self::Local => Self::Remote,
Self::Remote => Self::Local,
}
}
}
pub trait Endpoint: 'static + Send + Sized {
type PathHandle: path::Handle;
type Subscriber: crate::event::Subscriber;
const ENDPOINT_TYPE: Type;
fn receive<Rx, C>(&mut self, rx: &mut Rx, clock: &C)
where
Rx: rx::Queue<Handle = Self::PathHandle>,
C: Clock;
fn transmit<Tx, C>(&mut self, tx: &mut Tx, clock: &C)
where
Tx: tx::Queue<Handle = Self::PathHandle>,
C: Clock;
fn wakeups<'a, C: Clock>(&'a mut self, clock: &'a C) -> Wakeups<'a, Self, C> {
Wakeups {
endpoint: self,
clock,
}
}
fn poll_wakeups<C: Clock>(
&mut self,
cx: &mut Context<'_>,
clock: &C,
) -> Poll<Result<usize, CloseError>>;
fn timeout(&self) -> Option<Timestamp>;
fn set_max_mtu(&mut self, max_mtu: MaxMtu);
fn subscriber(&mut self) -> &mut Self::Subscriber;
}
pub struct Wakeups<'a, E: Endpoint, C: Clock> {
endpoint: &'a mut E,
clock: &'a C,
}
impl<'a, E: Endpoint, C: Clock> Future for Wakeups<'a, E, C> {
type Output = Result<usize, CloseError>;
fn poll(mut self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let clock = self.clock;
self.endpoint.poll_wakeups(cx, clock)
}
}
#[derive(Clone, Copy, Debug)]
pub struct CloseError;