use crate::errors::AtomicResult;
pub trait AtomicTransport: Send {
fn send(
&mut self,
frame: Vec<u8>,
) -> impl std::future::Future<Output = AtomicResult<()>> + Send;
fn recv(&mut self) -> impl std::future::Future<Output = AtomicResult<Option<Vec<u8>>>> + Send;
}
pub struct ChannelTransport {
tx: tokio::sync::mpsc::Sender<Vec<u8>>,
rx: tokio::sync::mpsc::Receiver<Vec<u8>>,
}
impl ChannelTransport {
pub fn pair() -> (Self, Self) {
let (a_tx, a_rx) = tokio::sync::mpsc::channel(32);
let (b_tx, b_rx) = tokio::sync::mpsc::channel(32);
(Self { tx: a_tx, rx: b_rx }, Self { tx: b_tx, rx: a_rx })
}
}
impl AtomicTransport for ChannelTransport {
async fn send(&mut self, frame: Vec<u8>) -> AtomicResult<()> {
self.tx
.send(frame)
.await
.map_err(|_| "sync transport closed".into())
}
async fn recv(&mut self) -> AtomicResult<Option<Vec<u8>>> {
Ok(self.rx.recv().await)
}
}