1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use net2::unix::UnixTcpBuilderExt;
use net2::TcpBuilder;
use tokio::net::{TcpListener, TcpStream};
use tokio::prelude::*;
pub mod reader;
pub mod request;
pub mod response;
pub mod status;
pub mod writer;
pub mod prelude {
pub use tokio::prelude::*;
}
pub mod server {
pub use crate::reader::Http;
pub use crate::request::Request;
pub use crate::response::Response;
pub use crate::status::StatusMessage;
pub use crate::Server;
}
pub mod runtime {
pub fn spawn<F>(future: F) -> Result<(), ()>
where
F: futures::Future<Item = (), Error = ()> + 'static,
{
tokio::runtime::current_thread::spawn(future);
Ok(())
}
pub fn run<F>(future: F)
where
F: futures::Future<Item = (), Error = ()> + 'static,
{
let mut runtime = tokio::runtime::current_thread::Runtime::new().unwrap();
runtime.spawn(future);
runtime.run().expect("Could not run runtime");
}
}
pub struct Server {
listener: TcpListener,
}
impl Server {
pub fn new(addr: &'static str) -> Server {
let listener = {
let builder = TcpBuilder::new_v4().unwrap();
builder.reuse_port(true).unwrap();
builder.bind(addr).unwrap();
builder.listen(512).unwrap() };
let listener = TcpListener::from_std(listener, &tokio::reactor::Handle::current()).unwrap();
Server { listener }
}
}
impl Stream for Server {
type Item = TcpStream;
type Error = ();
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
loop {
match self.listener.poll_accept() {
Ok(Async::Ready((socket, _))) => {
return Ok(Async::Ready(Some(socket)));
}
Ok(Async::NotReady) => {
return Ok(Async::NotReady);
}
Err(_) => {}
}
}
}
}