From c69d675113ae6b08bc679305389b94aa24943cdb Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 27 Aug 2018 14:20:41 -0700 Subject: [PATCH] Connector::Response returns addr and tcp stream --- examples/ssl.rs | 11 +++++++---- src/configurable.rs | 12 ++++-------- src/connector.rs | 17 ++++++++++++----- src/ssl/openssl.rs | 12 ++++-------- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/examples/ssl.rs b/examples/ssl.rs index f95ff83a..04ebe22d 100644 --- a/examples/ssl.rs +++ b/examples/ssl.rs @@ -43,7 +43,7 @@ fn main() { .unwrap(); let num = Arc::new(AtomicUsize::new(0)); - let openssl = ssl::OpensslService::new(builder); + let openssl = ssl::OpensslAcceptor::new(builder); // server start mutiple workers, it runs supplied `Fn` in each worker. Server::default() @@ -51,9 +51,12 @@ fn main() { let num = num.clone(); // configure service - openssl.clone().and_then((service, move || { - Ok::<_, io::Error>(ServiceState { num: num.clone() }) - })) + openssl + .clone() + .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) + .and_then((service, move || { + Ok::<_, io::Error>(ServiceState { num: num.clone() }) + })) }).unwrap() .start(); diff --git a/src/configurable.rs b/src/configurable.rs index df6dec1b..66eb5849 100644 --- a/src/configurable.rs +++ b/src/configurable.rs @@ -235,7 +235,7 @@ where pub struct AndThenNewConfigurableService where A: NewConfigurableService, - B: NewConfigurableService + B: NewConfigurableService, { a: A, b: B, @@ -259,11 +259,7 @@ where impl NewConfigurableService for AndThenNewConfigurableService where - A: NewConfigurableService< - C, - Response = B::Request, - InitError = B::InitError, - >, + A: NewConfigurableService, A::Error: Into, B: NewConfigurableService, C: Clone, @@ -359,7 +355,7 @@ where /// `MapErrNewService` new service combinator pub struct MapErrNewConfigurableService where - A: NewConfigurableService + A: NewConfigurableService, { a: A, f: F, @@ -455,7 +451,7 @@ where /// `MapInitErr` service combinator pub struct MapInitErr where - A: NewConfigurableService + A: NewConfigurableService, { a: A, f: F, diff --git a/src/connector.rs b/src/connector.rs index 9a80a984..5752228c 100644 --- a/src/connector.rs +++ b/src/connector.rs @@ -51,7 +51,7 @@ impl Connector { impl Service for Connector { type Request = String; - type Response = TcpStream; + type Response = (String, TcpStream); type Error = ConnectorError; type Future = ConnectorFuture; @@ -60,25 +60,32 @@ impl Service for Connector { } fn call(&mut self, addr: String) -> Self::Future { + let fut = ResolveFut::new(&addr, 0, &self.resolver); + ConnectorFuture { - fut: ResolveFut::new(addr, 0, &self.resolver), + fut, + addr: Some(addr), fut2: None, } } } pub struct ConnectorFuture { + addr: Option, fut: ResolveFut, fut2: Option, } impl Future for ConnectorFuture { - type Item = TcpStream; + type Item = (String, TcpStream); type Error = ConnectorError; fn poll(&mut self) -> Poll { if let Some(ref mut fut) = self.fut2 { - return fut.poll(); + return match fut.poll()? { + Async::Ready(stream) => Ok(Async::Ready((self.addr.take().unwrap(), stream))), + Async::NotReady => Ok(Async::NotReady), + }; } match self.fut.poll()? { Async::Ready(addrs) => { @@ -100,7 +107,7 @@ struct ResolveFut { } impl ResolveFut { - pub fn new(addr: String, port: u16, resolver: &AsyncResolver) -> Self { + pub fn new(addr: &str, port: u16, resolver: &AsyncResolver) -> Self { // we need to do dns resolution match ResolveFut::parse(addr.as_ref(), port) { Ok((host, port)) => ResolveFut { diff --git a/src/ssl/openssl.rs b/src/ssl/openssl.rs index 606e57bf..e1de2dbc 100644 --- a/src/ssl/openssl.rs +++ b/src/ssl/openssl.rs @@ -115,7 +115,7 @@ impl Clone for OpensslConnector { } impl NewService for OpensslConnector { - type Request = T; + type Request = (String, T); type Response = SslStream; type Error = Error; type Service = OpensslConnectorService; @@ -130,17 +130,13 @@ impl NewService for OpensslConnector { } } -pub trait OpensslDomain { - fn domain(&self) -> &str; -} - pub struct OpensslConnectorService { connector: SslConnector, io: PhantomData, } impl Service for OpensslConnectorService { - type Request = T; + type Request = (String, T); type Response = SslStream; type Error = Error; type Future = ConnectAsync; @@ -149,7 +145,7 @@ impl Service for OpensslConnectorService { Ok(Async::Ready(())) } - fn call(&mut self, req: Self::Request) -> Self::Future { - SslConnectorExt::connect_async(&self.connector, "", req) + fn call(&mut self, (host, stream): Self::Request) -> Self::Future { + SslConnectorExt::connect_async(&self.connector, &host, stream) } }