async_std_openssl/
tls_stream_wrapper.rs

1//! Stream Wrappers for SSL to extend support for some consuming Library use cases.
2
3use async_dup::{Arc, Mutex};
4use async_std::io::{Read, Result, Write};
5use async_std::net::TcpStream;
6use std::pin::Pin;
7use std::task::{Context, Poll};
8
9use crate::SslStream;
10
11// Ref: https://stackoverflow.com/questions/61643574/how-can-i-clone-an-opensslsslsslstream
12// "SSL / TLS logic contains state. All the clones need to agree on and update that state.
13// You will need to wrap it in an Arc<Mutex<_>> or equivalent and clone that."
14
15/// A wrapper for an SslStream that allows cloning and sending between multiple tasks/threads.
16/// In most cases you will not need this, but some consumers (such as tide listeners) rely
17/// on this behaviour.
18#[derive(Clone)]
19pub struct SslStreamWrapper(Arc<Mutex<SslStream<TcpStream>>>);
20
21impl SslStreamWrapper {
22    /// Wrap an SslStream in a clonable wrapper.
23    pub fn new(stream: SslStream<TcpStream>) -> Self {
24        Self(Arc::new(Mutex::new(stream)))
25    }
26}
27
28impl Read for SslStreamWrapper {
29    fn poll_read(
30        self: Pin<&mut Self>,
31        cx: &mut Context<'_>,
32        buf: &mut [u8],
33    ) -> Poll<Result<usize>> {
34        Pin::new(&mut &*self.0).poll_read(cx, buf)
35    }
36}
37
38impl Write for SslStreamWrapper {
39    fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
40        Pin::new(&mut &*self.0).poll_write(cx, buf)
41    }
42
43    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
44        Pin::new(&mut &*self.0).poll_flush(cx)
45    }
46
47    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
48        Pin::new(&mut &*self.0).poll_close(cx)
49    }
50}