TcpStream

Struct TcpStream 

Source
pub struct TcpStream { /* private fields */ }
Expand description

A TCP stream between a local and a remote socket.

A TCP stream can either be created by connecting to an endpoint, via the connect method, or by accepting a connection from a listener.

§Examples

use tokio_uring::net::TcpStream;
use std::net::ToSocketAddrs;

fn main() -> std::io::Result<()> {
    tokio_uring::start(async {
        // Connect to a peer
        let mut stream = TcpStream::connect("127.0.0.1:8080".parse().unwrap()).await?;

        // Write some data.
        let (result, _) = stream.write(b"hello world!".as_slice()).submit().await;
        result.unwrap();

        Ok(())
    })
}

Implementations§

Source§

impl TcpStream

Source

pub async fn connect(addr: SocketAddr) -> Result<TcpStream>

Opens a TCP connection to a remote host at the given SocketAddr

Source

pub fn from_std(socket: TcpStream) -> Self

Creates new TcpStream from a previously bound std::net::TcpStream.

This function is intended to be used to wrap a TCP stream from the standard library in the tokio-uring equivalent. The conversion assumes nothing about the underlying socket; it is left up to the user to decide what socket options are appropriate for their use case.

This can be used in conjunction with socket2’s Socket interface to configure a socket before it’s handed off, such as setting options like reuse_address or binding to multiple addresses.

Source

pub async fn read<T: BoundedBufMut>(&self, buf: T) -> BufResult<usize, T>

Read some data from the stream into the buffer.

Returns the original buffer and quantity of data read.

Source

pub async fn read_fixed<T>(&self, buf: T) -> BufResult<usize, T>
where T: BoundedBufMut<BufMut = FixedBuf>,

Read some data from the stream into a registered buffer.

Like read, but using a pre-mapped buffer registered with FixedBufRegistry.

§Errors

In addition to errors that can be reported by read, this operation fails if the buffer is not registered in the current tokio-uring runtime.

Source

pub fn write<T: BoundedBuf>(&self, buf: T) -> UnsubmittedWrite<T>

Write some data to the stream from the buffer.

Returns the original buffer and quantity of data written.

Source

pub async fn write_all<T: BoundedBuf>(&self, buf: T) -> BufResult<(), T>

Attempts to write an entire buffer to the stream.

This method will continuously call write until there is no more data to be written or an error is returned. This method will not return until the entire buffer has been successfully written or an error has occurred.

If the buffer contains no data, this will never call write.

§Errors

This function will return the first error that write returns.

§Examples
use std::net::SocketAddr;
use tokio_uring::net::TcpListener;
use tokio_uring::buf::BoundedBuf;

let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();

tokio_uring::start(async {
    let listener = TcpListener::bind(addr).unwrap();

    println!("Listening on {}", listener.local_addr().unwrap());

    loop {
        let (stream, _) = listener.accept().await.unwrap();
        tokio_uring::spawn(async move {
            let mut n = 0;
            let mut buf = vec![0u8; 4096];
            loop {
                let (result, nbuf) = stream.read(buf).await;
                buf = nbuf;
                let read = result.unwrap();
                if read == 0 {
                    break;
                }

                let (res, slice) = stream.write_all(buf.slice(..read)).await;
                let _ = res.unwrap();
                buf = slice.into_inner();
                n += read;
            }
        });
    }
});
Source

pub async fn write_fixed<T>(&self, buf: T) -> BufResult<usize, T>
where T: BoundedBuf<Buf = FixedBuf>,

Writes data into the socket from a registered buffer.

Like write, but using a pre-mapped buffer registered with FixedBufRegistry.

§Errors

In addition to errors that can be reported by write, this operation fails if the buffer is not registered in the current tokio-uring runtime.

Source

pub async fn write_fixed_all<T>(&self, buf: T) -> BufResult<(), T>
where T: BoundedBuf<Buf = FixedBuf>,

Attempts to write an entire buffer to the stream.

This method will continuously call write_fixed until there is no more data to be written or an error is returned. This method will not return until the entire buffer has been successfully written or an error has occurred.

If the buffer contains no data, this will never call write_fixed.

§Errors

This function will return the first error that write_fixed returns.

Source

pub async fn writev<T: BoundedBuf>( &self, buf: Vec<T>, ) -> BufResult<usize, Vec<T>>

Writes data from multiple buffers into this socket using the scatter/gather IO style.

This function will attempt to write the entire contents of bufs, but the entire write may not succeed, or the write may also generate an error. The bytes will be written starting at the specified offset.

§Return

The method returns the operation result and the same array of buffers passed in as an argument. A return value of 0 typically means that the underlying socket is no longer able to accept bytes and will likely not be able to in the future as well, or that the buffer provided is empty.

§Errors

Each call to write may generate an I/O error indicating that the operation could not be completed. If an error is returned then no bytes in the buffer were written to this writer.

It is not considered an error if the entire buffer could not be written to this writer.

Source

pub fn shutdown(&self, how: Shutdown) -> Result<()>

Shuts down the read, write, or both halves of this connection.

This function will cause all pending and future I/O on the specified portions to return immediately with an appropriate value.

Source

pub fn set_nodelay(&self, nodelay: bool) -> Result<()>

Sets the value of the TCP_NODELAY option on this socket.

If set, this option disables the Nagle algorithm. This means that segments are always sent as soon as possible, even if there is only a small amount of data. When not set, data is buffered until there is a sufficient amount to send out, thereby avoiding the frequent sending of small packets.

Trait Implementations§

Source§

impl AsRawFd for TcpStream

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl FromRawFd for TcpStream

Source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

Constructs a new instance of Self from the given raw file descriptor. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.