1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-31 17:07:01 +02:00

refactor Connector service

This commit is contained in:
Nikolay Kim
2018-08-29 15:15:24 -07:00
parent 7ec92f7b1c
commit 361ffd8d2f
3 changed files with 121 additions and 76 deletions

View File

@@ -92,83 +92,94 @@ impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
}
/// Openssl connector factory
pub struct OpensslConnector<T> {
pub struct OpensslConnector<T, Io> {
connector: SslConnector,
io: PhantomData<T>,
t: PhantomData<T>,
io: PhantomData<Io>,
}
impl<T> OpensslConnector<T> {
impl<T, Io> OpensslConnector<T, Io> {
pub fn new(connector: SslConnector) -> Self {
OpensslConnector {
connector,
t: PhantomData,
io: PhantomData,
}
}
}
impl<T> Clone for OpensslConnector<T> {
impl<T, Io> Clone for OpensslConnector<T, Io> {
fn clone(&self) -> Self {
Self {
connector: self.connector.clone(),
t: PhantomData,
io: PhantomData,
}
}
}
impl<T: AsyncRead + AsyncWrite> NewService for OpensslConnector<T> {
type Request = (ConnectionInfo, T);
type Response = (ConnectionInfo, SslStream<T>);
impl<T, Io: AsyncRead + AsyncWrite> NewService for OpensslConnector<T, Io> {
type Request = (T, ConnectionInfo, Io);
type Response = (T, ConnectionInfo, SslStream<Io>);
type Error = Error;
type Service = OpensslConnectorService<T>;
type Service = OpensslConnectorService<T, Io>;
type InitError = io::Error;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future {
future::ok(OpensslConnectorService {
connector: self.connector.clone(),
t: PhantomData,
io: PhantomData,
})
}
}
pub struct OpensslConnectorService<T> {
pub struct OpensslConnectorService<T, Io> {
connector: SslConnector,
io: PhantomData<T>,
t: PhantomData<T>,
io: PhantomData<Io>,
}
impl<T: AsyncRead + AsyncWrite> Service for OpensslConnectorService<T> {
type Request = (ConnectionInfo, T);
type Response = (ConnectionInfo, SslStream<T>);
impl<T, Io: AsyncRead + AsyncWrite> Service for OpensslConnectorService<T, Io> {
type Request = (T, ConnectionInfo, Io);
type Response = (T, ConnectionInfo, SslStream<Io>);
type Error = Error;
type Future = ConnectAsyncExt<T>;
type Future = ConnectAsyncExt<T, Io>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, (info, stream): Self::Request) -> Self::Future {
fn call(&mut self, (req, info, stream): Self::Request) -> Self::Future {
ConnectAsyncExt {
fut: SslConnectorExt::connect_async(&self.connector, &info.host, stream),
req: Some(req),
host: Some(info),
}
}
}
pub struct ConnectAsyncExt<T> {
fut: ConnectAsync<T>,
pub struct ConnectAsyncExt<T, Io> {
fut: ConnectAsync<Io>,
req: Option<T>,
host: Option<ConnectionInfo>,
}
impl<T> Future for ConnectAsyncExt<T>
impl<T, Io> Future for ConnectAsyncExt<T, Io>
where
T: AsyncRead + AsyncWrite,
Io: AsyncRead + AsyncWrite,
{
type Item = (ConnectionInfo, SslStream<T>);
type Item = (T, ConnectionInfo, SslStream<Io>);
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
match self.fut.poll()? {
Async::Ready(stream) => Ok(Async::Ready((self.host.take().unwrap(), stream))),
Async::Ready(stream) => Ok(Async::Ready((
self.req.take().unwrap(),
self.host.take().unwrap(),
stream,
))),
Async::NotReady => Ok(Async::NotReady),
}
}