use std::io;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::net::Shutdown;
pub trait TransportStream: Read + Write + Sized {
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
let mut total = 0;
while total < buf.len() {
let read = try!(self.read(&mut buf[total..]));
if read == 0 {
return Err(io::Error::new(io::ErrorKind::Other,
"Not enough bytes"));
}
total += read;
}
Ok(())
}
fn try_split(&self) -> Result<Self, io::Error>;
fn close(&mut self) -> Result<(), io::Error>;
}
impl TransportStream for TcpStream {
fn try_split(&self) -> Result<TcpStream, io::Error> {
self.try_clone()
}
fn close(&mut self) -> Result<(), io::Error> {
self.shutdown(Shutdown::Both)
}
}
#[cfg(feature="tls")]
use openssl::ssl::SslStream;
#[cfg(feature="tls")]
impl TransportStream for SslStream<TcpStream> {
fn try_split(&self) -> Result<SslStream<TcpStream>, io::Error> {
self.try_clone()
}
fn close(&mut self) -> Result<(), io::Error> {
self.get_ref().shutdown(Shutdown::Both)
}
}