actix_server_config/
lib.rs1use std::cell::Cell;
2use std::net::SocketAddr;
3use std::rc::Rc;
4use std::{fmt, io, net, time};
5
6use tokio_io::{AsyncRead, AsyncWrite};
7use tokio_tcp::TcpStream;
8
9#[derive(Debug, Clone)]
10pub struct ServerConfig {
11 addr: SocketAddr,
12 secure: Rc<Cell<bool>>,
13}
14
15impl ServerConfig {
16 pub fn new(addr: SocketAddr) -> Self {
17 ServerConfig {
18 addr,
19 secure: Rc::new(Cell::new(false)),
20 }
21 }
22
23 pub fn local_addr(&self) -> SocketAddr {
25 self.addr
26 }
27
28 pub fn secure(&self) -> bool {
30 self.secure.as_ref().get()
31 }
32
33 pub fn set_secure(&self) {
35 self.secure.as_ref().set(true)
36 }
37}
38
39#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
40pub enum Protocol {
41 Unknown,
42 Http10,
43 Http11,
44 Http2,
45 Proto1,
46 Proto2,
47 Proto3,
48 Proto4,
49 Proto5,
50 Proto6,
51}
52
53pub struct Io<T, P = ()> {
54 io: T,
55 proto: Protocol,
56 params: P,
57}
58
59impl<T> Io<T, ()> {
60 pub fn new(io: T) -> Self {
61 Self {
62 io,
63 proto: Protocol::Unknown,
64 params: (),
65 }
66 }
67}
68
69impl<T, P> Io<T, P> {
70 pub fn from_parts(io: T, params: P, proto: Protocol) -> Self {
72 Self { io, params, proto }
73 }
74
75 pub fn into_parts(self) -> (T, P, Protocol) {
77 (self.io, self.params, self.proto)
78 }
79
80 pub fn get_ref(&self) -> &T {
82 &self.io
83 }
84
85 pub fn get_mut(&mut self) -> &mut T {
87 &mut self.io
88 }
89
90 pub fn protocol(&self) -> Protocol {
92 self.proto
93 }
94
95 pub fn set<U>(self, params: U) -> Io<T, U> {
97 Io {
98 params,
99 io: self.io,
100 proto: self.proto,
101 }
102 }
103
104 pub fn map<U, F>(self, op: F) -> Io<T, U>
106 where
107 F: FnOnce(P) -> U,
108 {
109 Io {
110 io: self.io,
111 proto: self.proto,
112 params: op(self.params),
113 }
114 }
115}
116
117impl<T, P> std::ops::Deref for Io<T, P> {
118 type Target = T;
119
120 fn deref(&self) -> &T {
121 &self.io
122 }
123}
124
125impl<T, P> std::ops::DerefMut for Io<T, P> {
126 fn deref_mut(&mut self) -> &mut T {
127 &mut self.io
128 }
129}
130
131impl<T: fmt::Debug, P> fmt::Debug for Io<T, P> {
132 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
133 write!(f, "Io {{{:?}}}", self.io)
134 }
135}
136
137pub trait IoStream: AsyncRead + AsyncWrite {
139 fn peer_addr(&self) -> Option<SocketAddr> {
141 None
142 }
143
144 fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()>;
146
147 fn set_linger(&mut self, dur: Option<time::Duration>) -> io::Result<()>;
148
149 fn set_keepalive(&mut self, dur: Option<time::Duration>) -> io::Result<()>;
150}
151
152impl IoStream for TcpStream {
153 #[inline]
154 fn peer_addr(&self) -> Option<net::SocketAddr> {
155 TcpStream::peer_addr(self).ok()
156 }
157
158 #[inline]
159 fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()> {
160 TcpStream::set_nodelay(self, nodelay)
161 }
162
163 #[inline]
164 fn set_linger(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
165 TcpStream::set_linger(self, dur)
166 }
167
168 #[inline]
169 fn set_keepalive(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
170 TcpStream::set_keepalive(self, dur)
171 }
172}
173
174#[cfg(any(feature = "ssl"))]
175impl<T: IoStream> IoStream for tokio_openssl::SslStream<T> {
176 #[inline]
177 fn peer_addr(&self) -> Option<net::SocketAddr> {
178 self.get_ref().get_ref().peer_addr()
179 }
180
181 #[inline]
182 fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()> {
183 self.get_mut().get_mut().set_nodelay(nodelay)
184 }
185
186 #[inline]
187 fn set_linger(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
188 self.get_mut().get_mut().set_linger(dur)
189 }
190
191 #[inline]
192 fn set_keepalive(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
193 self.get_mut().get_mut().set_keepalive(dur)
194 }
195}
196
197#[cfg(any(feature = "rust-tls"))]
198impl<T: IoStream> IoStream for tokio_rustls::server::TlsStream<T> {
199 #[inline]
200 fn peer_addr(&self) -> Option<net::SocketAddr> {
201 self.get_ref().0.peer_addr()
202 }
203
204 #[inline]
205 fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()> {
206 self.get_mut().0.set_nodelay(nodelay)
207 }
208
209 #[inline]
210 fn set_linger(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
211 self.get_mut().0.set_linger(dur)
212 }
213
214 #[inline]
215 fn set_keepalive(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
216 self.get_mut().0.set_keepalive(dur)
217 }
218}
219
220#[cfg(all(unix, feature = "uds"))]
221impl IoStream for tokio_uds::UnixStream {
222 #[inline]
223 fn peer_addr(&self) -> Option<net::SocketAddr> {
224 None
225 }
226
227 #[inline]
228 fn set_nodelay(&mut self, _: bool) -> io::Result<()> {
229 Ok(())
230 }
231
232 #[inline]
233 fn set_linger(&mut self, _: Option<time::Duration>) -> io::Result<()> {
234 Ok(())
235 }
236
237 #[inline]
238 fn set_keepalive(&mut self, _: Option<time::Duration>) -> io::Result<()> {
239 Ok(())
240 }
241}