rust - Detecting client hangup in MIO -


when using mio (0.3.5) how detect termination of connection?

i tried following:

extern crate mio; use mio::{eventloop,token,readhint}; use std::io::read;  fn main(){   let listener = mio::tcp::tcplistener::bind("localhost:1234").unwrap();   let (stream,_) : (mio::tcp::tcpstream, _)  =  listener.accept().unwrap();    let mut event_loop = eventloop::new().unwrap();   event_loop.register(&stream,token(0)).unwrap();   println!("run...");   event_loop.run(&mut h{stream:stream}).unwrap(); }  struct h{stream : mio::tcp::tcpstream}  impl mio::handler h{   type timeout = ();   type message = ();    fn readable(&mut self, _ : &mut eventloop<self>, _ : token, hint: readhint){     let mut buf: [u8; 500] = [0; 500];     println!("{} {}",(hint==readhint::data()),self.stream.read(&mut buf).unwrap());     std::thread::sleep_ms(1000);   } } 

run this. connect using nc localhost 1234. terminate connection using ctrl-c. code think there new data available (hint==readhint::data()). try read result in 0 bytes available.

shouldn't hint readhint::hup()?

the default when calling register on mio v0.3.5 register readable interest, hint get.

if want warned hup well, need use function register_opt , give interest , pollopt parameters, code becomes:

extern crate mio; use mio::{eventloop,token,readhint,pollopt,interest}; use std::io::read;  fn main() {     let listener = mio::tcp::tcplistener::bind("localhost:1234").unwrap();     let (stream,_) : (mio::tcp::tcpstream, _)  =  listener.accept().unwrap();      let mut event_loop = eventloop::new().unwrap();     let interest = interest::readable() | interest::hup();     event_loop.register_opt(&stream,token(0), interest,pollopt::level()).unwrap();     println!("run...");     event_loop.run(&mut h{stream:stream}).unwrap(); }  struct h{stream : mio::tcp::tcpstream}  impl mio::handler h {     type timeout = ();     type message = ();      fn readable(&mut self, event_loop : &mut eventloop<self>, _ : token, hint: readhint){         let mut buf: [u8; 500] = [0; 500];         if hint.is_hup() {             println!("recieved hup, exiting");             event_loop.shutdown();             return;         }         println!("{} {}",hint.is_data(),self.stream.read(&mut buf).unwrap());         std::thread::sleep_ms(1000);     } } 

i think default convenience function register has changed in v0.4.0 interest::all() shouldn't problem in future.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -