From b44ef9cc2ca6a7ec7fd9886d8180878211784468 Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 19 Feb 2017 02:19:37 +0100 Subject: [PATCH] SslTransport::close and cleanups --- src/jid.rs | 4 +--- src/transport.rs | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/jid.rs b/src/jid.rs index 7e89be3..50d7261 100644 --- a/src/jid.rs +++ b/src/jid.rs @@ -4,8 +4,6 @@ use std::convert::Into; use std::str::FromStr; -use std::string::ToString; - #[derive(Debug, Clone, PartialEq, Eq)] pub enum JidParseError { NoDomain, @@ -43,7 +41,7 @@ impl FromStr for Jid { fn from_str(s: &str) -> Result { // 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 state = ParserState::Node; let mut node = None; diff --git a/src/transport.rs b/src/transport.rs index bf89916..8016f87 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,7 +1,7 @@ use std::io::prelude::*; use std::io; -use std::net::{SocketAddr, TcpStream}; +use std::net::TcpStream; use xml::reader::{EventReader, XmlEvent as XmlReaderEvent}; use xml::writer::{EventWriter, XmlEvent as XmlWriterEvent}; @@ -19,9 +19,9 @@ pub trait Transport { fn read_event(&mut self) -> Result; } -struct LockedWrite(Arc>); +struct LockedIO(Arc>); -impl io::Write for LockedWrite { +impl io::Write for LockedIO { fn write(&mut self, buf: &[u8]) -> io::Result { let mut inner = self.0.lock().unwrap(); // TODO: make safer inner.write(buf) @@ -33,9 +33,7 @@ impl io::Write for LockedWrite { } } -struct LockedRead(Arc>); - -impl io::Read for LockedRead { +impl io::Read for LockedIO { fn read(&mut self, buf: &mut [u8]) -> io::Result { let mut inner = self.0.lock().unwrap(); // TODO: make safer inner.read(buf) @@ -44,10 +42,10 @@ impl io::Read for LockedRead { pub struct SslTransport { inner: Arc>>, // TODO: this feels rather ugly - reader: EventReader>>, // TODO: especially feels ugly because - // this read would keep the lock - // held very long (potentially) - writer: EventWriter>>, + reader: EventReader>>, // TODO: especially feels ugly because + // this read would keep the lock + // held very long (potentially) + writer: EventWriter>>, } impl Transport for SslTransport { @@ -71,7 +69,7 @@ impl SslTransport { let mut parser = EventReader::new(stream); loop { // TODO: possibly a timeout? match parser.next()? { - XmlReaderEvent::StartElement { name, namespace, .. } => { + XmlReaderEvent::StartElement { name, .. } => { if let Some(ns) = name.namespace { if ns == ns::TLS && name.local_name == "proceed" { break; @@ -87,12 +85,19 @@ impl SslTransport { let stream = parser.into_inner(); let ssl_connector = SslConnectorBuilder::new(SslMethod::tls())?.build(); let ssl_stream = Arc::new(Mutex::new(ssl_connector.connect(host, stream)?)); - let reader = EventReader::new(LockedRead(ssl_stream.clone())); - let writer = EventWriter::new(LockedWrite(ssl_stream.clone())); + let reader = EventReader::new(LockedIO(ssl_stream.clone())); + let writer = EventWriter::new(LockedIO(ssl_stream.clone())); Ok(SslTransport { inner: ssl_stream, reader: reader, writer: writer, }) } + + pub fn close(&mut self) { + self.inner.lock() + .unwrap() + .shutdown() + .unwrap(); // TODO: safety, return value and such + } }