Remove transport module as it was obsolete
This commit is contained in:
parent
00b06963d7
commit
0a9cb559bd
|
@ -1,7 +1,6 @@
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
mod multiplexer;
|
mod multiplexer;
|
||||||
mod transport;
|
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum CnrError {
|
pub enum CnrError {
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
use serde::de::DeserializeOwned;
|
|
||||||
use serde::Serialize;
|
|
||||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
|
||||||
|
|
||||||
use crate::CnrError;
|
|
||||||
|
|
||||||
pub struct BareTransport<R, W> {
|
|
||||||
writer: W,
|
|
||||||
reader: R,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<W: AsyncWrite + Unpin, R: AsyncRead + Unpin> BareTransport<R, W> {
|
|
||||||
pub fn new(writer: W, reader: R) -> Self {
|
|
||||||
Self { writer, reader }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn write_one_message<M: Serialize>(&mut self, message: &M) -> Result<(), CnrError> {
|
|
||||||
let bytes = serde_bare::to_vec(message)?;
|
|
||||||
self.writer
|
|
||||||
.write_u32(bytes.len().try_into().unwrap())
|
|
||||||
.await?;
|
|
||||||
self.writer.write_all(&bytes).await?;
|
|
||||||
// TODO flush?
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn read_one_message<M: DeserializeOwned>(&mut self) -> Result<Option<M>, CnrError> {
|
|
||||||
let length = self.reader.read_u32().await? as usize;
|
|
||||||
// TODO(perf): use uninit?
|
|
||||||
let mut buf = vec![0u8; length];
|
|
||||||
|
|
||||||
let first_read = self.reader.read(&mut buf[..]).await?;
|
|
||||||
if first_read == 0 {
|
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
if first_read != length {
|
|
||||||
self.reader.read_exact(&mut buf[first_read..]).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(Some(serde_bare::from_slice(&buf)?));
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue