Added AsyncWrite and AsyncRead impls to `ESock<W,R>`

Fortune for rsh's current commit: Half blessing − 半吉
master
Avril 3 years ago
parent b6b3bb0fd5
commit edeb2ffee7
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -93,7 +93,6 @@ impl Default for ESockState
} }
} }
/// Contains a cc20 Key and IV that can be serialized and then encrypted /// Contains a cc20 Key and IV that can be serialized and then encrypted
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct ESockSessionKey struct ESockSessionKey
@ -110,7 +109,6 @@ impl fmt::Display for ESockSessionKey
} }
} }
impl ESockSessionKey impl ESockSessionKey
{ {
/// Generate a new cc20 key + iv, /// Generate a new cc20 key + iv,
@ -375,6 +373,44 @@ impl<W: AsyncWrite+ Unpin, R: AsyncRead + Unpin> ESock<W, R>
} }
} }
impl<W, R> AsyncWrite for ESock<W, R>
where W: AsyncWrite
{
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize, io::Error>> {
//XXX: If the encryption state of the socket is changed between polls, this breaks. Idk if we can do anything about that tho.
if self.state.encw {
self.project().tx.poll_write(cx, buf)
} else {
// SAFETY: Uhh... well I think this is fine? Because we can project the container.
// TODO: Can we project the `tx`? Or maybe add a method in `AsyncSink` to map a pinned sink to a `Pin<&mut W>`?
unsafe { self.map_unchecked_mut(|this| this.tx.inner_mut()).poll_write(cx, buf)}
}
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
// Should we do anything else here?
// Should we clear foreign key/current session key?
self.project().tx.poll_shutdown(cx)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
self.project().tx.poll_flush(cx)
}
}
impl<W, R> AsyncRead for ESock<W, R>
where R: AsyncRead
{
fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
//XXX: If the encryption state of the socket is changed between polls, this breaks. Idk if we can do anything about that tho.
if self.state.encw {
self.project().rx.poll_read(cx, buf)
} else {
// SAFETY: Uhh... well I think this is fine? Because we can project the container.
// TODO: Can we project the `tx`? Or maybe add a method in `AsyncSink` to map a pinned sink to a `Pin<&mut W>`?
unsafe { self.map_unchecked_mut(|this| this.rx.inner_mut()).poll_read(cx, buf)}
}
}
}
/// Write half for `ESock`. /// Write half for `ESock`.
#[pin_project] #[pin_project]
#[derive(Debug)] #[derive(Debug)]

Loading…
Cancel
Save