mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-03 00:00:05 +02:00
Doc improvements (#46)
* small doc cleanups in PollEvented * small doc cleanups in IoToken * improve crate level documentation - Add links to the futures, mio and tokio-uds crates. - Add links to various structs and types mentioned. - use eprintln for error reporting in the example. * improvements to the UdpSocket documentation - Fixed links usage. - Removed references to a no longer existing `Window` struct. - Made notes about using functions in context of a future. * documentation improvements to UdpFramed and UdpCodec - Since HTTP uses TCP (QUIC aside) using it as an example in an UDP protocol feels wrong. - Make the note of tampering with the underlying streams more explicit. * update reactor module level documentation Adds an explanation of every public struct. * expand Handle and Remote documentation * expand net module documentation Adds an explanation of every public struct and how they work together. * update TcpListener documentation Reorder the various option methods; get first then set. Note about panicing added to poll_read. * remove mention of none-existing future R * improve documentation of TcpStream * fix UdpSocket doc This when wrong when merging various commits.
This commit is contained in:
committed by
Alex Crichton
parent
0b54557796
commit
c801584d24
+36
-1
@@ -1,7 +1,42 @@
|
||||
//! TCP/UDP bindings for `tokio-core`
|
||||
//! TCP/UDP bindings for `tokio`.
|
||||
//!
|
||||
//! This module contains the TCP/UDP networking types, similar to the standard
|
||||
//! library, which can be used to implement networking protocols.
|
||||
//!
|
||||
//! # TCP
|
||||
//!
|
||||
//! Connecting to an address, via TCP, can be done using [`TcpStream`]'s
|
||||
//! [`connect`] method, which returns [`TcpStreamNew`]. `TcpStreamNew`
|
||||
//! implements a future which returns a `TcpStream`.
|
||||
//!
|
||||
//! To listen on an address [`TcpListener`] can be used. `TcpListener`'s
|
||||
//! [`incoming`][incoming_method] method can be used to accept new connections.
|
||||
//! It return the [`Incoming`] struct, which implements a stream which returns
|
||||
//! `TcpStream`s.
|
||||
//!
|
||||
//! [`TcpStream`]: struct.TcpStream.html
|
||||
//! [`connect`]: struct.TcpStream.html#method.connect
|
||||
//! [`TcpStreamNew`]: struct.TcpStreamNew.html
|
||||
//! [`TcpListener`]: struct.TcpListener.html
|
||||
//! [incoming_method]: struct.TcpListener.html#method.incoming
|
||||
//! [`Incoming`]: struct.Incoming.html
|
||||
//!
|
||||
//! # UDP
|
||||
//!
|
||||
//! The main struct for UDP is the [`UdpSocket`], which represents a UDP socket.
|
||||
//! Reading and writing to it can be done using futures, which return the
|
||||
//! [`RecvDgram`] and [`SendDgram`] structs respectively.
|
||||
//!
|
||||
//! For convience it's also possible to convert raw datagrams into higher-level
|
||||
//! frames. This done with [`UdpFramed`], created by calling [`framed`] on a
|
||||
//! [`UdpSocket`], and using the [`UdpCodec`].
|
||||
//!
|
||||
//! [`UdpSocket`]: struct.UdpSocket.html
|
||||
//! [`RecvDgram`]: struct.RecvDgram.html
|
||||
//! [`SendDgram`]: struct.SendDgram.html
|
||||
//! [`UdpFramed`]: struct.UdpFramed.html
|
||||
//! [`framed`]: struct.UdpSocket.html#method.framed
|
||||
//! [`UdpCodec`]: trait.UdpCodec.html
|
||||
|
||||
mod tcp;
|
||||
mod udp;
|
||||
|
||||
+101
-74
@@ -148,6 +148,11 @@ impl TcpListener {
|
||||
}
|
||||
|
||||
/// Test whether this socket is ready to be read or not.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called outside the context of a future's
|
||||
/// task.
|
||||
pub fn poll_read(&self) -> Async<()> {
|
||||
self.io.poll_read()
|
||||
}
|
||||
@@ -169,6 +174,15 @@ impl TcpListener {
|
||||
Incoming { inner: self }
|
||||
}
|
||||
|
||||
/// Gets the value of the `IP_TTL` option for this socket.
|
||||
///
|
||||
/// For more information about this option, see [`set_ttl`].
|
||||
///
|
||||
/// [`set_ttl`]: #method.set_ttl
|
||||
pub fn ttl(&self) -> io::Result<u32> {
|
||||
self.io.get_ref().ttl()
|
||||
}
|
||||
|
||||
/// Sets the value for the `IP_TTL` option on this socket.
|
||||
///
|
||||
/// This value sets the time-to-live field that is used in every packet sent
|
||||
@@ -177,13 +191,13 @@ impl TcpListener {
|
||||
self.io.get_ref().set_ttl(ttl)
|
||||
}
|
||||
|
||||
/// Gets the value of the `IP_TTL` option for this socket.
|
||||
/// Gets the value of the `IPV6_V6ONLY` option for this socket.
|
||||
///
|
||||
/// For more information about this option, see [`set_ttl`][link].
|
||||
/// For more information about this option, see [`set_only_v6`].
|
||||
///
|
||||
/// [link]: #method.set_ttl
|
||||
pub fn ttl(&self) -> io::Result<u32> {
|
||||
self.io.get_ref().ttl()
|
||||
/// [`set_only_v6`]: #method.set_only_v6
|
||||
pub fn only_v6(&self) -> io::Result<bool> {
|
||||
self.io.get_ref().only_v6()
|
||||
}
|
||||
|
||||
/// Sets the value for the `IPV6_V6ONLY` option on this socket.
|
||||
@@ -197,15 +211,6 @@ impl TcpListener {
|
||||
pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
|
||||
self.io.get_ref().set_only_v6(only_v6)
|
||||
}
|
||||
|
||||
/// Gets the value of the `IPV6_V6ONLY` option for this socket.
|
||||
///
|
||||
/// For more information about this option, see [`set_only_v6`][link].
|
||||
///
|
||||
/// [link]: #method.set_only_v6
|
||||
pub fn only_v6(&self) -> io::Result<bool> {
|
||||
self.io.get_ref().only_v6()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for TcpListener {
|
||||
@@ -225,10 +230,12 @@ impl Stream for Incoming {
|
||||
|
||||
/// An I/O object representing a TCP stream connected to a remote endpoint.
|
||||
///
|
||||
/// A TCP stream can either be created by connecting to an endpoint or by
|
||||
/// accepting a connection from a listener. Inside the stream is access to the
|
||||
/// raw underlying I/O object as well as streams for the read/write
|
||||
/// notifications on the stream itself.
|
||||
/// A TCP stream can either be created by connecting to an endpoint, via the
|
||||
/// [`connect`] method, or by [accepting] a connection from a [listener].
|
||||
///
|
||||
/// [`connect`]: struct.TcpStream.html#method.connect
|
||||
/// [accepting]: struct.TcpListener.html#method.accept
|
||||
/// [listener]: struct.TcpListener.html
|
||||
pub struct TcpStream {
|
||||
io: PollEvented<mio::net::TcpStream>,
|
||||
}
|
||||
@@ -252,9 +259,8 @@ impl TcpStream {
|
||||
///
|
||||
/// This function will create a new TCP socket and attempt to connect it to
|
||||
/// the `addr` provided. The returned future will be resolved once the
|
||||
/// stream has successfully connected. If an error happens during the
|
||||
/// connection or during the socket creation, that error will be returned to
|
||||
/// the future instead.
|
||||
/// stream has successfully connected, or it wil return an error if one
|
||||
/// occurs.
|
||||
pub fn connect(addr: &SocketAddr, handle: &Handle) -> TcpStreamNew {
|
||||
let inner = match mio::net::TcpStream::connect(addr) {
|
||||
Ok(tcp) => TcpStream::new(tcp, handle),
|
||||
@@ -273,9 +279,10 @@ impl TcpStream {
|
||||
|
||||
/// Create a new `TcpStream` from a `net::TcpStream`.
|
||||
///
|
||||
/// This function will convert a TCP stream in the standard library to a TCP
|
||||
/// stream ready to be used with the provided event loop handle. The object
|
||||
/// returned is associated with the event loop and ready to perform I/O.
|
||||
/// This function will convert a TCP stream created by the standard library
|
||||
/// to a TCP stream ready to be used with the provided event loop handle.
|
||||
/// The stream returned is associated with the event loop and ready to
|
||||
/// perform I/O.
|
||||
pub fn from_stream(stream: net::TcpStream, handle: &Handle)
|
||||
-> io::Result<TcpStream> {
|
||||
let inner = try!(mio::net::TcpStream::from_stream(stream));
|
||||
@@ -313,22 +320,28 @@ impl TcpStream {
|
||||
Box::new(state)
|
||||
}
|
||||
|
||||
/// Test whether this socket is ready to be read or not.
|
||||
/// Test whether this stream is ready to be read or not.
|
||||
///
|
||||
/// If the socket is *not* readable then the current task is scheduled to
|
||||
/// get a notification when the socket does become readable. That is, this
|
||||
/// is only suitable for calling in a `Future::poll` method and will
|
||||
/// automatically handle ensuring a retry once the socket is readable again.
|
||||
/// If the stream is *not* readable then the current task is scheduled to
|
||||
/// get a notification when the stream does become readable.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called outside the context of a future's
|
||||
/// task.
|
||||
pub fn poll_read(&self) -> Async<()> {
|
||||
self.io.poll_read()
|
||||
}
|
||||
|
||||
/// Test whether this socket is ready to be written to or not.
|
||||
/// Test whether this stream is ready to be written or not.
|
||||
///
|
||||
/// If the socket is *not* writable then the current task is scheduled to
|
||||
/// get a notification when the socket does become writable. That is, this
|
||||
/// is only suitable for calling in a `Future::poll` method and will
|
||||
/// automatically handle ensuring a retry once the socket is writable again.
|
||||
/// If the stream is *not* writable then the current task is scheduled to
|
||||
/// get a notification when the stream does become writable.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called outside the context of a future's
|
||||
/// task.
|
||||
pub fn poll_write(&self) -> Async<()> {
|
||||
self.io.poll_write()
|
||||
}
|
||||
@@ -352,6 +365,15 @@ impl TcpStream {
|
||||
self.io.get_ref().shutdown(how)
|
||||
}
|
||||
|
||||
/// Gets the value of the `TCP_NODELAY` option on this socket.
|
||||
///
|
||||
/// For more information about this option, see [`set_nodelay`].
|
||||
///
|
||||
/// [`set_nodelay`]: #method.set_nodelay
|
||||
pub fn nodelay(&self) -> io::Result<bool> {
|
||||
self.io.get_ref().nodelay()
|
||||
}
|
||||
|
||||
/// Sets the value of the `TCP_NODELAY` option on this socket.
|
||||
///
|
||||
/// If set, this option disables the Nagle algorithm. This means that
|
||||
@@ -363,13 +385,13 @@ impl TcpStream {
|
||||
self.io.get_ref().set_nodelay(nodelay)
|
||||
}
|
||||
|
||||
/// Gets the value of the `TCP_NODELAY` option on this socket.
|
||||
/// Gets the value of the `SO_RCVBUF` option on this socket.
|
||||
///
|
||||
/// For more information about this option, see [`set_nodelay`][link].
|
||||
/// For more information about this option, see [`set_recv_buffer_size`].
|
||||
///
|
||||
/// [link]: #method.set_nodelay
|
||||
pub fn nodelay(&self) -> io::Result<bool> {
|
||||
self.io.get_ref().nodelay()
|
||||
/// [`set_recv_buffer_size`]: #tymethod.set_recv_buffer_size
|
||||
pub fn recv_buffer_size(&self) -> io::Result<usize> {
|
||||
self.io.get_ref().recv_buffer_size()
|
||||
}
|
||||
|
||||
/// Sets the value of the `SO_RCVBUF` option on this socket.
|
||||
@@ -380,14 +402,13 @@ impl TcpStream {
|
||||
self.io.get_ref().set_recv_buffer_size(size)
|
||||
}
|
||||
|
||||
/// Gets the value of the `SO_RCVBUF` option on this socket.
|
||||
/// Gets the value of the `SO_SNDBUF` option on this socket.
|
||||
///
|
||||
/// For more information about this option, see
|
||||
/// [`set_recv_buffer_size`][link].
|
||||
/// For more information about this option, see [`set_send_buffer`].
|
||||
///
|
||||
/// [link]: #tymethod.set_recv_buffer_size
|
||||
pub fn recv_buffer_size(&self) -> io::Result<usize> {
|
||||
self.io.get_ref().recv_buffer_size()
|
||||
/// [`set_send_buffer`]: #tymethod.set_send_buffer
|
||||
pub fn send_buffer_size(&self) -> io::Result<usize> {
|
||||
self.io.get_ref().send_buffer_size()
|
||||
}
|
||||
|
||||
/// Sets the value of the `SO_SNDBUF` option on this socket.
|
||||
@@ -398,13 +419,14 @@ impl TcpStream {
|
||||
self.io.get_ref().set_send_buffer_size(size)
|
||||
}
|
||||
|
||||
/// Gets the value of the `SO_SNDBUF` option on this socket.
|
||||
/// Returns whether keepalive messages are enabled on this socket, and if so
|
||||
/// the duration of time between them.
|
||||
///
|
||||
/// For more information about this option, see [`set_send_buffer`][link].
|
||||
/// For more information about this option, see [`set_keepalive`].
|
||||
///
|
||||
/// [link]: #tymethod.set_send_buffer
|
||||
pub fn send_buffer_size(&self) -> io::Result<usize> {
|
||||
self.io.get_ref().send_buffer_size()
|
||||
/// [`set_keepalive`]: #tymethod.set_keepalive
|
||||
pub fn keepalive(&self) -> io::Result<Option<Duration>> {
|
||||
self.io.get_ref().keepalive()
|
||||
}
|
||||
|
||||
/// Sets whether keepalive messages are enabled to be sent on this socket.
|
||||
@@ -423,14 +445,13 @@ impl TcpStream {
|
||||
self.io.get_ref().set_keepalive(keepalive)
|
||||
}
|
||||
|
||||
/// Returns whether keepalive messages are enabled on this socket, and if so
|
||||
/// the duration of time between them.
|
||||
/// Gets the value of the `IP_TTL` option for this socket.
|
||||
///
|
||||
/// For more information about this option, see [`set_keepalive`][link].
|
||||
/// For more information about this option, see [`set_ttl`].
|
||||
///
|
||||
/// [link]: #tymethod.set_keepalive
|
||||
pub fn keepalive(&self) -> io::Result<Option<Duration>> {
|
||||
self.io.get_ref().keepalive()
|
||||
/// [`set_ttl`]: #tymethod.set_ttl
|
||||
pub fn ttl(&self) -> io::Result<u32> {
|
||||
self.io.get_ref().ttl()
|
||||
}
|
||||
|
||||
/// Sets the value for the `IP_TTL` option on this socket.
|
||||
@@ -441,13 +462,13 @@ impl TcpStream {
|
||||
self.io.get_ref().set_ttl(ttl)
|
||||
}
|
||||
|
||||
/// Gets the value of the `IP_TTL` option for this socket.
|
||||
/// Gets the value of the `IPV6_V6ONLY` option for this socket.
|
||||
///
|
||||
/// For more information about this option, see [`set_ttl`][link].
|
||||
/// For more information about this option, see [`set_only_v6`].
|
||||
///
|
||||
/// [link]: #tymethod.set_ttl
|
||||
pub fn ttl(&self) -> io::Result<u32> {
|
||||
self.io.get_ref().ttl()
|
||||
/// [`set_only_v6`]: #tymethod.set_only_v6
|
||||
pub fn only_v6(&self) -> io::Result<bool> {
|
||||
self.io.get_ref().only_v6()
|
||||
}
|
||||
|
||||
/// Sets the value for the `IPV6_V6ONLY` option on this socket.
|
||||
@@ -462,24 +483,30 @@ impl TcpStream {
|
||||
self.io.get_ref().set_only_v6(only_v6)
|
||||
}
|
||||
|
||||
/// Gets the value of the `IPV6_V6ONLY` option for this socket.
|
||||
/// Reads the linger duration for this socket by getting the `SO_LINGER`
|
||||
/// option.
|
||||
///
|
||||
/// For more information about this option, see [`set_only_v6`][link].
|
||||
/// For more information about this option, see [`set_linger`].
|
||||
///
|
||||
/// [link]: #tymethod.set_only_v6
|
||||
pub fn only_v6(&self) -> io::Result<bool> {
|
||||
self.io.get_ref().only_v6()
|
||||
}
|
||||
|
||||
/// Sets the linger duration of this socket by setting the SO_LINGER option
|
||||
pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
|
||||
self.io.get_ref().set_linger(dur)
|
||||
}
|
||||
|
||||
/// reads the linger duration for this socket by getting the SO_LINGER option
|
||||
/// [`set_linger`]: #tymethod.set_linger
|
||||
pub fn linger(&self) -> io::Result<Option<Duration>> {
|
||||
self.io.get_ref().linger()
|
||||
}
|
||||
|
||||
/// Sets the linger duration of this socket by setting the `SO_LINGER`
|
||||
/// option.
|
||||
///
|
||||
/// This option controls the action taken when a stream has unsent messages
|
||||
/// and the stream is closed. If `SO_LINGER` is set, the system
|
||||
/// shall block the process until it can transmit the data or until the
|
||||
/// time expires.
|
||||
///
|
||||
/// If `SO_LINGER` is not specified, and the stream is closed, the system
|
||||
/// handles the call in a way that allows the process to continue as quickly
|
||||
/// as possible.
|
||||
pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
|
||||
self.io.get_ref().set_linger(dur)
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for TcpStream {
|
||||
|
||||
+12
-12
@@ -5,7 +5,7 @@ use futures::{Async, Poll, Stream, Sink, StartSend, AsyncSink};
|
||||
|
||||
use net::UdpSocket;
|
||||
|
||||
/// Encoding of frames via buffers.
|
||||
/// Encoding of datagrams into frames via buffers.
|
||||
///
|
||||
/// This trait is used when constructing an instance of `UdpFramed` and provides
|
||||
/// the `In` and `Out` types which are decoded and encoded from the socket,
|
||||
@@ -18,7 +18,7 @@ use net::UdpSocket;
|
||||
/// The trait itself is implemented on a type that can track state for decoding
|
||||
/// or encoding, which is particularly useful for streaming parsers. In many
|
||||
/// cases, though, this type will simply be a unit struct (e.g. `struct
|
||||
/// HttpCodec`).
|
||||
/// MyCodec`).
|
||||
pub trait UdpCodec {
|
||||
/// The type of decoded frames.
|
||||
type In;
|
||||
@@ -140,9 +140,11 @@ pub fn new<C: UdpCodec>(socket: UdpSocket, codec: C) -> UdpFramed<C> {
|
||||
impl<C> UdpFramed<C> {
|
||||
/// Returns a reference to the underlying I/O stream wrapped by `Framed`.
|
||||
///
|
||||
/// Note that care should be taken to not tamper with the underlying stream
|
||||
/// of data coming in as it may corrupt the stream of frames otherwise being
|
||||
/// worked with.
|
||||
/// # Note
|
||||
///
|
||||
/// Care should be taken to not tamper with the underlying stream of data
|
||||
/// coming in as it may corrupt the stream of frames otherwise being worked
|
||||
/// with.
|
||||
pub fn get_ref(&self) -> &UdpSocket {
|
||||
&self.socket
|
||||
}
|
||||
@@ -150,18 +152,16 @@ impl<C> UdpFramed<C> {
|
||||
/// Returns a mutable reference to the underlying I/O stream wrapped by
|
||||
/// `Framed`.
|
||||
///
|
||||
/// Note that care should be taken to not tamper with the underlying stream
|
||||
/// of data coming in as it may corrupt the stream of frames otherwise being
|
||||
/// worked with.
|
||||
/// # Note
|
||||
///
|
||||
/// Care should be taken to not tamper with the underlying stream of data
|
||||
/// coming in as it may corrupt the stream of frames otherwise being worked
|
||||
/// with.
|
||||
pub fn get_mut(&mut self) -> &mut UdpSocket {
|
||||
&mut self.socket
|
||||
}
|
||||
|
||||
/// Consumes the `Framed`, returning its underlying I/O stream.
|
||||
///
|
||||
/// Note that care should be taken to not tamper with the underlying stream
|
||||
/// of data coming in as it may corrupt the stream of frames otherwise being
|
||||
/// worked with.
|
||||
pub fn into_inner(self) -> UdpSocket {
|
||||
self.socket
|
||||
}
|
||||
|
||||
+69
-47
@@ -16,10 +16,8 @@ mod frame;
|
||||
pub use self::frame::{UdpFramed, UdpCodec};
|
||||
|
||||
impl UdpSocket {
|
||||
/// Create a new UDP socket bound to the specified address.
|
||||
///
|
||||
/// This function will create a new UDP socket and attempt to bind it to the
|
||||
/// `addr` provided. If the result is `Ok`, the socket has successfully bound.
|
||||
/// This function will create a new UDP socket and attempt to bind it to
|
||||
/// the `addr` provided.
|
||||
pub fn bind(addr: &SocketAddr, handle: &Handle) -> io::Result<UdpSocket> {
|
||||
let udp = try!(mio::net::UdpSocket::bind(addr));
|
||||
UdpSocket::new(udp, handle)
|
||||
@@ -32,8 +30,8 @@ impl UdpSocket {
|
||||
|
||||
/// Creates a new `UdpSocket` from the previously bound socket provided.
|
||||
///
|
||||
/// The socket given will be registered with the event loop that `handle` is
|
||||
/// associated with. This function requires that `socket` has previously
|
||||
/// The socket given will be registered with the event loop that `handle`
|
||||
/// is associated with. This function requires that `socket` has previously
|
||||
/// been bound to an address to work correctly.
|
||||
///
|
||||
/// This can be used in conjunction with net2's `UdpBuilder` interface to
|
||||
@@ -68,19 +66,25 @@ impl UdpSocket {
|
||||
frame::new(self, codec)
|
||||
}
|
||||
|
||||
/// Returns the local address that this stream is bound to.
|
||||
/// Returns the local address that this socket is bound to.
|
||||
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||
self.io.get_ref().local_addr()
|
||||
}
|
||||
|
||||
/// Connects the UDP socket setting the default destination for send() and
|
||||
/// limiting packets that are read via recv from the address specified in addr.
|
||||
/// limiting packets that are read via recv from the address specified in
|
||||
/// `addr`.
|
||||
pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
|
||||
self.io.get_ref().connect(*addr)
|
||||
}
|
||||
|
||||
/// Sends data on the socket to the address previously bound via connect().
|
||||
/// On success, returns the number of bytes written.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called outside the context of a future's
|
||||
/// task.
|
||||
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
|
||||
if let Async::NotReady = self.io.poll_write() {
|
||||
return Err(io::ErrorKind::WouldBlock.into())
|
||||
@@ -98,6 +102,11 @@ impl UdpSocket {
|
||||
|
||||
/// Receives data from the socket previously bound with connect().
|
||||
/// On success, returns the number of bytes read.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called outside the context of a future's
|
||||
/// task.
|
||||
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
if let Async::NotReady = self.io.poll_read() {
|
||||
return Err(io::ErrorKind::WouldBlock.into())
|
||||
@@ -116,9 +125,12 @@ impl UdpSocket {
|
||||
/// Test whether this socket is ready to be read or not.
|
||||
///
|
||||
/// If the socket is *not* readable then the current task is scheduled to
|
||||
/// get a notification when the socket does become readable. That is, this
|
||||
/// is only suitable for calling in a `Future::poll` method and will
|
||||
/// automatically handle ensuring a retry once the socket is readable again.
|
||||
/// get a notification when the socket does become readable.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called outside the context of a future's
|
||||
/// task.
|
||||
pub fn poll_read(&self) -> Async<()> {
|
||||
self.io.poll_read()
|
||||
}
|
||||
@@ -126,9 +138,12 @@ impl UdpSocket {
|
||||
/// Test whether this socket is ready to be written to or not.
|
||||
///
|
||||
/// If the socket is *not* writable then the current task is scheduled to
|
||||
/// get a notification when the socket does become writable. That is, this
|
||||
/// is only suitable for calling in a `Future::poll` method and will
|
||||
/// automatically handle ensuring a retry once the socket is writable again.
|
||||
/// get a notification when the socket does become writable.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called outside the context of a future's
|
||||
/// task.
|
||||
pub fn poll_write(&self) -> Async<()> {
|
||||
self.io.poll_write()
|
||||
}
|
||||
@@ -136,8 +151,10 @@ impl UdpSocket {
|
||||
/// Sends data on the socket to the given address. On success, returns the
|
||||
/// number of bytes written.
|
||||
///
|
||||
/// Address type can be any implementer of `ToSocketAddrs` trait. See its
|
||||
/// documentation for concrete examples.
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called outside the context of a future's
|
||||
/// task.
|
||||
pub fn send_to(&self, buf: &[u8], target: &SocketAddr) -> io::Result<usize> {
|
||||
if let Async::NotReady = self.io.poll_write() {
|
||||
return Err(io::ErrorKind::WouldBlock.into())
|
||||
@@ -157,7 +174,7 @@ impl UdpSocket {
|
||||
/// `buf` provided as a datagram to this socket.
|
||||
///
|
||||
/// The returned future will return after data has been written to the
|
||||
/// outbound socket. The future will resolve to the stream as well as the
|
||||
/// outbound socket. The future will resolve to the stream as well as the
|
||||
/// buffer (for reuse if needed).
|
||||
///
|
||||
/// Any error which happens during writing will cause both the stream and
|
||||
@@ -166,8 +183,7 @@ impl UdpSocket {
|
||||
///
|
||||
/// The `buf` parameter here only requires the `AsRef<[u8]>` trait, which
|
||||
/// should be broadly applicable to accepting data which can be converted
|
||||
/// to a slice. The `Window` struct is also available in this crate to
|
||||
/// provide a different window into a slice if necessary.
|
||||
/// to a slice.
|
||||
pub fn send_dgram<T>(self, buf: T, addr: SocketAddr) -> SendDgram<T>
|
||||
where T: AsRef<[u8]>,
|
||||
{
|
||||
@@ -176,6 +192,11 @@ impl UdpSocket {
|
||||
|
||||
/// Receives data from the socket. On success, returns the number of bytes
|
||||
/// read and the address from whence the data came.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if called outside the context of a future's
|
||||
/// task.
|
||||
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
|
||||
if let Async::NotReady = self.io.poll_read() {
|
||||
return Err(io::ErrorKind::WouldBlock.into())
|
||||
@@ -199,12 +220,11 @@ impl UdpSocket {
|
||||
/// amount of data read, and the address the data was received from.
|
||||
///
|
||||
/// An error during reading will cause the socket and buffer to get
|
||||
/// destroyed and the socket will be returned.
|
||||
/// destroyed.
|
||||
///
|
||||
/// The `buf` parameter here only requires the `AsMut<[u8]>` trait, which
|
||||
/// should be broadly applicable to accepting data which can be converted
|
||||
/// to a slice. The `Window` struct is also available in this crate to
|
||||
/// provide a different window into a slice if necessary.
|
||||
/// to a slice.
|
||||
pub fn recv_dgram<T>(self, buf: T) -> RecvDgram<T>
|
||||
where T: AsMut<[u8]>,
|
||||
{
|
||||
@@ -213,10 +233,9 @@ impl UdpSocket {
|
||||
|
||||
/// Gets the value of the `SO_BROADCAST` option for this socket.
|
||||
///
|
||||
/// For more information about this option, see
|
||||
/// [`set_broadcast`][link].
|
||||
/// For more information about this option, see [`set_broadcast`].
|
||||
///
|
||||
/// [link]: #method.set_broadcast
|
||||
/// [`set_broadcast`]: #method.set_broadcast
|
||||
pub fn broadcast(&self) -> io::Result<bool> {
|
||||
self.io.get_ref().broadcast()
|
||||
}
|
||||
@@ -231,10 +250,9 @@ impl UdpSocket {
|
||||
|
||||
/// Gets the value of the `IP_MULTICAST_LOOP` option for this socket.
|
||||
///
|
||||
/// For more information about this option, see
|
||||
/// [`set_multicast_loop_v4`][link].
|
||||
/// For more information about this option, see [`set_multicast_loop_v4`].
|
||||
///
|
||||
/// [link]: #method.set_multicast_loop_v4
|
||||
/// [`set_multicast_loop_v4`]: #method.set_multicast_loop_v4
|
||||
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
|
||||
self.io.get_ref().multicast_loop_v4()
|
||||
}
|
||||
@@ -242,17 +260,19 @@ impl UdpSocket {
|
||||
/// Sets the value of the `IP_MULTICAST_LOOP` option for this socket.
|
||||
///
|
||||
/// If enabled, multicast packets will be looped back to the local socket.
|
||||
/// Note that this may not have any affect on IPv6 sockets.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// This may not have any affect on IPv6 sockets.
|
||||
pub fn set_multicast_loop_v4(&self, on: bool) -> io::Result<()> {
|
||||
self.io.get_ref().set_multicast_loop_v4(on)
|
||||
}
|
||||
|
||||
/// Gets the value of the `IP_MULTICAST_TTL` option for this socket.
|
||||
///
|
||||
/// For more information about this option, see
|
||||
/// [`set_multicast_ttl_v4`][link].
|
||||
/// For more information about this option, see [`set_multicast_ttl_v4`].
|
||||
///
|
||||
/// [link]: #method.set_multicast_ttl_v4
|
||||
/// [`set_multicast_ttl_v4`]: #method.set_multicast_ttl_v4
|
||||
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
|
||||
self.io.get_ref().multicast_ttl_v4()
|
||||
}
|
||||
@@ -263,17 +283,18 @@ impl UdpSocket {
|
||||
/// this socket. The default value is 1 which means that multicast packets
|
||||
/// don't leave the local network unless explicitly requested.
|
||||
///
|
||||
/// Note that this may not have any affect on IPv6 sockets.
|
||||
/// # Note
|
||||
///
|
||||
/// This may not have any affect on IPv6 sockets.
|
||||
pub fn set_multicast_ttl_v4(&self, ttl: u32) -> io::Result<()> {
|
||||
self.io.get_ref().set_multicast_ttl_v4(ttl)
|
||||
}
|
||||
|
||||
/// Gets the value of the `IPV6_MULTICAST_LOOP` option for this socket.
|
||||
///
|
||||
/// For more information about this option, see
|
||||
/// [`set_multicast_loop_v6`][link].
|
||||
/// For more information about this option, see [`set_multicast_loop_v6`].
|
||||
///
|
||||
/// [link]: #method.set_multicast_loop_v6
|
||||
/// [`set_multicast_loop_v6`]: #method.set_multicast_loop_v6
|
||||
pub fn multicast_loop_v6(&self) -> io::Result<bool> {
|
||||
self.io.get_ref().multicast_loop_v6()
|
||||
}
|
||||
@@ -281,16 +302,19 @@ impl UdpSocket {
|
||||
/// Sets the value of the `IPV6_MULTICAST_LOOP` option for this socket.
|
||||
///
|
||||
/// Controls whether this socket sees the multicast packets it sends itself.
|
||||
/// Note that this may not have any affect on IPv4 sockets.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// This may not have any affect on IPv4 sockets.
|
||||
pub fn set_multicast_loop_v6(&self, on: bool) -> io::Result<()> {
|
||||
self.io.get_ref().set_multicast_loop_v6(on)
|
||||
}
|
||||
|
||||
/// Gets the value of the `IP_TTL` option for this socket.
|
||||
///
|
||||
/// For more information about this option, see [`set_ttl`][link].
|
||||
/// For more information about this option, see [`set_ttl`].
|
||||
///
|
||||
/// [link]: #method.set_ttl
|
||||
/// [`set_ttl`]: #method.set_ttl
|
||||
pub fn ttl(&self) -> io::Result<u32> {
|
||||
self.io.get_ref().ttl()
|
||||
}
|
||||
@@ -329,10 +353,9 @@ impl UdpSocket {
|
||||
|
||||
/// Executes an operation of the `IP_DROP_MEMBERSHIP` type.
|
||||
///
|
||||
/// For more information about this option, see
|
||||
/// [`join_multicast_v4`][link].
|
||||
/// For more information about this option, see [`join_multicast_v4`].
|
||||
///
|
||||
/// [link]: #method.join_multicast_v4
|
||||
/// [`join_multicast_v4`]: #method.join_multicast_v4
|
||||
pub fn leave_multicast_v4(&self,
|
||||
multiaddr: &Ipv4Addr,
|
||||
interface: &Ipv4Addr) -> io::Result<()> {
|
||||
@@ -341,10 +364,9 @@ impl UdpSocket {
|
||||
|
||||
/// Executes an operation of the `IPV6_DROP_MEMBERSHIP` type.
|
||||
///
|
||||
/// For more information about this option, see
|
||||
/// [`join_multicast_v6`][link].
|
||||
/// For more information about this option, see [`join_multicast_v6`].
|
||||
///
|
||||
/// [link]: #method.join_multicast_v6
|
||||
/// [`join_multicast_v6`]: #method.join_multicast_v6
|
||||
pub fn leave_multicast_v6(&self,
|
||||
multiaddr: &Ipv6Addr,
|
||||
interface: u32) -> io::Result<()> {
|
||||
@@ -365,9 +387,9 @@ impl UdpSocket {
|
||||
|
||||
/// Gets the value of the `IPV6_V6ONLY` option for this socket.
|
||||
///
|
||||
/// For more information about this option, see [`set_only_v6`][link].
|
||||
/// For more information about this option, see [`set_only_v6`].
|
||||
///
|
||||
/// [link]: #method.set_only_v6
|
||||
/// [`set_only_v6`]: #method.set_only_v6
|
||||
pub fn only_v6(&self) -> io::Result<bool> {
|
||||
self.io.get_ref().only_v6()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user