auto merge of #13919 : thomaslee/rust/thomaslee_proposed_tcpstream_open, r=alexcrichton
Been meaning to try my hand at something like this for a while, and noticed something similar mentioned as part of #13537. The suggestion on the original ticket is to use `TcpStream::open(&str)` to pass in a host + port string, but seems a little cleaner to pass in host and port separately -- so a signature like `TcpStream::open(&str, u16)`. Also means we can use std::io::net::addrinfo directly instead of using e.g. liburl to parse the host+port pair from a string. One outstanding issue in this PR that I'm not entirely sure how to address: in open_timeout, the timeout_ms will apply for every A record we find associated with a hostname -- probably not the intended behavior, but I didn't want to waste my time on elaborate alternatives until the general idea was a-OKed. :) Anyway, perhaps there are other reasons for us to prefer the original proposed syntax, but thought I'd get some thoughts on this. Maybe there are some solid reasons to prefer using liburl to do this stuff.
This commit is contained in:
commit
e162438162
6 changed files with 288 additions and 109 deletions
|
|
@ -54,6 +54,8 @@ macro_rules! iotest (
|
|||
iotest!(fn eventual_timeout() {
|
||||
use native;
|
||||
let addr = next_test_ip4();
|
||||
let host = addr.ip.to_str();
|
||||
let port = addr.port;
|
||||
|
||||
// Use a native task to receive connections because it turns out libuv is
|
||||
// really good at accepting connections and will likely run out of file
|
||||
|
|
@ -61,7 +63,7 @@ iotest!(fn eventual_timeout() {
|
|||
let (tx1, rx1) = channel();
|
||||
let (_tx2, rx2) = channel::<()>();
|
||||
native::task::spawn(proc() {
|
||||
let _l = TcpListener::bind(addr).unwrap().listen();
|
||||
let _l = TcpListener::bind(host, port).unwrap().listen();
|
||||
tx1.send(());
|
||||
let _ = rx2.recv_opt();
|
||||
});
|
||||
|
|
@ -80,7 +82,9 @@ iotest!(fn eventual_timeout() {
|
|||
|
||||
iotest!(fn timeout_success() {
|
||||
let addr = next_test_ip4();
|
||||
let _l = TcpListener::bind(addr).unwrap().listen();
|
||||
let host = addr.ip.to_str();
|
||||
let port = addr.port;
|
||||
let _l = TcpListener::bind(host, port).unwrap().listen();
|
||||
|
||||
assert!(TcpStream::connect_timeout(addr, 1000).is_ok());
|
||||
})
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ extern crate libc;
|
|||
extern crate green;
|
||||
extern crate rustuv;
|
||||
|
||||
use std::io::net::ip::{Ipv4Addr, SocketAddr};
|
||||
use std::io::net::tcp::{TcpListener, TcpStream};
|
||||
use std::io::{Acceptor, Listener};
|
||||
use std::task::TaskBuilder;
|
||||
|
|
@ -38,10 +37,9 @@ fn main() {
|
|||
unsafe { libc::exit(1) }
|
||||
});
|
||||
|
||||
let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 0 };
|
||||
let (tx, rx) = channel();
|
||||
spawn(proc() {
|
||||
let mut listener = TcpListener::bind(addr).unwrap();
|
||||
let mut listener = TcpListener::bind("127.0.0.1", 0).unwrap();
|
||||
tx.send(listener.socket_name().unwrap());
|
||||
let mut acceptor = listener.listen();
|
||||
loop {
|
||||
|
|
@ -64,7 +62,9 @@ fn main() {
|
|||
let mut builder = TaskBuilder::new();
|
||||
builder.opts.stack_size = Some(32 * 1024);
|
||||
builder.spawn(proc() {
|
||||
match TcpStream::connect(addr) {
|
||||
let host = addr.ip.to_str();
|
||||
let port = addr.port;
|
||||
match TcpStream::connect(host, port) {
|
||||
Ok(stream) => {
|
||||
let mut stream = stream;
|
||||
stream.write([1]);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue