The types defined in this module are designed to closely follow the APIs of the
analogous types in std::net
. But rather than implementing synchronous traits
like std::io::{Read, Write}
, these types implement the asychronous versions
provided by the futures-preview
crate, i.e. futures::io::{AsyncRead, AsyncWrite}
.
When using async
/await
syntax, the experience should be quite similar to
traditional blocking code that uses std::net
.
Because futures-preview is currently unstable, this crate requires nightly Rust.
TCP Server
#![feature(async_await, await_macro, futures_api)] use romio::tcp::{TcpListener, TcpStream}; use futures::prelude::*; async fn say_hello(mut stream: TcpStream) { await!(stream.write_all(b"Shall I hear more, or shall I speak at this?")); } async fn listen() -> Result<(), Box<dyn std::error::Error + 'static>> { let socket_addr = "127.0.0.1:8080".parse()?; let listener = TcpListener::bind(&socket_addr)?; let mut incoming = listener.incoming(); // accept connections and process them serially while let Some(stream) = await!(incoming.next()) { await!(say_hello(stream?)); } Ok(()) }
TCP Client
#![feature(async_await, await_macro, futures_api)] use std::error::Error; use futures::prelude::*; use romio::tcp::{TcpListener, TcpStream}; async fn receive_sonnet() -> Result<(), Box<dyn Error + 'static>> { let socket_addr = "127.0.0.1:8080".parse()?; let mut buffer = vec![]; let mut stream = await!(TcpStream::connect(&socket_addr))?; await!(stream.read(&mut buffer))?; println!("{:?}", buffer); Ok(()) }
tcp |
Async TCP bindings. |
udp |
Async UDP bindings. |
uds |
Async UDS (Unix Domain Sockets) bindings. |
TcpListener |
A TCP socket server, listening for connections. |
TcpStream |
A TCP stream between a local and a remote socket. |
UdpSocket |
A UDP socket. |