From cbc111f90b5facc1f2a9dd79ced216279d6260af Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 19 Nov 2024 02:08:52 +0100 Subject: move files + rtp parser --- src/transport/mod.rs | 11 ----------- src/transport/tcp.rs | 48 ------------------------------------------------ src/transport/udp.rs | 35 ----------------------------------- 3 files changed, 94 deletions(-) delete mode 100644 src/transport/mod.rs delete mode 100644 src/transport/tcp.rs delete mode 100644 src/transport/udp.rs (limited to 'src/transport') diff --git a/src/transport/mod.rs b/src/transport/mod.rs deleted file mode 100644 index b4c512b..0000000 --- a/src/transport/mod.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::encoding::Message; -use anyhow::Result; - -pub mod tcp; -pub mod udp; - -#[allow(async_fn_in_trait)] -pub trait Transport { - async fn recv(&self) -> Result; - async fn send(&self, message: Message) -> Result<()>; -} diff --git a/src/transport/tcp.rs b/src/transport/tcp.rs deleted file mode 100644 index efe433d..0000000 --- a/src/transport/tcp.rs +++ /dev/null @@ -1,48 +0,0 @@ -use super::Transport; -use crate::encoding::Message; -use anyhow::Result; -use log::debug; -use std::str::FromStr; -use tokio::{ - io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter}, - net::{ - tcp::{OwnedReadHalf, OwnedWriteHalf}, - TcpStream, - }, - sync::Mutex, -}; - -pub struct TcpTransport { - write: Mutex>, - read: Mutex>, -} - -impl TcpTransport { - pub async fn new(stream: TcpStream) -> Result { - let (read, write) = stream.into_split(); - Ok(Self { - write: BufWriter::new(write).into(), - read: BufReader::new(read).into(), - }) - } -} - -impl Transport for TcpTransport { - async fn recv(&self) -> Result { - let mut g = self.read.lock().await; - let mut message = String::new(); - while !message.ends_with("\r\n\r\n") { - g.read_line(&mut message).await?; - } - let mesg = Message::from_str(message.trim())?; - debug!("<- {mesg}"); - Ok(mesg) - } - async fn send(&self, request: Message) -> Result<()> { - debug!("-> {request}"); - let mut g = self.write.lock().await; - g.write_all(format!("{request}").as_bytes()).await?; - g.flush().await?; - Ok(()) - } -} diff --git a/src/transport/udp.rs b/src/transport/udp.rs deleted file mode 100644 index c0d7829..0000000 --- a/src/transport/udp.rs +++ /dev/null @@ -1,35 +0,0 @@ -use super::Transport; -use crate::encoding::Message; -use anyhow::{anyhow, Result}; -use log::debug; -use std::str::FromStr; -use tokio::net::UdpSocket; - -pub struct UdpTransport { - sock: UdpSocket, -} - -impl UdpTransport { - pub async fn new(sock: UdpSocket) -> Result { - Ok(Self { sock }) - } -} -impl Transport for UdpTransport { - async fn recv(&self) -> Result { - let mut buf = [0; 1024]; - let size = self.sock.recv(&mut buf).await?; - let message = String::from_utf8(buf[..size].to_vec())?; - let (head, body) = message - .split_once("\r\n\r\n") - .ok_or(anyhow!("header end missing"))?; - debug!("<- {head}\n\n{body}"); - let mut mesg = Message::from_str(head.trim_end())?; - *mesg.body_mut() = body.to_string(); - Ok(mesg) - } - async fn send(&self, request: Message) -> Result<()> { - debug!("-> {request}"); - self.sock.send(format!("{request}").as_bytes()).await?; - Ok(()) - } -} -- cgit v1.2.3-70-g09d2