Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub struct AcceptTls {
- stream: Option<TcpStream>,
- tls_acceptor: TlsAcceptor,
- client_logger: slog::Logger,
- }
- impl AcceptTls {
- pub fn new(stream: TcpStream, tls_acceptor: TlsAcceptor, client_logger: slog::Logger) -> Self {
- Self {
- stream: Some(stream),
- tls_acceptor,
- client_logger,
- }
- }
- }
- impl Future for AcceptTls {
- type Item = TlsStream<TcpStream>;
- type Error = io::Error;
- fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
- let tls_stream = try_ready!(self
- .tls_acceptor
- .accept(
- self.stream
- .take()
- .expect("The stream must exist before accepting the TLS connection")
- )
- .map_err(|e| {
- io::Error::new(
- io::ErrorKind::ConnectionRefused,
- format!("Failed to accept a TLS connection: {}", e),
- )
- })
- .poll());
- info!(self.client_logger, "TLS connection has been accepted");
- Ok(Async::Ready(tls_stream))
- }
- }
- pub fn establish_tls_connection_with_client<S>(
- client: S,
- tls_acceptor: TlsAcceptor,
- client_logger: slog::Logger,
- ) -> impl Future<Item = TlsStream<S>, Error = io::Error> + Send
- where
- S: AsyncRead + AsyncWrite + Send,
- {
- tls_acceptor
- .accept(client)
- .map_err(move |e| {
- io::Error::new(
- io::ErrorKind::ConnectionRefused,
- format!("failed to accept a client connection: {}", e),
- )
- })
- .and_then(move |client_tls| {
- info!(client_logger, "TLS connection has been accepted");
- Ok(client_tls)
- })
- }
- fn main() {
- // AcceptTls::new(...).and_then(...) FAIL
- // establish_tls_connection_with_client(...).and_then(...) OKcf
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement