use std::io; use std::marker::PhantomData; use actix_net::codec::Framed; use actix_net::service::{NewService, Service}; use futures::future::{ok, Either, FutureResult}; use futures::{Async, AsyncSink, Future, Poll, Sink}; use tokio_io::AsyncWrite; use error::ResponseError; use h1::{Codec, Message}; use response::Response; pub struct SendError(PhantomData<(T, R, E)>); impl Default for SendError where T: AsyncWrite, E: ResponseError, { fn default() -> Self { SendError(PhantomData) } } impl NewService for SendError where T: AsyncWrite, E: ResponseError, { type Request = Result)>; type Response = R; type Error = (E, Framed); type InitError = (); type Service = SendError; type Future = FutureResult; fn new_service(&self) -> Self::Future { ok(SendError(PhantomData)) } } impl Service for SendError where T: AsyncWrite, E: ResponseError, { type Request = Result)>; type Response = R; type Error = (E, Framed); type Future = Either)>, SendErrorFut>; fn poll_ready(&mut self) -> Poll<(), Self::Error> { Ok(Async::Ready(())) } fn call(&mut self, req: Self::Request) -> Self::Future { match req { Ok(r) => Either::A(ok(r)), Err((e, framed)) => Either::B(SendErrorFut { framed: Some(framed), res: Some(Message::Item(e.error_response())), err: Some(e), _t: PhantomData, }), } } } pub struct SendErrorFut { res: Option>, framed: Option>, err: Option, _t: PhantomData, } impl Future for SendErrorFut where E: ResponseError, T: AsyncWrite, { type Item = R; type Error = (E, Framed); fn poll(&mut self) -> Poll { if let Some(res) = self.res.take() { match self.framed.as_mut().unwrap().start_send(res) { Ok(AsyncSink::Ready) => (), Ok(AsyncSink::NotReady(res)) => { self.res = Some(res); return Ok(Async::NotReady); } Err(_) => { return Err((self.err.take().unwrap(), self.framed.take().unwrap())) } } } match self.framed.as_mut().unwrap().poll_complete() { Ok(Async::Ready(_)) => { return Err((self.err.take().unwrap(), self.framed.take().unwrap())) } Ok(Async::NotReady) => Ok(Async::NotReady), Err(_) => { return Err((self.err.take().unwrap(), self.framed.take().unwrap())) } } } } pub struct SendResponse(PhantomData<(T,)>); impl Default for SendResponse where T: AsyncWrite, { fn default() -> Self { SendResponse(PhantomData) } } impl NewService for SendResponse where T: AsyncWrite, { type Request = (Response, Framed); type Response = Framed; type Error = io::Error; type InitError = (); type Service = SendResponse; type Future = FutureResult; fn new_service(&self) -> Self::Future { ok(SendResponse(PhantomData)) } } impl Service for SendResponse where T: AsyncWrite, { type Request = (Response, Framed); type Response = Framed; type Error = io::Error; type Future = SendResponseFut; fn poll_ready(&mut self) -> Poll<(), Self::Error> { Ok(Async::Ready(())) } fn call(&mut self, (res, framed): Self::Request) -> Self::Future { SendResponseFut { res: Some(Message::Item(res)), framed: Some(framed), } } } pub struct SendResponseFut { res: Option>, framed: Option>, } impl Future for SendResponseFut where T: AsyncWrite, { type Item = Framed; type Error = io::Error; fn poll(&mut self) -> Poll { if let Some(res) = self.res.take() { match self.framed.as_mut().unwrap().start_send(res)? { AsyncSink::Ready => (), AsyncSink::NotReady(res) => { self.res = Some(res); return Ok(Async::NotReady); } } } match self.framed.as_mut().unwrap().poll_complete()? { Async::Ready(_) => Ok(Async::Ready(self.framed.take().unwrap())), Async::NotReady => Ok(Async::NotReady), } } }