diff options
author | Yorhel <git@yorhel.nl> | 2016-09-07 16:51:37 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2016-09-07 16:51:37 +0200 |
commit | 863080971958332975098b918dd74ec9c2c44036 (patch) | |
tree | bb784b4468339aa2e5266449e0da334be0dd23bc /src/itf_http.rs | |
parent | ccd38998fd8ff2bbf2f916e6e0d04087eaaf6293 (diff) |
eventloop: Make spawn() accept a closure that creates a Machine
This provides two advantages: Machines don't have to be Send/Sync
anymore (but can still be spawned on different threads), and the init()
and new() methods are now merged, allowing the Machine to access its
context on initialization.
I guess Rotor had a point here after all. :)
Diffstat (limited to 'src/itf_http.rs')
-rw-r--r-- | src/itf_http.rs | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/src/itf_http.rs b/src/itf_http.rs index a89d9e0..cda923f 100644 --- a/src/itf_http.rs +++ b/src/itf_http.rs @@ -22,15 +22,16 @@ pub struct ItfHttp { impl ItfHttp { - pub fn new(itf: Arc<Interface>, sock: TcpStream, addr: SocketAddr) -> ItfHttp { + pub fn new(ctx: &mut Context, itf: Arc<Interface>, sock: TcpStream, addr: SocketAddr) -> ItfHttp { + let io = ctx.register(&sock, Ready::readable()); ItfHttp { sock: sock, addr: addr, - io: Token(0), + io: io, ioreg: Ready::readable(), rbuf: Buf::new(), wbuf: Buf::new(), - iotimeout: TToken(0), + iotimeout: ctx.set_timeout(itf.cfg.io_timeout), itf: itf, } } @@ -47,7 +48,7 @@ impl ItfHttp { } return; }, - Ok(0) => { // This behaviour isn't documented, unsure if it's intended. + Ok(0) => { debug!("{}: Connection closed", self.addr); self.remove(ctx); return; @@ -106,11 +107,6 @@ impl ItfHttp { impl Machine for ItfHttp { - fn init(&mut self, ctx: &mut Context) { - self.io = ctx.register(&self.sock, self.ioreg); - self.iotimeout = ctx.set_timeout(self.itf.cfg.io_timeout); - } - fn handle(&mut self, ctx: &mut Context, ev: Event) { if ev.kind().is_readable() { self.handle_read(ctx); |