Dummy data instead of HTTP

This commit is contained in:
Pascal Engélibert 2026-02-06 15:05:41 +01:00
commit dec39cf2e3
10 changed files with 1543 additions and 1413 deletions

40
src/codec.rs Normal file
View file

@ -0,0 +1,40 @@
use tokio::io::{AsyncRead, AsyncReadExt};
pub struct StreamCodec<S> {
stream: S,
}
impl<S: AsyncRead + Unpin> StreamCodec<S> {
pub fn new(stream: S) -> Self {
Self { stream }
}
pub async fn next(&mut self) -> Result<Vec<u8>, std::io::Error> {
let mut buf = vec![0; 8];
self.stream.read_exact(&mut buf).await?;
let expected_len = u32::from_be_bytes(buf[0..4].try_into().unwrap()) as usize;
if expected_len < 8 || expected_len > 8 * 1024 * 1024 {
return Err(std::io::ErrorKind::InvalidData.into());
}
buf.resize(expected_len, 0);
self.stream.read_exact(&mut buf[8..expected_len]).await?;
Ok(buf)
}
pub fn get_mut(&mut self) -> &mut S {
&mut self.stream
}
}
/*#[cfg(test)]
mod test {
use super::*;
use tokio_util::{bytes::BytesMut, codec::Framed};
#[test]
fn test_decode() {
let stream = futures_util::stream::iter([BytesMut::fr&[0u8]]);
let stream = Framed::new(stream, CustomCodec::new());
}
}*/