| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
use crate::{connection::Connection, enums::CallbackResult, error::Error}; |
| 18 |
use core::task::Poll; |
| 19 |
use pin_project_lite::pin_project; |
| 20 |
use std::pin::Pin; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
pub trait ConnectionFuture: 'static + Send + Sync { |
| 30 |
fn poll( |
| 31 |
self: Pin<&mut Self>, |
| 32 |
connection: &mut Connection, |
| 33 |
ctx: &mut core::task::Context, |
| 34 |
) -> Poll<Result<(), Error>>; |
| 35 |
} |
| 36 |
|
| 37 |
pub(crate) type ConnectionFutureResult = Result<Option<Pin<Box<dyn ConnectionFuture>>>, Error>; |
| 38 |
|
| 39 |
|
| 40 |
pub(crate) struct ErrorFuture { |
| 41 |
error: Option<Error>, |
| 42 |
} |
| 43 |
|
| 44 |
impl ConnectionFuture for ErrorFuture { |
| 45 |
fn poll( |
| 46 |
mut self: Pin<&mut Self>, |
| 47 |
_connection: &mut Connection, |
| 48 |
_ctx: &mut core::task::Context, |
| 49 |
) -> Poll<Result<(), Error>> { |
| 50 |
let err = self.error.take().expect( |
| 51 |
"ErrorFuture should be initialized with Some(error) and a Future should never |
| 52 |
be polled after it returns Poll::Ready", |
| 53 |
); |
| 54 |
Poll::Ready(Err(err)) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
pin_project! { |
| 59 |
|
| 60 |
|
| 61 |
struct OptionalFuture { |
| 62 |
option: Option<Pin<Box<dyn ConnectionFuture>>>, |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl OptionalFuture { |
| 67 |
fn new(input: ConnectionFutureResult) -> Self { |
| 68 |
match input { |
| 69 |
Ok(option) => OptionalFuture { option }, |
| 70 |
Err(error) => { |
| 71 |
let error = Some(error); |
| 72 |
OptionalFuture { |
| 73 |
option: Some(Box::pin(ErrorFuture { error })), |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl ConnectionFuture for OptionalFuture { |
| 81 |
fn poll( |
| 82 |
mut self: Pin<&mut Self>, |
| 83 |
conn: &mut Connection, |
| 84 |
ctx: &mut core::task::Context, |
| 85 |
) -> Poll<Result<(), Error>> { |
| 86 |
match self.option.as_mut() { |
| 87 |
Some(future) => future.as_mut().poll(conn, ctx), |
| 88 |
None => Poll::Ready(Ok(())), |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
#[non_exhaustive] |
| 102 |
#[derive(PartialEq)] |
| 103 |
enum MarkDone { |
| 104 |
ClientHello, |
| 105 |
None, |
| 106 |
} |
| 107 |
|
| 108 |
pin_project! { |
| 109 |
|
| 110 |
pub(crate) struct AsyncCallback { |
| 111 |
#[pin] |
| 112 |
future: OptionalFuture, |
| 113 |
cleanup: MarkDone, |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
impl AsyncCallback { |
| 118 |
pub(crate) fn poll( |
| 119 |
self: Pin<&mut Self>, |
| 120 |
conn: &mut Connection, |
| 121 |
ctx: &mut core::task::Context, |
| 122 |
) -> Poll<Result<(), Error>> { |
| 123 |
let this = self.project(); |
| 124 |
let poll = this.future.poll(conn, ctx); |
| 125 |
if let Poll::Ready(Ok(())) = poll { |
| 126 |
if this.cleanup == &MarkDone::ClientHello { |
| 127 |
conn.mark_client_hello_cb_done()?; |
| 128 |
} |
| 129 |
} |
| 130 |
poll |
| 131 |
} |
| 132 |
|
| 133 |
pub(crate) fn trigger_client_hello_cb( |
| 134 |
future: ConnectionFutureResult, |
| 135 |
conn: &mut Connection, |
| 136 |
) -> CallbackResult { |
| 137 |
let future = OptionalFuture::new(future); |
| 138 |
let cleanup = MarkDone::ClientHello; |
| 139 |
let callback = AsyncCallback { future, cleanup }; |
| 140 |
conn.set_async_callback(callback); |
| 141 |
CallbackResult::Success |
| 142 |
} |
| 143 |
|
| 144 |
pub(crate) fn trigger(future: ConnectionFutureResult, conn: &mut Connection) -> CallbackResult { |
| 145 |
let future = OptionalFuture::new(future); |
| 146 |
let cleanup = MarkDone::None; |
| 147 |
let callback = AsyncCallback { future, cleanup }; |
| 148 |
conn.set_async_callback(callback); |
| 149 |
CallbackResult::Success |
| 150 |
} |
| 151 |
} |