summaryrefslogtreecommitdiff
path: root/src/listener.rs
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2016-09-05 09:49:24 +0200
committerYorhel <git@yorhel.nl>2016-09-05 09:49:24 +0200
commit39515558ad68edffc6fe52936714fcefbfc58949 (patch)
treecb6891169c101638ff798b268b0aa66328845e3a /src/listener.rs
parent72813e1af8aaeef36a22592b83204dcbb48afdd9 (diff)
Add some buffer handling + improve error handling a bit
Diffstat (limited to 'src/listener.rs')
-rw-r--r--src/listener.rs31
1 files changed, 26 insertions, 5 deletions
diff --git a/src/listener.rs b/src/listener.rs
index e6683f3..293099e 100644
--- a/src/listener.rs
+++ b/src/listener.rs
@@ -1,13 +1,16 @@
use mio::{Ready,Token,Event};
use mio::tcp::TcpListener;
use std::net::SocketAddr;
-use std::io::Result;
+use std::io::{Result,ErrorKind};
use config::Config;
use eventloop::{Machine,Context,EventLoop};
+use itf_http;
+
struct Listener {
sock: TcpListener,
+ addr: SocketAddr,
io: Token
}
@@ -18,6 +21,7 @@ impl Listener {
let sock = try!(TcpListener::bind(addr));
Ok(Listener {
sock: sock,
+ addr: *addr,
io: Token(0)
})
}
@@ -28,12 +32,29 @@ impl Machine for Listener {
fn init(&mut self, ctx: &mut Context) {
self.io = ctx.assign();
ctx.register(&self.sock, self.io, Ready::readable());
- info!("Listening on {}", self.sock.local_addr().unwrap());
+ info!("Listening on {}", self.addr);
}
- fn handle(&mut self, _: &mut Context, _: Event) {
- let(_, addr) = self.sock.accept().unwrap(); // TODO: ERROR HANDLING!
- debug!("New connection from {}", addr);
+ fn handle(&mut self, ctx: &mut Context, _: Event) {
+ // TODO: max_connections or something
+ match self.sock.accept() {
+ Err(err) => {
+ match err.kind() {
+ ErrorKind::WouldBlock |
+ ErrorKind::Interrupted |
+ ErrorKind::TimedOut => { },
+ _ => {
+ error!("Error accepting on {}: {}", self.addr, err);
+ // If the error is persistent, we may be getting into an infinite loop.
+ // TODO: Have a back-off timer here (especially for EMFILE).
+ }
+ }
+ },
+ Ok((sock, addr)) => {
+ debug!("{}: New connection", addr);
+ ctx.spawn(Box::new(itf_http::ItfHttp::new(sock, addr)));
+ }
+ }
}
}