You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.1 KiB
81 lines
2.1 KiB
//! Traits for serialising the message
|
|
use super::*;
|
|
use std::{
|
|
pin::Pin,
|
|
task::{
|
|
Context, Poll,
|
|
},
|
|
};
|
|
|
|
/// A type that can be used to serialise a message
|
|
pub trait MessageSender
|
|
{
|
|
#[inline] fn encrypt_key(&self, _key: &aes::AesKey) -> Option<[u8; RSA_BLOCK_SIZE]> { None }
|
|
#[inline] fn sign_data(&self, _data: &[u8]) -> Option<rsa::Signature> { None }
|
|
}
|
|
|
|
/// A type that can be used to deserialise a message
|
|
pub trait MessageReceiver
|
|
{
|
|
#[inline] fn decrypt_key(&self, _enc_key: &[u8; RSA_BLOCK_SIZE]) -> Option<aes::AesKey>{ None }
|
|
#[inline] fn verify_data(&self, _data: &[u8], _sig: rsa::Signature) -> Option<bool> { None }
|
|
}
|
|
|
|
impl MessageSender for (){}
|
|
impl MessageReceiver for (){}
|
|
|
|
#[derive(Debug)]
|
|
pub(super) struct WriteCounter<W:?Sized>(pub usize, pub W);
|
|
|
|
impl<W: ?Sized + io::Write> io::Write for WriteCounter<W>
|
|
{
|
|
#[inline] fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
let w = self.1.write(buf)?;
|
|
self.0 +=w;
|
|
Ok(w)
|
|
}
|
|
#[inline] fn flush(&mut self) -> io::Result<()> {
|
|
self.1.flush()
|
|
}
|
|
}
|
|
|
|
|
|
#[pin_project]
|
|
#[derive(Debug)]
|
|
pub(super) struct AsyncWriteCounter<W:?Sized>(pub usize, #[pin] pub W);
|
|
|
|
impl<W: ?Sized + AsyncWrite> AsyncWrite for AsyncWriteCounter<W>
|
|
{
|
|
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
self.project().1.poll_shutdown(cx)
|
|
}
|
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
self.project().1.poll_flush(cx)
|
|
}
|
|
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
|
|
let this = self.project();
|
|
match this.1.poll_write(cx, buf) {
|
|
Poll::Ready(Ok(sz)) => {
|
|
*this.0 += sz;
|
|
Poll::Ready(Ok(sz))
|
|
},
|
|
x => x,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(super) async fn write_all_async(mut to: impl AsyncWrite + Unpin, bytes: impl AsRef<[u8]>) -> io::Result<usize>
|
|
{
|
|
use tokio::prelude::*;
|
|
let bytes= bytes.as_ref();
|
|
to.write_all(bytes).await?;
|
|
Ok(bytes.len())
|
|
}
|
|
#[inline(always)] pub(super) fn write_all(mut to: impl io::Write, bytes: impl AsRef<[u8]>) -> io::Result<usize>
|
|
{
|
|
let bytes= bytes.as_ref();
|
|
to.write_all(bytes)?;
|
|
Ok(bytes.len())
|
|
}
|
|
|