pub struct TcpStream { /* private fields */ }
Expand description
A TCP stream between a local and a remote socket, akin to
std::net::TcpStream
All I/O operations provided by mtcp_rs::TcpStream
are “blocking”, but –
unlike the std::net
implementation – proper timeout and
cancellation support is available. The mtcp_rs::TcpStream
is tied
to an mtcp_rs::TcpManager
instance.
The TCP stream is created by connect()
ing to a
remote host, or directly from()
an existing
mtcp_rs::TcpConnection
.
If the timeout
parameter was set to Some(Duration)
and if the I/O
operation does not complete before the specified timeout period
expires, then the pending I/O operation will be aborted and fail with an
TcpError::TimedOut
error.
Functions like Read::read()
and
Write::write()
, which do not have an
explicit timeout
parameter, implicitly use the timeouts that have been
set up via the
set_default_timeouts()
function.
Initially, these timeouts are disabled.
Implementations§
Source§impl TcpStream
impl TcpStream
Sourcepub fn from(
manager: &Rc<TcpManager>,
connection: TcpConnection,
) -> IoResult<Self>
pub fn from( manager: &Rc<TcpManager>, connection: TcpConnection, ) -> IoResult<Self>
Initialize a new TcpStream
from an existing TcpConnection
instance.
TcpConnection
instances are usually obtained by
accept()
ing incoming TCP connections
via a bound TcpListener
.
The new TcpStream
is tied to the specified TcpManager
instance.
Sourcepub fn set_default_timeouts(
&mut self,
timeout_rd: Option<Duration>,
timeout_wr: Option<Duration>,
)
pub fn set_default_timeouts( &mut self, timeout_rd: Option<Duration>, timeout_wr: Option<Duration>, )
Set up the default timeouts, to be used by functions like
Read::read()
and
Write::write()
.
Sourcepub fn peer_addr(&self) -> Option<SocketAddr>
pub fn peer_addr(&self) -> Option<SocketAddr>
Get the peer socket address of this TCP stream.
Sourcepub fn local_addr(&self) -> Option<SocketAddr>
pub fn local_addr(&self) -> Option<SocketAddr>
Get the local socket address of this TCP stream.
Sourcepub fn shutdown(&self, how: Shutdown) -> IoResult<()>
pub fn shutdown(&self, how: Shutdown) -> IoResult<()>
Shuts down the read, write, or both halves of this TCP stream.
Sourcepub fn connect(
manager: &Rc<TcpManager>,
addr: SocketAddr,
timeout: Option<Duration>,
) -> Result<Self, TcpError>
pub fn connect( manager: &Rc<TcpManager>, addr: SocketAddr, timeout: Option<Duration>, ) -> Result<Self, TcpError>
Opens a new TCP connection to the remote host at the specified address.
An optional timeout can be specified, after which the operation is going to fail, if the connection could not be established yet.
The new TcpStream
is tied to the specified TcpManager
instance.
Sourcepub fn read_timeout(
&mut self,
buffer: &mut [u8],
timeout: Option<Duration>,
) -> Result<usize, TcpError>
pub fn read_timeout( &mut self, buffer: &mut [u8], timeout: Option<Duration>, ) -> Result<usize, TcpError>
Read the next “chunk” of incoming data from the TCP stream into the specified destination buffer.
This function attempts to read a maximum of buffer.len()
bytes, but
fewer bytes may actually be read! Specifically, the function waits
until some data become available for reading, or the end of the
stream (or an error) is encountered. It then reads as many bytes as are
available and returns immediately. The function does not wait any
longer, even if the buffer
is not filled completely.
An optional timeout can be specified, after which the operation is going to fail, if still no data is available for reading.
Returns the number of bytes that have been pulled from the stream into
the buffer, which is less than or equal to buffer.len()
. A zero
return value indicates the end of the stream. Otherwise, more data may
become available for reading soon!
Sourcepub fn read_all_timeout<F>(
&mut self,
buffer: &mut Vec<u8>,
timeout: Option<Duration>,
chunk_size: Option<NonZeroUsize>,
maximum_length: Option<NonZeroUsize>,
fn_complete: F,
) -> Result<(), TcpError>
pub fn read_all_timeout<F>( &mut self, buffer: &mut Vec<u8>, timeout: Option<Duration>, chunk_size: Option<NonZeroUsize>, maximum_length: Option<NonZeroUsize>, fn_complete: F, ) -> Result<(), TcpError>
Read all incoming data from the TCP stream into the specified destination buffer.
This function keeps on reading from the stream,
until the input data has been read completely, as defined by the
fn_complete
closure, or an error is encountered. All input data is
appended to the given buffer
, extending the buffer as needed. The
fn_complete
closure is invoked every time that a new “chunk” of input
was received. Unless the closure returned true
, the function waits
for more input. If the end of the stream is encountered while the data
still is incomplete, the function fails.
The closure fn_complete
takes a single parameter, a reference to the
current buffer, which contains all data that has been read so far.
That closure shall return true
if and only if the data in the buffer
is considered “complete”.
An optional timeout can be specified, after which the operation is going to fail, if the data still is not complete.
The optional chunk size specifies the maximum amount of data that can be read at once.
An optional maximum length can be specified. If the total size exceeds this limit before the data is complete, the function fails.
Sourcepub fn write_timeout(
&mut self,
buffer: &[u8],
timeout: Option<Duration>,
) -> Result<usize, TcpError>
pub fn write_timeout( &mut self, buffer: &[u8], timeout: Option<Duration>, ) -> Result<usize, TcpError>
Write the next “chunk” of outgoing data from the specified source buffer to the TCP stream.
This function attempts to write a maximum of buffer.len()
bytes, but
fewer bytes may actually be written! Specifically, the function waits
until some data can be written, the stream is closed by the peer, or
an error is encountered. It then writes as many bytes as possible to
the stream. The function does not wait any longer, even if not
all data in buffer
could be written yet.
An optional timeout can be specified, after which the operation is going to fail, if still no data could be written.
Returns the number of bytes that have been pushed from the buffer into
the stream, which is less than or equal to buffer.len()
. A zero
return value indicates that the stream was closed. Otherwise, it may be
possible to write more data soon!
Sourcepub fn write_all_timeout(
&mut self,
buffer: &[u8],
timeout: Option<Duration>,
) -> Result<(), TcpError>
pub fn write_all_timeout( &mut self, buffer: &[u8], timeout: Option<Duration>, ) -> Result<(), TcpError>
Write all outgoing data from the specified source buffer to the TCP stream.
This function keeps on writing to the stream, until the output data has been written completely, the peer closes the stream, or an error is encountered. If the stream is closed before all data could be written, the function fails.
An optional timeout can be specified, after which the operation is going to fail, if the data still was not written completely.
Trait Implementations§
Source§impl Read for TcpStream
impl Read for TcpStream
Source§fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl Write for TcpStream
impl Write for TcpStream
Source§fn write(&mut self, buf: &[u8]) -> IoResult<usize>
fn write(&mut self, buf: &[u8]) -> IoResult<usize>
Source§fn flush(&mut self) -> IoResult<()>
fn flush(&mut self) -> IoResult<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)