Migrated io::net::udp over to ToSocketAddr
UdpSocket constructor methods now use ToSocketAddr trait instead of SocketAddr. [breaking-change]
This commit is contained in:
parent
ac846749f0
commit
7e3344b17f
@ -10,11 +10,11 @@
|
|||||||
|
|
||||||
//! Networking I/O
|
//! Networking I/O
|
||||||
|
|
||||||
use io::{IoError, InvalidInput};
|
use io::{IoError, IoResult, InvalidInput};
|
||||||
use option::None;
|
use option::None;
|
||||||
use result::{Result, Ok, Err};
|
use result::{Result, Ok, Err};
|
||||||
use rt::rtio;
|
use rt::rtio;
|
||||||
use self::ip::{Ipv4Addr, Ipv6Addr, IpAddr, ToSocketAddr};
|
use self::ip::{Ipv4Addr, Ipv6Addr, IpAddr, SocketAddr, ToSocketAddr};
|
||||||
|
|
||||||
pub use self::addrinfo::get_host_addresses;
|
pub use self::addrinfo::get_host_addresses;
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ fn from_rtio(ip: rtio::IpAddr) -> IpAddr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_addresses<A: ToSocketAddr, T>(
|
fn with_addresses_io<A: ToSocketAddr, T>(
|
||||||
addr: A,
|
addr: A,
|
||||||
action: |&mut rtio::IoFactory, rtio::SocketAddr| -> Result<T, rtio::IoError>
|
action: |&mut rtio::IoFactory, rtio::SocketAddr| -> Result<T, rtio::IoError>
|
||||||
) -> Result<T, IoError> {
|
) -> Result<T, IoError> {
|
||||||
@ -63,3 +63,22 @@ fn with_addresses<A: ToSocketAddr, T>(
|
|||||||
}
|
}
|
||||||
Err(err)
|
Err(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn with_addresses<A: ToSocketAddr, T>(addr: A, action: |SocketAddr| -> IoResult<T>)
|
||||||
|
-> IoResult<T> {
|
||||||
|
const DEFAULT_ERROR: IoError = IoError {
|
||||||
|
kind: InvalidInput,
|
||||||
|
desc: "no addresses found for hostname",
|
||||||
|
detail: None
|
||||||
|
};
|
||||||
|
|
||||||
|
let addresses = try!(addr.to_socket_addr_all());
|
||||||
|
let mut err = DEFAULT_ERROR;
|
||||||
|
for addr in addresses.into_iter() {
|
||||||
|
match action(addr) {
|
||||||
|
Ok(r) => return Ok(r),
|
||||||
|
Err(e) => err = e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
@ -67,7 +67,7 @@ impl TcpStream {
|
|||||||
/// trait can be supplied for the address; see this trait documentation for
|
/// trait can be supplied for the address; see this trait documentation for
|
||||||
/// concrete examples.
|
/// concrete examples.
|
||||||
pub fn connect<A: ToSocketAddr>(addr: A) -> IoResult<TcpStream> {
|
pub fn connect<A: ToSocketAddr>(addr: A) -> IoResult<TcpStream> {
|
||||||
super::with_addresses(addr, |io, addr| io.tcp_connect(addr, None).map(TcpStream::new))
|
super::with_addresses_io(addr, |io, addr| io.tcp_connect(addr, None).map(TcpStream::new))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a TCP connection to a remote socket address, timing out after
|
/// Creates a TCP connection to a remote socket address, timing out after
|
||||||
@ -89,7 +89,7 @@ impl TcpStream {
|
|||||||
return Err(standard_error(TimedOut));
|
return Err(standard_error(TimedOut));
|
||||||
}
|
}
|
||||||
|
|
||||||
super::with_addresses(addr, |io, addr|
|
super::with_addresses_io(addr, |io, addr|
|
||||||
io.tcp_connect(addr, Some(timeout.num_milliseconds() as u64)).map(TcpStream::new)
|
io.tcp_connect(addr, Some(timeout.num_milliseconds() as u64)).map(TcpStream::new)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -324,7 +324,7 @@ impl TcpListener {
|
|||||||
/// to this listener. The port allocated can be queried via the
|
/// to this listener. The port allocated can be queried via the
|
||||||
/// `socket_name` function.
|
/// `socket_name` function.
|
||||||
pub fn bind<A: ToSocketAddr>(addr: A) -> IoResult<TcpListener> {
|
pub fn bind<A: ToSocketAddr>(addr: A) -> IoResult<TcpListener> {
|
||||||
super::with_addresses(addr, |io, addr| io.tcp_bind(addr).map(|l| TcpListener { obj: l }))
|
super::with_addresses_io(addr, |io, addr| io.tcp_bind(addr).map(|l| TcpListener { obj: l }))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the local socket address of this listener.
|
/// Returns the local socket address of this listener.
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
//! datagram protocol.
|
//! datagram protocol.
|
||||||
|
|
||||||
use clone::Clone;
|
use clone::Clone;
|
||||||
use io::net::ip::{SocketAddr, IpAddr};
|
use io::net::ip::{SocketAddr, IpAddr, ToSocketAddr};
|
||||||
use io::{Reader, Writer, IoResult, IoError};
|
use io::{Reader, Writer, IoResult, IoError};
|
||||||
use kinds::Send;
|
use kinds::Send;
|
||||||
use boxed::Box;
|
use boxed::Box;
|
||||||
@ -65,18 +65,13 @@ pub struct UdpSocket {
|
|||||||
|
|
||||||
impl UdpSocket {
|
impl UdpSocket {
|
||||||
/// Creates a UDP socket from the given socket address.
|
/// Creates a UDP socket from the given socket address.
|
||||||
pub fn bind(addr: SocketAddr) -> IoResult<UdpSocket> {
|
pub fn bind<A: ToSocketAddr>(addr: A) -> IoResult<UdpSocket> {
|
||||||
let SocketAddr { ip, port } = addr;
|
super::with_addresses_io(addr, |io, addr| io.udp_bind(addr).map(|s| UdpSocket { obj: s }))
|
||||||
LocalIo::maybe_raise(|io| {
|
|
||||||
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
|
|
||||||
io.udp_bind(addr).map(|s| UdpSocket { obj: s })
|
|
||||||
}).map_err(IoError::from_rtio_error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Receives data from the socket. On success, returns the number of bytes
|
/// Receives data from the socket. On success, returns the number of bytes
|
||||||
/// read and the address from whence the data came.
|
/// read and the address from whence the data came.
|
||||||
pub fn recv_from(&mut self, buf: &mut [u8])
|
pub fn recv_from(&mut self, buf: &mut [u8]) -> IoResult<(uint, SocketAddr)> {
|
||||||
-> IoResult<(uint, SocketAddr)> {
|
|
||||||
match self.obj.recv_from(buf) {
|
match self.obj.recv_from(buf) {
|
||||||
Ok((amt, rtio::SocketAddr { ip, port })) => {
|
Ok((amt, rtio::SocketAddr { ip, port })) => {
|
||||||
Ok((amt, SocketAddr { ip: super::from_rtio(ip), port: port }))
|
Ok((amt, SocketAddr { ip: super::from_rtio(ip), port: port }))
|
||||||
@ -87,11 +82,11 @@ impl UdpSocket {
|
|||||||
|
|
||||||
/// Sends data on the socket to the given address. Returns nothing on
|
/// Sends data on the socket to the given address. Returns nothing on
|
||||||
/// success.
|
/// success.
|
||||||
pub fn send_to(&mut self, buf: &[u8], dst: SocketAddr) -> IoResult<()> {
|
pub fn send_to<A: ToSocketAddr>(&mut self, buf: &[u8], addr: A) -> IoResult<()> {
|
||||||
self.obj.send_to(buf, rtio::SocketAddr {
|
super::with_addresses(addr, |addr| self.obj.send_to(buf, rtio::SocketAddr {
|
||||||
ip: super::to_rtio(dst.ip),
|
ip: super::to_rtio(addr.ip),
|
||||||
port: dst.port,
|
port: addr.port,
|
||||||
}).map_err(IoError::from_rtio_error)
|
}).map_err(IoError::from_rtio_error))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a `UdpStream`, which allows use of the `Reader` and `Writer`
|
/// Creates a `UdpStream`, which allows use of the `Reader` and `Writer`
|
||||||
|
Loading…
Reference in New Issue
Block a user