Detecting client hangup in MIO

2019-06-25 12:47发布

问题:

When using MIO (0.3.5) how do I detect the termination of a connection?

I tried the 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 for 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 to it using something like nc localhost 1234. Terminate connection by using Ctrl-C. My code will think there is new data available (hint==ReadHint::data()). A try to read it will result in zero bytes available.

Shouldn't the hint be something like ReadHint::hup()?

回答1:

The default when calling register on mio v0.3.5 is to register only for the readable Interest, so that is the only hint you will get.

If you want to be warned for hup as well, you need to use the function register_opt and give your Interest and PollOpt as parameters, so your 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 for 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 the default for the convenience function register has changed in v0.4.0 to be Interest::all() so this shouldn't be a problem in the future.



标签: rust mio