hyper_simple_server/
connection.rs1
2
3use crate::prelude::*;
4
5
6
7
8#[ cfg (feature = "hss-accepter") ]
9pub enum Connection {
10 TcpStream (tokio::TcpStream, net::SocketAddr),
11 #[ cfg (feature = "hss-tls-rust") ]
12 RustTlsTcpStreamPending (tokio_rustls::Accept<tokio::TcpStream>, net::SocketAddr),
13 #[ cfg (feature = "hss-tls-rust") ]
14 RustTlsTcpStream (tokio_rustls::server::TlsStream<tokio::TcpStream>, net::SocketAddr),
15 #[ cfg (feature = "hss-tls-native") ]
16 NativeTlsTcpStreamPending (Arc<tokio_natls::TlsAcceptor>, Pin<Box<dyn Future<Output = Result<tokio_natls::TlsStream<tokio::TcpStream>, natls::Error>> + Send + 'static>>, net::SocketAddr),
17 #[ cfg (feature = "hss-tls-native") ]
18 NativeTlsTcpStream (tokio_natls::TlsStream<tokio::TcpStream>, net::SocketAddr),
19}
20
21
22
23
24#[ cfg (feature = "hss-accepter") ]
25impl Connection {
26
27 fn poll_stream (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<ServerResult<Pin<&mut dyn AsyncStream>>> {
28
29 let _self = Pin::into_inner (self);
30
31 match _self {
32
33 Connection::TcpStream (_stream, _) =>
34 Poll::Ready (Ok (Pin::new (_stream))),
35
36 #[ cfg (feature = "hss-tls-rust") ]
37 Connection::RustTlsTcpStreamPending (_accepter, _address) =>
38 match futures::ready! (Pin::new (_accepter) .poll (_context)) {
39 Ok (_stream) => {
40 *_self = Connection::RustTlsTcpStream (_stream, *_address);
41 Self::poll_stream (Pin::new (_self), _context)
42 }
43 Err (_error) =>
44 Poll::Ready (Err (_error)),
45 }
46
47 #[ cfg (feature = "hss-tls-rust") ]
48 Connection::RustTlsTcpStream (_stream, _) =>
49 Poll::Ready (Ok (Pin::new (_stream))),
50
51 #[ cfg (feature = "hss-tls-native") ]
52 Connection::NativeTlsTcpStreamPending (_tls, _accepter, _address) =>
53 match futures::ready! (_accepter.as_mut () .poll (_context)) {
54 Ok (_stream) => {
55 *_self = Connection::NativeTlsTcpStream (_stream, *_address);
56 Self::poll_stream (Pin::new (_self), _context)
57 }
58 Err (_error) =>
59 Poll::Ready (Err (_error.wrap (0xba9facee))),
60 }
61
62 #[ cfg (feature = "hss-tls-native") ]
63 Connection::NativeTlsTcpStream (_stream, _) =>
64 Poll::Ready (Ok (Pin::new (_stream))),
65 }
66 }
67}
68
69
70#[ cfg (feature = "hss-accepter") ]
71trait AsyncStream : tokio::AsyncRead + tokio::AsyncWrite + Unpin {}
72
73#[ cfg (feature = "hss-accepter") ]
74impl <S : tokio::AsyncRead + tokio::AsyncWrite + Unpin> AsyncStream for S {}
75
76
77
78
79#[ cfg (feature = "hss-accepter") ]
80impl tokio::AsyncRead for Connection {
81
82 fn poll_read (self : Pin<&mut Self>, _context : &mut Context<'_>, _buffer : &mut tokio::ReadBuf<'_>) -> Poll<ServerResult> {
83 match futures::ready! (self.poll_stream (_context)) {
84 Ok (_stream) =>
85 _stream.poll_read (_context, _buffer),
86 Err (_error) =>
87 Poll::Ready (Err (_error)),
88 }
89 }
90}
91
92
93#[ cfg (feature = "hss-accepter") ]
94impl tokio::AsyncWrite for Connection {
95
96 fn poll_write (self : Pin<&mut Self>, _context : &mut Context<'_>, _buffer : &[u8]) -> Poll<ServerResult<usize>> {
97 match futures::ready! (self.poll_stream (_context)) {
98 Ok (_stream) =>
99 _stream.poll_write (_context, _buffer),
100 Err (_error) =>
101 Poll::Ready (Err (_error)),
102 }
103 }
104
105 fn poll_flush (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<ServerResult> {
106 match futures::ready! (self.poll_stream (_context)) {
107 Ok (_stream) =>
108 _stream.poll_flush (_context),
109 Err (_error) =>
110 Poll::Ready (Err (_error)),
111 }
112 }
113
114 fn poll_shutdown (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<ServerResult> {
115 match futures::ready! (self.poll_stream (_context)) {
116 Ok (_stream) =>
117 _stream.poll_shutdown (_context),
118 Err (_error) =>
119 Poll::Ready (Err (_error)),
120 }
121 }
122}
123