pub struct TcpStream<B: RxBuf = NonBuffered> { /* private fields */ }Expand description
A Tcp Stream of bytes. This can be used with AsyncRead, AsyncBufRead and
AsyncWrite
Implementations§
Source§impl TcpStream
impl TcpStream
Sourcepub async fn connect<A: ToSocketAddrs>(addr: A) -> Result<TcpStream, ()>
pub async fn connect<A: ToSocketAddrs>(addr: A) -> Result<TcpStream, ()>
Creates a TCP connection to the specified address.
§Examples
use glommio::{net::TcpStream, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
TcpStream::connect("127.0.0.1:10000").await.unwrap();
})Sourcepub async fn connect_timeout<A: ToSocketAddrs>(
addr: A,
duration: Duration,
) -> Result<TcpStream, ()>
pub async fn connect_timeout<A: ToSocketAddrs>( addr: A, duration: Duration, ) -> Result<TcpStream, ()>
Creates a TCP connection to the specified address with a timeout.
It is an error to pass a zero Duration to this function.
Timeouts are implemented using io_uring’s IORING_OP_LINK_TIMEOUT.
§Examples
use glommio::{net::TcpStream, LocalExecutor};
use std::time::Duration;
let ex = LocalExecutor::default();
ex.run(async move {
TcpStream::connect_timeout("127.0.0.1:10000", Duration::from_secs(10))
.await
.unwrap();
})Sourcepub fn buffered(self) -> TcpStream<Preallocated>
pub fn buffered(self) -> TcpStream<Preallocated>
Creates a buffered TCP connection with default receive buffer.
Sourcepub fn buffered_with<B: Buffered>(self, buf: B) -> TcpStream<B>
pub fn buffered_with<B: Buffered>(self, buf: B) -> TcpStream<B>
Creates a buffered TCP connection with custom receive buffer.
Source§impl<B: RxBuf> TcpStream<B>
impl<B: RxBuf> TcpStream<B>
Sourcepub fn set_read_timeout(&self, dur: Option<Duration>) -> Result<(), ()>
pub fn set_read_timeout(&self, dur: Option<Duration>) -> Result<(), ()>
Sets the read timeout to the timeout specified.
If the value specified is None, then read calls will block
indefinitely. An Err is returned if the zero Duration is
passed to this method.
§Examples
let stream = TcpStream::connect("127.0.0.1:10000").await.unwrap();
stream
.set_read_timeout(Some(Duration::from_secs(1)))
.unwrap();Sourcepub fn set_write_timeout(&self, dur: Option<Duration>) -> Result<(), ()>
pub fn set_write_timeout(&self, dur: Option<Duration>) -> Result<(), ()>
Sets the write timeout to the timeout specified.
If the value specified is None, then write calls will block
indefinitely. An Err is returned if the zero Duration is
passed to this method.
let stream = TcpStream::connect("127.0.0.1:10000").await.unwrap();
stream
.set_write_timeout(Some(Duration::from_secs(1)))
.unwrap();Sourcepub fn read_timeout(&self) -> Option<Duration>
pub fn read_timeout(&self) -> Option<Duration>
Returns the read timeout of this socket.
Sourcepub fn write_timeout(&self) -> Option<Duration>
pub fn write_timeout(&self) -> Option<Duration>
Returns the write timeout of this socket.
Sourcepub async fn shutdown(&self, how: Shutdown) -> Result<(), ()>
pub async fn shutdown(&self, how: Shutdown) -> Result<(), ()>
Shuts down the read, write, or both halves of this connection.
Sourcepub fn set_nodelay(&self, value: bool) -> Result<(), ()>
pub fn set_nodelay(&self, value: 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.
§Examples
use glommio::{net::TcpStream, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let stream = TcpStream::connect("127.0.0.1:10000").await.unwrap();
stream.set_nodelay(true).expect("set_nodelay call failed");
});Sourcepub fn nodelay(&self) -> Result<bool, ()>
pub fn nodelay(&self) -> Result<bool, ()>
Gets the TCP_NODELAY option on this socket.
For more information about this option, see TcpStream::set_nodelay.
§Examples
use glommio::{net::TcpStream, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let stream = TcpStream::connect("127.0.0.1:10000").await.unwrap();
stream.set_nodelay(true).expect("set_nodelay call failed");
assert_eq!(stream.nodelay().unwrap(), true);
});Sourcepub fn ttl(&self) -> Result<u32, ()>
pub fn ttl(&self) -> Result<u32, ()>
Gets the value of the IP_TTL option for this socket.
This option configures the time-to-live field that is used in every packet sent from this socket.
§Examples
use glommio::{net::TcpStream, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let stream = TcpStream::connect("127.0.0.1:10000").await.unwrap();
stream.set_ttl(100).expect("could not set TTL");
assert_eq!(stream.ttl().unwrap(), 100);
});Sourcepub fn set_ttl(&self, ttl: u32) -> Result<(), ()>
pub fn set_ttl(&self, ttl: u32) -> Result<(), ()>
Sets the value of the IP_TTL option for this socket.
This option configures the time-to-live field that is used in every packet sent from this socket.
§Examples
use glommio::{net::TcpStream, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let stream = TcpStream::connect("127.0.0.1:10000").await.unwrap();
stream.set_ttl(100).expect("could not set TTL");
assert_eq!(stream.ttl().unwrap(), 100);
});Sourcepub async fn peek(&self, buf: &mut [u8]) -> Result<usize, ()>
pub async fn peek(&self, buf: &mut [u8]) -> Result<usize, ()>
Receives data on the socket from the remote address to which it is connected, without removing that data from the queue.
On success, returns the number of bytes peeked.
Successive calls return the same data. This is accomplished by passing
MSG_PEEK as a flag to the underlying recv system call.
Sourcepub fn peer_addr(&self) -> Result<SocketAddr, ()>
pub fn peer_addr(&self) -> Result<SocketAddr, ()>
Returns the socket address of the remote peer of this TCP connection.
§Examples
use glommio::{net::TcpStream, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let stream = TcpStream::connect("127.0.0.1:10000").await.unwrap();
println!("My peer: {:?}", stream.peer_addr());
})Sourcepub fn local_addr(&self) -> Result<SocketAddr, ()>
pub fn local_addr(&self) -> Result<SocketAddr, ()>
Returns the socket address of the local half of this TCP connection.
§Examples
use glommio::{net::TcpStream, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let stream = TcpStream::connect("127.0.0.1:10000").await.unwrap();
println!("My peer: {:?}", stream.local_addr());
})Trait Implementations§
Source§impl<B: Buffered + Unpin> AsyncBufRead for TcpStream<B>
impl<B: Buffered + Unpin> AsyncBufRead for TcpStream<B>
Source§impl<B: RxBuf + Unpin> AsyncRead for TcpStream<B>
impl<B: RxBuf + Unpin> AsyncRead for TcpStream<B>
Source§impl<B: RxBuf + Unpin> AsyncWrite for TcpStream<B>
impl<B: RxBuf + Unpin> AsyncWrite for TcpStream<B>
Source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>
buf into the object. Read moreSource§fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
Source§impl FromRawFd for TcpStream
impl FromRawFd for TcpStream
Source§unsafe fn from_raw_fd(fd: RawFd) -> Self
unsafe fn from_raw_fd(fd: RawFd) -> Self
Self from the given raw file
descriptor. Read moreimpl<'__pin, B: RxBuf> Unpin for TcpStream<B>where
PinnedFieldsOf<__Origin<'__pin, B>>: Unpin,
Auto Trait Implementations§
impl<B = NonBuffered> !Freeze for TcpStream<B>
impl<B = NonBuffered> !RefUnwindSafe for TcpStream<B>
impl<B = NonBuffered> !Send for TcpStream<B>
impl<B = NonBuffered> !Sync for TcpStream<B>
impl<B = NonBuffered> !UnwindSafe for TcpStream<B>
impl<B> UnsafeUnpin for TcpStream<B>where
B: UnsafeUnpin,
Blanket Implementations§
Source§impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
Source§fn fill_buf(&mut self) -> FillBuf<'_, Self>where
Self: Unpin,
fn fill_buf(&mut self) -> FillBuf<'_, Self>where
Self: Unpin,
Source§fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntilFuture<'a, Self>where
Self: Unpin,
fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntilFuture<'a, Self>where
Self: Unpin,
Source§fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>where
Self: Unpin,
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>where
Self: Unpin,
buf until a newline (the 0xA byte) or EOF is found. Read moreSource§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
Source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
Source§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
Source§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
Source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf. Read moreSource§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit bytes from it. Read moreSource§impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
Source§fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>where
Self: Unpin,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>where
Self: Unpin,
Source§fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectoredFuture<'a, Self>where
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectoredFuture<'a, Self>where
Self: Unpin,
Source§fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>where
Self: Unpin,
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>where
Self: Unpin,
Source§fn flush(&mut self) -> FlushFuture<'_, Self>where
Self: Unpin,
fn flush(&mut self) -> FlushFuture<'_, Self>where
Self: Unpin,
Source§fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
dyn AsyncWrite + Send + 'a. Read more