From 1ea1f0b74957b3d5e3111abc22245834fdf62b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 4 Jul 2020 13:28:55 +0200 Subject: [PATCH] aquatic_http: in Connection.inner, replace Either with new enum --- aquatic_http/src/lib/network/connection.rs | 29 +++++++++++++++++--- aquatic_http/src/lib/network/mod.rs | 31 ++++++++++++---------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/aquatic_http/src/lib/network/connection.rs b/aquatic_http/src/lib/network/connection.rs index afaad67..effccd9 100644 --- a/aquatic_http/src/lib/network/connection.rs +++ b/aquatic_http/src/lib/network/connection.rs @@ -3,7 +3,6 @@ use std::io::ErrorKind; use std::io::{Read, Write}; use std::sync::Arc; -use either::Either; use hashbrown::HashMap; use mio::Token; use mio::net::TcpStream; @@ -192,9 +191,15 @@ impl <'a>TlsHandshakeMachine { } +pub enum ConnectionInner { + Established(EstablishedConnection), + InProgress(TlsHandshakeMachine), +} + + pub struct Connection { pub valid_until: ValidUntil, - pub inner: Either, + pub inner: ConnectionInner, } @@ -207,9 +212,9 @@ impl Connection { ) -> Self { // Setup handshake machine if TLS is requested let inner = if let Some(tls_acceptor) = opt_tls_acceptor { - Either::Right(TlsHandshakeMachine::new(tls_acceptor.clone(), tcp_stream)) + ConnectionInner::InProgress(TlsHandshakeMachine::new(tls_acceptor.clone(), tcp_stream)) } else { - Either::Left(EstablishedConnection::new(Stream::TcpStream(tcp_stream))) + ConnectionInner::Established(EstablishedConnection::new(Stream::TcpStream(tcp_stream))) }; Self { @@ -217,6 +222,22 @@ impl Connection { inner, } } + + pub fn get_established(&mut self) -> Option<&mut EstablishedConnection> { + if let ConnectionInner::Established(ref mut established) = self.inner { + Some(established) + } else { + None + } + } + + pub fn get_in_progress(self) -> Option { + if let ConnectionInner::InProgress(machine) = self.inner { + Some(machine) + } else { + None + } + } } diff --git a/aquatic_http/src/lib/network/mod.rs b/aquatic_http/src/lib/network/mod.rs index 7cc5c3c..d796825 100644 --- a/aquatic_http/src/lib/network/mod.rs +++ b/aquatic_http/src/lib/network/mod.rs @@ -5,7 +5,6 @@ use std::io::ErrorKind; use std::sync::Arc; use std::vec::Drain; -use either::Either; use hashbrown::HashMap; use log::{info, debug, error}; use native_tls::TlsAcceptor; @@ -178,17 +177,20 @@ pub fn run_handshakes_and_read_requests( valid_until: ValidUntil, ){ loop { - let opt_connection = connections.get_mut(&poll_token); + // Get connection, updating valid_until + let opt_connection = { + if let Some(connection) = connections.get_mut(&poll_token) { + connection.valid_until = valid_until; - let opt_established = if let Some(connection) = opt_connection { - connection.valid_until = valid_until; - - connection.inner.as_mut().left() - } else { - None + Some(connection) + } else { + None + } }; - if let Some(established) = opt_established { + if let Some(established) = opt_connection + .and_then(Connection::get_established) + { match established.read_request(){ Ok(request) => { let meta = ConnectionMeta { @@ -246,13 +248,13 @@ pub fn run_handshakes_and_read_requests( }, } } else if let Some(handshake_machine) = connections.remove(&poll_token) - .and_then(|c| c.inner.right()) + .and_then(Connection::get_in_progress) { match handshake_machine.establish_tls(){ Ok(established) => { let connection = Connection { valid_until, - inner: Either::Left(established) + inner: ConnectionInner::Established(established) }; connections.insert(poll_token, connection); @@ -260,17 +262,18 @@ pub fn run_handshakes_and_read_requests( Err(TlsHandshakeMachineError::WouldBlock(machine)) => { let connection = Connection { valid_until, - inner: Either::Right(machine) + inner: ConnectionInner::InProgress(machine) }; connections.insert(poll_token, connection); + // Break and wait for more data break }, Err(TlsHandshakeMachineError::Failure(err)) => { info!("tls handshake error: {}", err); - // TLS negotiation error occured + // TLS negotiation failed break } } @@ -287,7 +290,7 @@ pub fn send_responses( ){ for (meta, response) in local_responses.chain(response_channel_receiver){ if let Some(established) = connections.get_mut(&meta.poll_token) - .and_then(|c| c.inner.as_mut().left()) + .and_then(Connection::get_established) { if established.peer_addr != meta.peer_addr { info!("socket worker error: peer socket addrs didn't match");