mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
more cleanups, put LockedIO into its own module
This commit is contained in:
parent
b44ef9cc2c
commit
75986db40b
3 changed files with 44 additions and 24 deletions
|
@ -6,3 +6,5 @@ pub mod transport;
|
|||
pub mod error;
|
||||
pub mod jid;
|
||||
pub mod client;
|
||||
|
||||
mod locked_io;
|
||||
|
|
37
src/locked_io.rs
Normal file
37
src/locked_io.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub struct LockedIO<T>(Arc<Mutex<T>>);
|
||||
|
||||
impl<T> LockedIO<T> {
|
||||
pub fn from(inner: Arc<Mutex<T>>) -> LockedIO<T> {
|
||||
LockedIO(inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for LockedIO<T> {
|
||||
fn clone(&self) -> LockedIO<T> {
|
||||
LockedIO(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Write> io::Write for LockedIO<T> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
let mut inner = self.0.lock().unwrap(); // TODO: make safer
|
||||
inner.write(buf)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
let mut inner = self.0.lock().unwrap(); // TODO: make safer
|
||||
inner.flush()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Read> io::Read for LockedIO<T> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
let mut inner = self.0.lock().unwrap(); // TODO: make safer
|
||||
inner.read(buf)
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
use std::io::prelude::*;
|
||||
use std::io;
|
||||
|
||||
use std::net::TcpStream;
|
||||
|
||||
|
@ -10,6 +9,8 @@ use std::sync::{Arc, Mutex};
|
|||
|
||||
use ns;
|
||||
|
||||
use locked_io::LockedIO;
|
||||
|
||||
use error::Error;
|
||||
|
||||
use openssl::ssl::{SslMethod, SslConnectorBuilder, SslStream};
|
||||
|
@ -19,27 +20,6 @@ pub trait Transport {
|
|||
fn read_event(&mut self) -> Result<XmlReaderEvent, Error>;
|
||||
}
|
||||
|
||||
struct LockedIO<T>(Arc<Mutex<T>>);
|
||||
|
||||
impl<T: Write> io::Write for LockedIO<T> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
let mut inner = self.0.lock().unwrap(); // TODO: make safer
|
||||
inner.write(buf)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
let mut inner = self.0.lock().unwrap(); // TODO: make safer
|
||||
inner.flush()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Read> io::Read for LockedIO<T> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
let mut inner = self.0.lock().unwrap(); // TODO: make safer
|
||||
inner.read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SslTransport {
|
||||
inner: Arc<Mutex<SslStream<TcpStream>>>, // TODO: this feels rather ugly
|
||||
reader: EventReader<LockedIO<SslStream<TcpStream>>>, // TODO: especially feels ugly because
|
||||
|
@ -85,8 +65,9 @@ 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(LockedIO(ssl_stream.clone()));
|
||||
let writer = EventWriter::new(LockedIO(ssl_stream.clone()));
|
||||
let locked_io = LockedIO::from(ssl_stream.clone());
|
||||
let reader = EventReader::new(locked_io.clone());
|
||||
let writer = EventWriter::new(locked_io);
|
||||
Ok(SslTransport {
|
||||
inner: ssl_stream,
|
||||
reader: reader,
|
||||
|
|
Loading…
Reference in a new issue