aboutsummaryrefslogtreecommitdiff
path: root/karyons_net/src/transports/tcp.rs
diff options
context:
space:
mode:
authorhozan23 <hozan23@proton.me>2023-11-09 11:38:19 +0300
committerhozan23 <hozan23@proton.me>2023-11-09 11:38:19 +0300
commit849d827486c75b2ab223d7b0e638dbb5b74d4d1d (patch)
tree41cd3babc37147ec4a40cab8ce8ae31c91cce33b /karyons_net/src/transports/tcp.rs
parentde1354525895ffbad18f90a5246fd65157f7449e (diff)
rename crates
Diffstat (limited to 'karyons_net/src/transports/tcp.rs')
-rw-r--r--karyons_net/src/transports/tcp.rs82
1 files changed, 0 insertions, 82 deletions
diff --git a/karyons_net/src/transports/tcp.rs b/karyons_net/src/transports/tcp.rs
deleted file mode 100644
index 5ff7b28..0000000
--- a/karyons_net/src/transports/tcp.rs
+++ /dev/null
@@ -1,82 +0,0 @@
-use async_trait::async_trait;
-
-use smol::{
- io::{split, AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf},
- lock::Mutex,
- net::{TcpListener, TcpStream},
-};
-
-use crate::{
- connection::Connection,
- endpoint::{Addr, Endpoint, Port},
- listener::Listener,
- Result,
-};
-
-/// TCP network connection implementations of the `Connection` trait.
-pub struct TcpConn {
- inner: TcpStream,
- read: Mutex<ReadHalf<TcpStream>>,
- write: Mutex<WriteHalf<TcpStream>>,
-}
-
-impl TcpConn {
- /// Creates a new TcpConn
- pub fn new(conn: TcpStream) -> Self {
- let (read, write) = split(conn.clone());
- Self {
- inner: conn,
- read: Mutex::new(read),
- write: Mutex::new(write),
- }
- }
-}
-
-#[async_trait]
-impl Connection for TcpConn {
- fn peer_endpoint(&self) -> Result<Endpoint> {
- Ok(Endpoint::new_tcp_addr(&self.inner.peer_addr()?))
- }
-
- fn local_endpoint(&self) -> Result<Endpoint> {
- Ok(Endpoint::new_tcp_addr(&self.inner.local_addr()?))
- }
-
- async fn recv(&self, buf: &mut [u8]) -> Result<usize> {
- self.read.lock().await.read_exact(buf).await?;
- Ok(buf.len())
- }
-
- async fn send(&self, buf: &[u8]) -> Result<usize> {
- self.write.lock().await.write_all(buf).await?;
- Ok(buf.len())
- }
-}
-
-#[async_trait]
-impl Listener for TcpListener {
- fn local_endpoint(&self) -> Result<Endpoint> {
- Ok(Endpoint::new_tcp_addr(&self.local_addr()?))
- }
-
- async fn accept(&self) -> Result<Box<dyn Connection>> {
- let (conn, _) = self.accept().await?;
- conn.set_nodelay(true)?;
- Ok(Box::new(TcpConn::new(conn)))
- }
-}
-
-/// Connects to the given TCP address and port.
-pub async fn dial_tcp(addr: &Addr, port: &Port) -> Result<TcpConn> {
- let address = format!("{}:{}", addr, port);
- let conn = TcpStream::connect(address).await?;
- conn.set_nodelay(true)?;
- Ok(TcpConn::new(conn))
-}
-
-/// Listens on the given TCP address and port.
-pub async fn listen_tcp(addr: &Addr, port: &Port) -> Result<TcpListener> {
- let address = format!("{}:{}", addr, port);
- let listener = TcpListener::bind(address).await?;
- Ok(listener)
-}