2014-03-10 23:27:34 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// ignore-linux see joyent/libuv#1189
|
|
|
|
// ignore-android needs extra network permissions
|
|
|
|
// exec-env:RUST_LOG=debug
|
|
|
|
|
2014-04-03 19:55:06 -05:00
|
|
|
#![feature(phase)]
|
2014-05-24 23:24:35 -05:00
|
|
|
#[phase(plugin, link)]
|
2014-03-16 08:18:17 -05:00
|
|
|
extern crate log;
|
2014-02-26 11:58:41 -06:00
|
|
|
extern crate libc;
|
2014-04-06 00:18:52 -05:00
|
|
|
extern crate green;
|
|
|
|
extern crate rustuv;
|
2014-05-22 13:28:01 -05:00
|
|
|
extern crate debug;
|
2014-03-16 08:18:17 -05:00
|
|
|
|
2014-03-10 23:27:34 -05:00
|
|
|
use std::io::net::tcp::{TcpListener, TcpStream};
|
|
|
|
use std::io::{Acceptor, Listener};
|
2014-04-21 23:19:59 -05:00
|
|
|
use std::task::TaskBuilder;
|
2014-04-06 00:18:52 -05:00
|
|
|
|
|
|
|
#[start]
|
2014-06-25 14:47:34 -05:00
|
|
|
fn start(argc: int, argv: *const *const u8) -> int {
|
2014-04-06 00:18:52 -05:00
|
|
|
green::start(argc, argv, rustuv::event_loop, main)
|
|
|
|
}
|
2014-03-10 23:27:34 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// This test has a chance to time out, try to not let it time out
|
|
|
|
spawn(proc() {
|
|
|
|
use std::io::timer;
|
|
|
|
timer::sleep(30 * 1000);
|
|
|
|
println!("timed out!");
|
|
|
|
unsafe { libc::exit(1) }
|
|
|
|
});
|
|
|
|
|
2014-03-13 23:26:14 -05:00
|
|
|
let (tx, rx) = channel();
|
2014-03-10 23:27:34 -05:00
|
|
|
spawn(proc() {
|
2014-05-10 23:30:36 -05:00
|
|
|
let mut listener = TcpListener::bind("127.0.0.1", 0).unwrap();
|
2014-03-13 23:26:14 -05:00
|
|
|
tx.send(listener.socket_name().unwrap());
|
2014-03-10 23:27:34 -05:00
|
|
|
let mut acceptor = listener.listen();
|
|
|
|
loop {
|
|
|
|
let mut stream = match acceptor.accept() {
|
|
|
|
Ok(stream) => stream,
|
|
|
|
Err(error) => {
|
|
|
|
debug!("accept failed: {:?}", error);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
stream.read_byte();
|
|
|
|
stream.write([2]);
|
|
|
|
}
|
|
|
|
});
|
2014-03-13 23:26:14 -05:00
|
|
|
let addr = rx.recv();
|
2014-03-10 23:27:34 -05:00
|
|
|
|
2014-03-13 23:26:14 -05:00
|
|
|
let (tx, rx) = channel();
|
2014-04-21 16:58:52 -05:00
|
|
|
for _ in range(0u, 1000) {
|
2014-03-13 23:26:14 -05:00
|
|
|
let tx = tx.clone();
|
2014-06-16 18:17:59 -05:00
|
|
|
TaskBuilder::new().stack_size(64 * 1024).spawn(proc() {
|
2014-06-21 05:39:03 -05:00
|
|
|
let host = addr.ip.to_string();
|
2014-05-10 23:30:36 -05:00
|
|
|
let port = addr.port;
|
2014-05-16 12:45:16 -05:00
|
|
|
match TcpStream::connect(host.as_slice(), port) {
|
2014-03-10 23:27:34 -05:00
|
|
|
Ok(stream) => {
|
|
|
|
let mut stream = stream;
|
|
|
|
stream.write([1]);
|
|
|
|
let mut buf = [0];
|
|
|
|
stream.read(buf);
|
|
|
|
},
|
|
|
|
Err(e) => debug!("{:?}", e)
|
|
|
|
}
|
2014-03-13 23:26:14 -05:00
|
|
|
tx.send(());
|
2014-03-10 23:27:34 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all clients to exit, but don't wait for the server to exit. The
|
|
|
|
// server just runs infinitely.
|
2014-03-13 23:26:14 -05:00
|
|
|
drop(tx);
|
2014-04-21 16:58:52 -05:00
|
|
|
for _ in range(0u, 1000) {
|
2014-03-13 23:26:14 -05:00
|
|
|
rx.recv();
|
2014-03-10 23:27:34 -05:00
|
|
|
}
|
|
|
|
unsafe { libc::exit(0) }
|
|
|
|
}
|