#tcp #uds #tokio #async

async-stream-connection

AsyncRead and AsyncWrite on TCP as well as Unix sockets

2 stable releases

1.0.1 May 12, 2023

#7 in #uds

Download history 92/week @ 2023-06-11 118/week @ 2023-06-18 167/week @ 2023-06-25 100/week @ 2023-07-02 190/week @ 2023-07-09 138/week @ 2023-07-16 159/week @ 2023-07-23 306/week @ 2023-07-30 310/week @ 2023-08-06 231/week @ 2023-08-13 278/week @ 2023-08-20 105/week @ 2023-08-27 90/week @ 2023-09-03 127/week @ 2023-09-10 133/week @ 2023-09-17 109/week @ 2023-09-24

460 downloads per month
Used in 2 crates

MIT license

17KB
354 lines

Project Status: Active – The project has reached a stable, usable state and is being actively developed. crates.io Released API docs GitHub GitHub Workflow Status

A simple enum that supports tokio::io::AsyncRead and tokio::io::AsyncWrite on TCP as well as Unix sockets.


lib.rs:

A simple enum that supports tokio::io::AsyncRead and tokio::io::AsyncWrite on TCP as well as Unix sockets. * *

  • Example

  • A simple TCP echo server:
  • use async_stream_connection::Listener;
  • use tokio::io::{AsyncReadExt, AsyncWriteExt};
  • #[tokio::main(flavor = "current_thread")]
  • async fn main() -> Result<(), Box> {
  •  let listener = Listener::bind(&"127.0.0.1:8080".parse()?).await?;
    
  •  loop {
    
  •      let (mut socket, _) = listener.accept().await?;
    
  •      tokio::spawn(async move {
    
  •          let mut buf = [0; 1024];
    
  •          // In a loop, read data from the socket and write the data back.
    
  •          loop {
    
  •              let n = match socket.read(&mut buf).await {
    
  •                  // socket closed
    
  •                  Ok(n) if n == 0 => return,
    
  •                  Ok(n) => n,
    
  •                  Err(e) => {
    
  •                      eprintln!("failed to read from socket; err = {:?}", e);
    
  •                      return;
    
  •                  }
    
  •              };
    
  •              // Write the data back
    
  •              if let Err(e) = socket.write_all(&buf[0..n]).await {
    
  •                  eprintln!("failed to write to socket; err = {:?}", e);
    
  •                  return;
    
  •              }
    
  •          }
    
  •      });
    
  •  }
    
  • }
  • 
    

Dependencies

~2–14MB
~115K SLoC