Box<dyn ReadWrite> for TlsConnector::connect
This commit is contained in:
@@ -31,21 +31,28 @@ struct PassThrough {
|
||||
}
|
||||
|
||||
impl TlsConnector for PassThrough {
|
||||
fn connect(&self, _dns_name: &str, tcp_stream: TcpStream) -> Result<Box<dyn ReadWrite>, Error> {
|
||||
fn connect(
|
||||
&self,
|
||||
_dns_name: &str,
|
||||
io: Box<dyn ReadWrite>,
|
||||
) -> Result<Box<dyn ReadWrite>, Error> {
|
||||
if self.handshake_fail {
|
||||
let io_err = io::Error::new(io::ErrorKind::InvalidData, PassThroughError);
|
||||
return Err(io_err.into());
|
||||
}
|
||||
|
||||
Ok(Box::new(CustomTlsStream(tcp_stream)))
|
||||
Ok(Box::new(CustomTlsStream(io)))
|
||||
}
|
||||
}
|
||||
|
||||
struct CustomTlsStream(TcpStream);
|
||||
struct CustomTlsStream(Box<dyn ReadWrite>);
|
||||
|
||||
impl ReadWrite for CustomTlsStream {
|
||||
fn socket(&self) -> Option<&TcpStream> {
|
||||
Some(&self.0)
|
||||
self.0.socket()
|
||||
}
|
||||
fn is_poolable(&self) -> bool {
|
||||
self.0.is_poolable()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -53,9 +53,14 @@ impl MbedTlsConnector {
|
||||
}
|
||||
|
||||
impl TlsConnector for MbedTlsConnector {
|
||||
fn connect(&self, _dns_name: &str, tcp_stream: TcpStream) -> Result<Box<dyn ReadWrite>, Error> {
|
||||
fn connect(
|
||||
&self,
|
||||
_dns_name: &str,
|
||||
io: Box<dyn ReadWrite>,
|
||||
) -> Result<Box<dyn ReadWrite>, Error> {
|
||||
let mut ctx = self.context.lock().unwrap();
|
||||
match ctx.establish(tcp_stream, None) {
|
||||
let sync = SyncIo(Mutex::new(io));
|
||||
match ctx.establish(sync, None) {
|
||||
Err(_) => {
|
||||
let io_err = io::Error::new(io::ErrorKind::InvalidData, MbedTlsError);
|
||||
return Err(io_err.into());
|
||||
@@ -65,6 +70,28 @@ impl TlsConnector for MbedTlsConnector {
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal wrapper to make Box<dyn ReadWrite> implement Sync
|
||||
struct SyncIo(Mutex<Box<dyn ReadWrite>>);
|
||||
|
||||
impl io::Read for SyncIo {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
let mut lock = self.0.lock().unwrap();
|
||||
lock.read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl io::Write for SyncIo {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
let mut lock = self.0.lock().unwrap();
|
||||
lock.write(buf)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
let mut lock = self.0.lock().unwrap();
|
||||
lock.flush()
|
||||
}
|
||||
}
|
||||
|
||||
struct MbedTlsStream {
|
||||
context: Arc<Mutex<Context>>, //tcp_stream: TcpStream,
|
||||
}
|
||||
@@ -84,6 +111,9 @@ impl ReadWrite for MbedTlsStream {
|
||||
fn socket(&self) -> Option<&TcpStream> {
|
||||
None
|
||||
}
|
||||
fn is_poolable(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl io::Read for MbedTlsStream {
|
||||
|
||||
Reference in New Issue
Block a user