Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. pub struct AcceptTls {
  2. stream: Option<TcpStream>,
  3. tls_acceptor: TlsAcceptor,
  4. client_logger: slog::Logger,
  5. }
  6.  
  7. impl AcceptTls {
  8. pub fn new(stream: TcpStream, tls_acceptor: TlsAcceptor, client_logger: slog::Logger) -> Self {
  9. Self {
  10. stream: Some(stream),
  11. tls_acceptor,
  12. client_logger,
  13. }
  14. }
  15. }
  16.  
  17. impl Future for AcceptTls {
  18. type Item = TlsStream<TcpStream>;
  19. type Error = io::Error;
  20.  
  21. fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
  22. let tls_stream = try_ready!(self
  23. .tls_acceptor
  24. .accept(
  25. self.stream
  26. .take()
  27. .expect("The stream must exist before accepting the TLS connection")
  28. )
  29. .map_err(|e| {
  30. io::Error::new(
  31. io::ErrorKind::ConnectionRefused,
  32. format!("Failed to accept a TLS connection: {}", e),
  33. )
  34. })
  35. .poll());
  36.  
  37. info!(self.client_logger, "TLS connection has been accepted");
  38.  
  39. Ok(Async::Ready(tls_stream))
  40. }
  41. }
  42.  
  43. pub fn establish_tls_connection_with_client<S>(
  44. client: S,
  45. tls_acceptor: TlsAcceptor,
  46. client_logger: slog::Logger,
  47. ) -> impl Future<Item = TlsStream<S>, Error = io::Error> + Send
  48. where
  49. S: AsyncRead + AsyncWrite + Send,
  50. {
  51. tls_acceptor
  52. .accept(client)
  53. .map_err(move |e| {
  54. io::Error::new(
  55. io::ErrorKind::ConnectionRefused,
  56. format!("failed to accept a client connection: {}", e),
  57. )
  58. })
  59. .and_then(move |client_tls| {
  60. info!(client_logger, "TLS connection has been accepted");
  61.  
  62. Ok(client_tls)
  63. })
  64. }
  65.  
  66. fn main() {
  67. // AcceptTls::new(...).and_then(...) FAIL
  68. // establish_tls_connection_with_client(...).and_then(...) OKcf
  69.  
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement