Skip to content

Commit

Permalink
feat(cdk-rust): Implement TCP transport
Browse files Browse the repository at this point in the history
  • Loading branch information
kckeiks committed Oct 16, 2023
1 parent b961092 commit 00f97b8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/cdk-rust/src/transport/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod tcp;
pub mod webtransport;

use anyhow::Result;
Expand Down
81 changes: 81 additions & 0 deletions lib/cdk-rust/src/transport/tcp/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use std::io;
use std::net::SocketAddr;
use std::pin::Pin;
use std::task::{Context, Poll};

use async_trait::async_trait;
use bytes::Bytes;
use futures::{Sink, Stream};
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
use tokio::net::TcpStream;
use tokio_util::codec::{FramedRead, FramedWrite, LengthDelimitedCodec};

use crate::transport::{Message, Transport};

struct TcpTransport {
target: SocketAddr,
}

#[async_trait]
impl Transport for TcpTransport {
type Sender = TcpSender;
type Receiver = TcpReceiver;

async fn connect(&self) -> anyhow::Result<(Self::Sender, Self::Receiver)> {
let stream = TcpStream::connect(self.target).await?;
let (rx, tx) = stream.into_split();
Ok((
TcpSender {
inner: FramedWrite::new(tx, LengthDelimitedCodec::new()),
},
TcpReceiver {
inner: FramedRead::new(rx, LengthDelimitedCodec::new()),
},
))
}
}

pub struct TcpSender {
inner: FramedWrite<OwnedWriteHalf, LengthDelimitedCodec>,
}

impl Sink<Message> for TcpSender {
type Error = io::Error;

fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut self.inner).poll_ready(cx)
}

fn start_send(mut self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error> {
Pin::new(&mut self.inner).start_send(item)
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut self.inner).poll_flush(cx)
}

fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut self.inner).poll_close(cx)
}
}

pub struct TcpReceiver {
inner: FramedRead<OwnedReadHalf, LengthDelimitedCodec>,
}

impl Stream for TcpReceiver {
type Item = Message;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.inner)
.poll_next(cx)
.map(|opt| match opt {
None => None,
Some(Ok(bytes)) => Some(Bytes::from(bytes)),
Some(Err(e)) => {
log::error!("unexpected error in receiving stream: {e:?}");
None
},
})
}
}

0 comments on commit 00f97b8

Please sign in to comment.