|
| 1 | +use std::{ |
| 2 | + future::Future, |
| 3 | + io, |
| 4 | + ops::{Deref, DerefMut}, |
| 5 | + pin::Pin, |
| 6 | + task::{Context, Poll}, |
| 7 | +}; |
| 8 | + |
| 9 | +use pin_project_lite::pin_project; |
| 10 | +use rustls::{ConnectionCommon, SideData}; |
| 11 | +use tokio::io::{AsyncRead, AsyncWrite}; |
| 12 | + |
| 13 | +use crate::common::IoSession; |
| 14 | + |
| 15 | +use super::{Stream, TlsState}; |
| 16 | + |
| 17 | +/// Full result of sync closure |
| 18 | +type SessionResult<S> = Result<S, (Option<S>, io::Error)>; |
| 19 | +/// Executor result wrapping sync closure result |
| 20 | +type SyncExecutorResult<S> = Result<SessionResult<S>, compute_heavy_future_executor::Error>; |
| 21 | +/// Future wrapping waiting on executor |
| 22 | +type SessionFuture<S> = Box<dyn Future<Output = SyncExecutorResult<S>> + Unpin + Send>; |
| 23 | + |
| 24 | +pin_project! { |
| 25 | +/// Session is off doing compute-heavy sync work, such as initializing the session or processing handshake packets. |
| 26 | +/// Might be on another thread / external threadpool. |
| 27 | +/// |
| 28 | +/// This future sleeps on it in current worker thread until it completes. |
| 29 | +pub(crate) struct AsyncSession<IS: IoSession> { |
| 30 | + #[pin] |
| 31 | + future: SessionFuture<IS::Session>, |
| 32 | + io: IS::Io, |
| 33 | + state: TlsState, |
| 34 | + extras: IS::Extras, |
| 35 | +} |
| 36 | +} |
| 37 | + |
| 38 | +impl<IS, SD> AsyncSession<IS> |
| 39 | +where |
| 40 | + IS: IoSession + Unpin, |
| 41 | + IS::Io: AsyncRead + AsyncWrite + Unpin, |
| 42 | + IS::Session: DerefMut + Deref<Target = ConnectionCommon<SD>> + Unpin + Send + 'static, |
| 43 | + SD: SideData, |
| 44 | +{ |
| 45 | + pub(crate) fn process_packets(stream: IS) -> Self { |
| 46 | + let (state, io, mut session, extras) = stream.into_inner(); |
| 47 | + |
| 48 | + let closure = move || match session.process_new_packets() { |
| 49 | + Ok(_) => Ok(session), |
| 50 | + Err(err) => Err(( |
| 51 | + Some(session), |
| 52 | + io::Error::new(io::ErrorKind::InvalidData, err), |
| 53 | + )), |
| 54 | + }; |
| 55 | + |
| 56 | + let future = compute_heavy_future_executor::execute_sync(closure); |
| 57 | + |
| 58 | + Self { |
| 59 | + future: Box::new(Box::pin(future)), |
| 60 | + io, |
| 61 | + state, |
| 62 | + extras, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + pub(crate) fn into_stream( |
| 67 | + mut self, |
| 68 | + session_result: Result<IS::Session, (Option<IS::Session>, io::Error)>, |
| 69 | + cx: &mut Context<'_>, |
| 70 | + ) -> Result<IS, (io::Error, IS::Io)> { |
| 71 | + match session_result { |
| 72 | + Ok(session) => Ok(IS::from_inner(self.state, self.io, session, self.extras)), |
| 73 | + Err((Some(mut session), err)) => { |
| 74 | + // In case we have an alert to send describing this error, |
| 75 | + // try a last-gasp write -- but don't predate the primary |
| 76 | + // error. |
| 77 | + let mut tls_stream: Stream<'_, <IS as IoSession>::Io, <IS as IoSession>::Session> = |
| 78 | + Stream::new(&mut self.io, &mut session).set_eof(!self.state.readable()); |
| 79 | + let _ = tls_stream.write_io(cx); |
| 80 | + |
| 81 | + // still drop the tls session and return the io error only |
| 82 | + Err((err, self.io)) |
| 83 | + } |
| 84 | + Err((None, err)) => Err((err, self.io)), |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + #[inline] |
| 89 | + pub fn get_ref(&self) -> &IS::Io { |
| 90 | + &self.io |
| 91 | + } |
| 92 | + |
| 93 | + #[inline] |
| 94 | + pub fn get_mut(&mut self) -> &mut IS::Io { |
| 95 | + &mut self.io |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl<IS, SD> Future for AsyncSession<IS> |
| 100 | +where |
| 101 | + IS: IoSession + Unpin, |
| 102 | + IS::Session: DerefMut + Deref<Target = ConnectionCommon<SD>> + Unpin + Send + 'static, |
| 103 | + SD: SideData, |
| 104 | +{ |
| 105 | + type Output = Result<IS::Session, (Option<IS::Session>, io::Error)>; |
| 106 | + |
| 107 | + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 108 | + let mut this = self.project(); |
| 109 | + |
| 110 | + match ready!(this.future.as_mut().poll(cx)) { |
| 111 | + Ok(session_res) => match session_res { |
| 112 | + Ok(res) => Poll::Ready(Ok(res)), |
| 113 | + // return any session along with the error, |
| 114 | + // so the caller can flush any remaining alerts in buffer to i/o |
| 115 | + Err((session, err)) => Poll::Ready(Err(( |
| 116 | + session, |
| 117 | + io::Error::new(io::ErrorKind::InvalidData, err), |
| 118 | + ))), |
| 119 | + }, |
| 120 | + // We don't have a session to flush here because the executor ate it |
| 121 | + // TODO: not all errors should be modeled as io |
| 122 | + Err(executor_error) => Poll::Ready(Err(( |
| 123 | + None, |
| 124 | + io::Error::new(io::ErrorKind::Other, executor_error), |
| 125 | + ))), |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments