SslTransport::close and cleanups

This commit is contained in:
lumi 2017-02-19 02:19:37 +01:00
parent e5d549d1fe
commit b44ef9cc2c
2 changed files with 19 additions and 16 deletions

View file

@ -4,8 +4,6 @@ use std::convert::Into;
use std::str::FromStr; use std::str::FromStr;
use std::string::ToString;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum JidParseError { pub enum JidParseError {
NoDomain, NoDomain,
@ -43,7 +41,7 @@ impl FromStr for Jid {
fn from_str(s: &str) -> Result<Jid, JidParseError> { fn from_str(s: &str) -> Result<Jid, JidParseError> {
// TODO: very naive, may need to do it differently // TODO: very naive, may need to do it differently
let mut iter = s.chars(); let iter = s.chars();
let mut buf = String::new(); let mut buf = String::new();
let mut state = ParserState::Node; let mut state = ParserState::Node;
let mut node = None; let mut node = None;

View file

@ -1,7 +1,7 @@
use std::io::prelude::*; use std::io::prelude::*;
use std::io; use std::io;
use std::net::{SocketAddr, TcpStream}; use std::net::TcpStream;
use xml::reader::{EventReader, XmlEvent as XmlReaderEvent}; use xml::reader::{EventReader, XmlEvent as XmlReaderEvent};
use xml::writer::{EventWriter, XmlEvent as XmlWriterEvent}; use xml::writer::{EventWriter, XmlEvent as XmlWriterEvent};
@ -19,9 +19,9 @@ pub trait Transport {
fn read_event(&mut self) -> Result<XmlReaderEvent, Error>; fn read_event(&mut self) -> Result<XmlReaderEvent, Error>;
} }
struct LockedWrite<W: Write>(Arc<Mutex<W>>); struct LockedIO<T>(Arc<Mutex<T>>);
impl<W: Write> io::Write for LockedWrite<W> { impl<T: Write> io::Write for LockedIO<T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut inner = self.0.lock().unwrap(); // TODO: make safer let mut inner = self.0.lock().unwrap(); // TODO: make safer
inner.write(buf) inner.write(buf)
@ -33,9 +33,7 @@ impl<W: Write> io::Write for LockedWrite<W> {
} }
} }
struct LockedRead<R: Read>(Arc<Mutex<R>>); impl<T: Read> io::Read for LockedIO<T> {
impl<R: Read> io::Read for LockedRead<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let mut inner = self.0.lock().unwrap(); // TODO: make safer let mut inner = self.0.lock().unwrap(); // TODO: make safer
inner.read(buf) inner.read(buf)
@ -44,10 +42,10 @@ impl<R: Read> io::Read for LockedRead<R> {
pub struct SslTransport { pub struct SslTransport {
inner: Arc<Mutex<SslStream<TcpStream>>>, // TODO: this feels rather ugly inner: Arc<Mutex<SslStream<TcpStream>>>, // TODO: this feels rather ugly
reader: EventReader<LockedRead<SslStream<TcpStream>>>, // TODO: especially feels ugly because reader: EventReader<LockedIO<SslStream<TcpStream>>>, // TODO: especially feels ugly because
// this read would keep the lock // this read would keep the lock
// held very long (potentially) // held very long (potentially)
writer: EventWriter<LockedWrite<SslStream<TcpStream>>>, writer: EventWriter<LockedIO<SslStream<TcpStream>>>,
} }
impl Transport for SslTransport { impl Transport for SslTransport {
@ -71,7 +69,7 @@ impl SslTransport {
let mut parser = EventReader::new(stream); let mut parser = EventReader::new(stream);
loop { // TODO: possibly a timeout? loop { // TODO: possibly a timeout?
match parser.next()? { match parser.next()? {
XmlReaderEvent::StartElement { name, namespace, .. } => { XmlReaderEvent::StartElement { name, .. } => {
if let Some(ns) = name.namespace { if let Some(ns) = name.namespace {
if ns == ns::TLS && name.local_name == "proceed" { if ns == ns::TLS && name.local_name == "proceed" {
break; break;
@ -87,12 +85,19 @@ impl SslTransport {
let stream = parser.into_inner(); let stream = parser.into_inner();
let ssl_connector = SslConnectorBuilder::new(SslMethod::tls())?.build(); let ssl_connector = SslConnectorBuilder::new(SslMethod::tls())?.build();
let ssl_stream = Arc::new(Mutex::new(ssl_connector.connect(host, stream)?)); let ssl_stream = Arc::new(Mutex::new(ssl_connector.connect(host, stream)?));
let reader = EventReader::new(LockedRead(ssl_stream.clone())); let reader = EventReader::new(LockedIO(ssl_stream.clone()));
let writer = EventWriter::new(LockedWrite(ssl_stream.clone())); let writer = EventWriter::new(LockedIO(ssl_stream.clone()));
Ok(SslTransport { Ok(SslTransport {
inner: ssl_stream, inner: ssl_stream,
reader: reader, reader: reader,
writer: writer, writer: writer,
}) })
} }
pub fn close(&mut self) {
self.inner.lock()
.unwrap()
.shutdown()
.unwrap(); // TODO: safety, return value and such
}
} }