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


use crate::prelude::*;




#[ cfg (feature = "hss-accepter") ]
pub enum Connection {
	TcpStream (tokio::TcpStream, net::SocketAddr),
	#[ cfg (feature = "hss-tls-rust") ]
	RustTlsTcpStreamPending (tokio_rustls::Accept<tokio::TcpStream>, net::SocketAddr),
	#[ cfg (feature = "hss-tls-rust") ]
	RustTlsTcpStream (tokio_rustls::server::TlsStream<tokio::TcpStream>, net::SocketAddr),
	#[ cfg (feature = "hss-tls-native") ]
	NativeTlsTcpStreamPending (Arc<tokio_natls::TlsAcceptor>, Pin<Box<dyn Future<Output = Result<tokio_natls::TlsStream<tokio::TcpStream>, natls::Error>> + Send + 'static>>, net::SocketAddr),
	#[ cfg (feature = "hss-tls-native") ]
	NativeTlsTcpStream (tokio_natls::TlsStream<tokio::TcpStream>, net::SocketAddr),
}




#[ cfg (feature = "hss-accepter") ]
impl Connection {
	
	fn poll_stream (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<ServerResult<Pin<&mut dyn AsyncStream>>> {
		
		let _self = Pin::into_inner (self);
		
		match _self {
			
			Connection::TcpStream (_stream, _) =>
				Poll::Ready (Ok (Pin::new (_stream))),
			
			#[ cfg (feature = "hss-tls-rust") ]
			Connection::RustTlsTcpStreamPending (_accepter, _address) =>
				match futures::ready! (Pin::new (_accepter) .poll (_context)) {
					Ok (_stream) => {
						*_self = Connection::RustTlsTcpStream (_stream, *_address);
						Self::poll_stream (Pin::new (_self), _context)
					}
					Err (_error) =>
						Poll::Ready (Err (_error)),
				}
			
			#[ cfg (feature = "hss-tls-rust") ]
			Connection::RustTlsTcpStream (_stream, _) =>
				Poll::Ready (Ok (Pin::new (_stream))),
			
			#[ cfg (feature = "hss-tls-native") ]
			Connection::NativeTlsTcpStreamPending (_tls, _accepter, _address) =>
				match futures::ready! (_accepter.as_mut () .poll (_context)) {
					Ok (_stream) => {
						*_self = Connection::NativeTlsTcpStream (_stream, *_address);
						Self::poll_stream (Pin::new (_self), _context)
					}
					Err (_error) =>
						Poll::Ready (Err (_error.wrap (0xba9facee))),
				}
			
			#[ cfg (feature = "hss-tls-native") ]
			Connection::NativeTlsTcpStream (_stream, _) =>
				Poll::Ready (Ok (Pin::new (_stream))),
		}
	}
}


#[ cfg (feature = "hss-accepter") ]
trait AsyncStream : tokio::AsyncRead + tokio::AsyncWrite + Unpin {}

#[ cfg (feature = "hss-accepter") ]
impl <S : tokio::AsyncRead + tokio::AsyncWrite + Unpin> AsyncStream for S {}




#[ cfg (feature = "hss-accepter") ]
impl tokio::AsyncRead for Connection {
	
	fn poll_read (self : Pin<&mut Self>, _context : &mut Context<'_>, _buffer : &mut tokio::ReadBuf<'_>) -> Poll<ServerResult> {
		match futures::ready! (self.poll_stream (_context)) {
			Ok (_stream) =>
				_stream.poll_read (_context, _buffer),
			Err (_error) =>
				Poll::Ready (Err (_error)),
		}
	}
}


#[ cfg (feature = "hss-accepter") ]
impl tokio::AsyncWrite for Connection {
	
	fn poll_write (self : Pin<&mut Self>, _context : &mut Context<'_>, _buffer : &[u8]) -> Poll<ServerResult<usize>> {
		match futures::ready! (self.poll_stream (_context)) {
			Ok (_stream) =>
				_stream.poll_write (_context, _buffer),
			Err (_error) =>
				Poll::Ready (Err (_error)),
		}
	}
	
	fn poll_flush (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<ServerResult> {
		match futures::ready! (self.poll_stream (_context)) {
			Ok (_stream) =>
				_stream.poll_flush (_context),
			Err (_error) =>
				Poll::Ready (Err (_error)),
		}
	}
	
	fn poll_shutdown (self : Pin<&mut Self>, _context : &mut Context<'_>) -> Poll<ServerResult> {
		match futures::ready! (self.poll_stream (_context)) {
			Ok (_stream) =>
				_stream.poll_shutdown (_context),
			Err (_error) =>
				Poll::Ready (Err (_error)),
		}
	}
}