|
| 1 | +use std::sync::{Arc, Mutex}; |
| 2 | + |
| 3 | +use crate::prover::{Prover, ProverError, control::ProverControl, state}; |
| 4 | +use futures::{AsyncRead, AsyncWrite}; |
| 5 | + |
| 6 | +pub(crate) type ProverConnected = Arc<Mutex<Prover<state::Connected>>>; |
| 7 | + |
| 8 | +/// A TLS connection to a server. |
| 9 | +/// |
| 10 | +/// This type implements [`AsyncRead`] and [`AsyncWrite`] and can be used to |
| 11 | +/// communicate with a server using TLS. |
| 12 | +/// |
| 13 | +/// # Note |
| 14 | +/// |
| 15 | +/// This connection is closed on a best-effort basis if this is dropped. To |
| 16 | +/// ensure a clean close, you should call |
| 17 | +/// [`AsyncWriteExt::close`](futures::io::AsyncWriteExt::close) to close the |
| 18 | +/// connection. |
| 19 | +pub struct TlsConnection { |
| 20 | + prover: ProverConnected, |
| 21 | +} |
| 22 | + |
| 23 | +impl TlsConnection { |
| 24 | + pub(crate) fn new(prover: ProverConnected) -> Self { |
| 25 | + Self { prover } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl AsyncRead for TlsConnection { |
| 30 | + fn poll_read( |
| 31 | + self: std::pin::Pin<&mut Self>, |
| 32 | + cx: &mut std::task::Context<'_>, |
| 33 | + buf: &mut [u8], |
| 34 | + ) -> std::task::Poll<std::io::Result<usize>> { |
| 35 | + todo!() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl AsyncWrite for TlsConnection { |
| 40 | + fn poll_write( |
| 41 | + self: std::pin::Pin<&mut Self>, |
| 42 | + cx: &mut std::task::Context<'_>, |
| 43 | + buf: &[u8], |
| 44 | + ) -> std::task::Poll<std::io::Result<usize>> { |
| 45 | + todo!() |
| 46 | + } |
| 47 | + |
| 48 | + fn poll_flush( |
| 49 | + self: std::pin::Pin<&mut Self>, |
| 50 | + cx: &mut std::task::Context<'_>, |
| 51 | + ) -> std::task::Poll<std::io::Result<()>> { |
| 52 | + todo!() |
| 53 | + } |
| 54 | + |
| 55 | + fn poll_close( |
| 56 | + self: std::pin::Pin<&mut Self>, |
| 57 | + cx: &mut std::task::Context<'_>, |
| 58 | + ) -> std::task::Poll<std::io::Result<()>> { |
| 59 | + todo!() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// A future to drive the connection. Must be polled to make progress. |
| 64 | +pub struct ConnectionFuture<S> { |
| 65 | + socket: S, |
| 66 | + prover: ProverConnected, |
| 67 | +} |
| 68 | + |
| 69 | +impl<S> ConnectionFuture<S> { |
| 70 | + pub(crate) fn new(socket: S, prover: ProverConnected) -> Self { |
| 71 | + Self { socket, prover } |
| 72 | + } |
| 73 | + |
| 74 | + /// Returns a handle to control the prover. |
| 75 | + pub fn handle(&self) -> ProverControl { |
| 76 | + ProverControl { |
| 77 | + prover: self.prover.clone(), |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<S> Future for ConnectionFuture<S> |
| 83 | +where |
| 84 | + S: AsyncRead + AsyncWrite + Send, |
| 85 | +{ |
| 86 | + type Output = Result<Prover<state::Committed>, ProverError>; |
| 87 | + |
| 88 | + fn poll( |
| 89 | + self: std::pin::Pin<&mut Self>, |
| 90 | + cx: &mut std::task::Context<'_>, |
| 91 | + ) -> std::task::Poll<Self::Output> { |
| 92 | + todo!() |
| 93 | + } |
| 94 | +} |
0 commit comments