From 271e861169e2c3ee32e0015e7a73b6003e4edb4b Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 19 Aug 2021 20:00:36 +0100 Subject: [PATCH] Started BufferedEsock: A task-based ``wrapper" around an ESock that buffers its input and output. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for rsh's current commit: Small blessing − 小吉 --- src/{sock.rs => sock/mod.rs} | 1 + src/sock/pipe.rs | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) rename src/{sock.rs => sock/mod.rs} (99%) create mode 100644 src/sock/pipe.rs diff --git a/src/sock.rs b/src/sock/mod.rs similarity index 99% rename from src/sock.rs rename to src/sock/mod.rs index 3237b8c..306f7fc 100644 --- a/src/sock.rs +++ b/src/sock/mod.rs @@ -19,6 +19,7 @@ use bytes::Bytes; use cancel::*; pub mod enc; +pub mod pipe; /// Details of a newly accepted raw socket peer. /// diff --git a/src/sock/pipe.rs b/src/sock/pipe.rs new file mode 100644 index 0000000..2f3b956 --- /dev/null +++ b/src/sock/pipe.rs @@ -0,0 +1,53 @@ +//! Piping buffered data from a raw socket to `ESock` +//! +//! This exists because i'm too dumb to implement a functional AsyncRead/Write buffered wrapper stream :/ +use super::*; +use std::{ + io, + marker::{ + Send, Sync, + + Unpin, + PhantomData, + }, +}; +use tokio::sync::{ + mpsc, +}; +use enc::{ + ESock, + + ESockReadHalf, + ESockWriteHalf, +}; + +/// The default buffer size for `BufferedESock`. +pub const DEFAULT_BUFFER_SIZE: usize = 32; + +/// Task-based buffered piping to/from encrypted sockets. +pub struct BufferedESock +{ + bufsz: usize, + _backing: PhantomData>, +} + +impl BufferedESock +where W: AsyncWrite + Unpin + Send + 'static, + R: AsyncRead + Unpin + Send + 'static +{ + /// Create a new buffered ESock pipe with a specific buffer size + pub fn with_size(tx: W, rx: R, bufsz: usize) -> Self + { + //TODO: Spawn read+write buffer tasks + Self { + bufsz, + _backing: PhantomData, + } + } + + /// Create a new buffered ESock pipe with the default buffer size (`DEFAULT_BUFFER_SIZE`). + #[inline] pub fn new(tx: W, rx: R) -> Self + { + Self::with_size(tx, rx, DEFAULT_BUFFER_SIZE) + } +}