Skip to main content

TcpSocket

Struct TcpSocket 

Source
pub struct TcpSocket(/* private fields */);
Expand description

The TCP socket type for the STD network stack.

Implementations§

Source§

impl TcpSocket

Source

pub const fn new(socket: Async<TcpStream>) -> Self

Create a new TCP socket from the given async TCP stream.

§Arguments
  • socket: The async TCP stream to wrap.
Source

pub fn release(self) -> Async<TcpStream>

Release the underlying async TCP stream.

Methods from Deref<Target = Async<TcpStream>>§

Source

pub fn get_ref(&self) -> &T

Gets a reference to the inner I/O handle.

§Examples
use async_io::Async;
use std::net::TcpListener;

let listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;
let inner = listener.get_ref();
Source

pub fn readable(&self) -> Readable<'_, T>

Waits until the I/O handle is readable.

This method completes when a read operation on this I/O handle wouldn’t block.

§Examples
use async_io::Async;
use std::net::TcpListener;

let mut listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;

// Wait until a client can be accepted.
listener.readable().await?;
Source

pub fn readable_owned(self: Arc<Async<T>>) -> ReadableOwned<T>

Waits until the I/O handle is readable.

This method completes when a read operation on this I/O handle wouldn’t block.

Source

pub fn writable(&self) -> Writable<'_, T>

Waits until the I/O handle is writable.

This method completes when a write operation on this I/O handle wouldn’t block.

§Examples
use async_io::Async;
use std::net::{TcpStream, ToSocketAddrs};

let addr = "example.com:80".to_socket_addrs()?.next().unwrap();
let stream = Async::<TcpStream>::connect(addr).await?;

// Wait until the stream is writable.
stream.writable().await?;
Source

pub fn writable_owned(self: Arc<Async<T>>) -> WritableOwned<T>

Waits until the I/O handle is writable.

This method completes when a write operation on this I/O handle wouldn’t block.

Source

pub fn poll_readable(&self, cx: &mut Context<'_>) -> Poll<Result<(), Error>>

Polls the I/O handle for readability.

When this method returns Poll::Ready, that means the OS has delivered an event indicating readability since the last time this task has called the method and received Poll::Pending.

§Caveats

Two different tasks should not call this method concurrently. Otherwise, conflicting tasks will just keep waking each other in turn, thus wasting CPU time.

Note that the AsyncRead implementation for Async also uses this method.

§Examples
use async_io::Async;
use std::future::poll_fn;
use std::net::TcpListener;

let mut listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;

// Wait until a client can be accepted.
poll_fn(|cx| listener.poll_readable(cx)).await?;
Source

pub fn poll_writable(&self, cx: &mut Context<'_>) -> Poll<Result<(), Error>>

Polls the I/O handle for writability.

When this method returns Poll::Ready, that means the OS has delivered an event indicating writability since the last time this task has called the method and received Poll::Pending.

§Caveats

Two different tasks should not call this method concurrently. Otherwise, conflicting tasks will just keep waking each other in turn, thus wasting CPU time.

Note that the AsyncWrite implementation for Async also uses this method.

§Examples
use async_io::Async;
use std::future::poll_fn;
use std::net::{TcpStream, ToSocketAddrs};

let addr = "example.com:80".to_socket_addrs()?.next().unwrap();
let stream = Async::<TcpStream>::connect(addr).await?;

// Wait until the stream is writable.
poll_fn(|cx| stream.poll_writable(cx)).await?;
Source

pub async fn read_with<R>( &self, op: impl FnMut(&T) -> Result<R, Error>, ) -> Result<R, Error>

Performs a read operation asynchronously.

The I/O handle is registered in the reactor and put in non-blocking mode. This method invokes the op closure in a loop until it succeeds or returns an error other than io::ErrorKind::WouldBlock. In between iterations of the loop, it waits until the OS sends a notification that the I/O handle is readable.

The closure receives a shared reference to the I/O handle.

§Examples
use async_io::Async;
use std::net::TcpListener;

let listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;

// Accept a new client asynchronously.
let (stream, addr) = listener.read_with(|l| l.accept()).await?;
Source

pub async fn write_with<R>( &self, op: impl FnMut(&T) -> Result<R, Error>, ) -> Result<R, Error>

Performs a write operation asynchronously.

The I/O handle is registered in the reactor and put in non-blocking mode. This method invokes the op closure in a loop until it succeeds or returns an error other than io::ErrorKind::WouldBlock. In between iterations of the loop, it waits until the OS sends a notification that the I/O handle is writable.

The closure receives a shared reference to the I/O handle.

§Examples
use async_io::Async;
use std::net::UdpSocket;

let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 8000))?;
socket.get_ref().connect("127.0.0.1:9000")?;

let msg = b"hello";
let len = socket.write_with(|s| s.send(msg)).await?;
Source

pub async fn peek(&self, buf: &mut [u8]) -> Result<usize, Error>

Reads data from the stream without removing it from the buffer.

Returns the number of bytes read. Successive calls of this method read the same data.

§Examples
use async_io::Async;
use futures_lite::{io::AsyncWriteExt, stream::StreamExt};
use std::net::{TcpStream, ToSocketAddrs};

let addr = "example.com:80".to_socket_addrs()?.next().unwrap();
let mut stream = Async::<TcpStream>::connect(addr).await?;

stream
    .write_all(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
    .await?;

let mut buf = [0u8; 1024];
let len = stream.peek(&mut buf).await?;

Trait Implementations§

Source§

impl Deref for TcpSocket

Source§

type Target = Async<TcpStream>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl ErrorType for TcpSocket

Source§

type Error = Error

Error type of all the IO operations on this type.
Source§

impl ErrorType for &TcpSocket

Source§

type Error = Error

Error type of all the IO operations on this type.
Source§

impl Read for TcpSocket

Source§

async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>

Read some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Source§

async fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>

Read the exact number of bytes required to fill buf. Read more
Source§

impl Read for &TcpSocket

Source§

async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>

Read some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Source§

async fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>

Read the exact number of bytes required to fill buf. Read more
Source§

impl Readable for TcpSocket

Source§

async fn readable(&mut self) -> Result<(), Self::Error>

Source§

impl Readable for &TcpSocket

Source§

async fn readable(&mut self) -> Result<(), Self::Error>

Source§

impl TcpShutdown for TcpSocket

Source§

async fn close(&mut self, what: Close) -> Result<(), Self::Error>

Gracefully shutdown either or both the read and write halves of the socket. Read more
Source§

async fn abort(&mut self) -> Result<(), Self::Error>

Abort the connection, sending an RST packet to the peer Read more
Source§

impl TcpSplit for TcpSocket

Source§

type Read<'a> = &'a TcpSocket where Self: 'a

Source§

type Write<'a> = &'a TcpSocket where Self: 'a

Source§

fn split(&mut self) -> (Self::Read<'_>, Self::Write<'_>)

Source§

impl Write for TcpSocket

Source§

async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>

Write a buffer into this writer, returning how many bytes were written. Read more
Source§

async fn flush(&mut self) -> Result<(), Self::Error>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
Source§

async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>

Write an entire buffer into this writer. Read more
Source§

impl Write for &TcpSocket

Source§

async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>

Write a buffer into this writer, returning how many bytes were written. Read more
Source§

async fn flush(&mut self) -> Result<(), Self::Error>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
Source§

async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>

Write an entire buffer into this writer. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.