Struct fibers::net::TcpStream [−][src]
pub struct TcpStream { /* fields omitted */ }
A structure which represents a TCP stream between a local socket and a remote socket.
The socket will be closed when the value is dropped.
Note
Non blocking mode is always enabled on this socket.
Roughly speaking, if an operation (read or write) for a socket would block,
it returns the std::io::ErrorKind::WouldBlock
error and
current fiber is suspended until the socket becomes available.
If the fiber has multiple sockets (or other objects which may block),
it will be suspended only the case that all of them are unavailable.
To handle read/write operations over TCP streams in
futures style,
it is preferred to use external crate like handy_async
.
Examples
// See also: fibers/examples/tcp_example.rs use fibers::{Executor, InPlaceExecutor, Spawn}; use fibers::net::{TcpListener, TcpStream}; use fibers::sync::oneshot; use futures::{Future, Stream}; let mut executor = InPlaceExecutor::new().unwrap(); let (addr_tx, addr_rx) = oneshot::channel(); // Spawns TCP listener executor.spawn(TcpListener::bind("127.0.0.1:0".parse().unwrap()) .and_then(|listener| { let addr = listener.local_addr().unwrap(); println!("# Start listening: {}", addr); addr_tx.send(addr).unwrap(); listener.incoming() .for_each(move |(_client, addr)| { println!("# Accepted: {}", addr); Ok(()) }) }) .map_err(|e| panic!("{:?}", e))); // Spawns TCP client let mut monitor = executor.spawn_monitor(addr_rx.map_err(|e| panic!("{:?}", e)) .and_then(|server_addr| { TcpStream::connect(server_addr).and_then(move |mut stream| { use std::io::Write; println!("# Connected: {}", server_addr); stream.write(b"Hello World!"); // This may return `WouldBlock` error Ok(()) }) })); // Runs until the TCP client exits while monitor.poll().unwrap().is_not_ready() { executor.run_once().unwrap(); } println!("# Succeeded");
Methods
impl TcpStream
[src]
impl TcpStream
pub fn connect(addr: SocketAddr) -> Connect
[src]
pub fn connect(addr: SocketAddr) -> Connect
Makes a future to open a TCP connection to a remote host.
pub fn local_addr(&self) -> Result<SocketAddr>
[src]
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the local socket address of this listener.
pub fn peer_addr(&self) -> Result<SocketAddr>
[src]
pub fn peer_addr(&self) -> Result<SocketAddr>
Returns the socket address of the remote peer of this TCP connection.
pub fn take_error(&self) -> Result<Option<Error>>
[src]
pub fn take_error(&self) -> Result<Option<Error>>
Get the value of the SO_ERROR
option on this socket.
This will retrieve the stored error in the underlying socket, clearing the field in the process. This can be useful for checking errors between calls.
pub fn nodelay(&self) -> Result<bool>
[src]
pub fn nodelay(&self) -> Result<bool>
Gets the value of the TCP_NODELAY
option on this socket.
pub fn set_nodelay(&self, nodelay: bool) -> Result<()>
[src]
pub fn set_nodelay(&self, nodelay: bool) -> Result<()>
Sets the value of the TCP_NODELAY
option on this socket.
pub unsafe fn with_inner<F, T>(&self, f: F) -> T where
F: FnOnce(&MioTcpStream) -> T,
[src]
pub unsafe fn with_inner<F, T>(&self, f: F) -> T where
F: FnOnce(&MioTcpStream) -> T,
Calls f
with the reference to the inner socket.
Trait Implementations
impl Clone for TcpStream
[src]
impl Clone for TcpStream
fn clone(&self) -> Self
[src]
fn clone(&self) -> Self
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl Read for TcpStream
[src]
impl Read for TcpStream
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
unsafe fn initializer(&self) -> Initializer
[src]
unsafe fn initializer(&self) -> Initializer
read_initializer
)Determines if this Read
er can work with buffers of uninitialized memory. Read more
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0[src]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to buf
. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Read
. Read more
fn bytes(self) -> Bytes<Self>
1.0.0[src]
fn bytes(self) -> Bytes<Self>
Transforms this Read
instance to an [Iterator
] over its bytes. Read more
fn chars(self) -> Chars<Self>
[src]
fn chars(self) -> Chars<Self>
: Use str::from_utf8 instead: https://doc.rust-lang.org/nightly/std/str/struct.Utf8Error.html#examples
🔬 This is a nightly-only experimental API. (io
)
the semantics of a partial read/write of where errors happen is currently unclear and may change
Transforms this Read
instance to an [Iterator
] over [char
]s. Read more
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
Creates an adaptor which will chain this stream with another. Read more
fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
fn take(self, limit: u64) -> Take<Self>
Creates an adaptor which will read at most limit
bytes from it. Read more
impl Write for TcpStream
[src]
impl Write for TcpStream
fn write(&mut self, buf: &[u8]) -> Result<usize>
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this object, returning how many bytes were written. Read more
fn flush(&mut self) -> Result<()>
[src]
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this write. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0[src]
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Write
. Read more
impl Debug for TcpStream
[src]
impl Debug for TcpStream