在MIO中检测客户端挂起

时间:2015-06-10 14:26:00

标签: rust mio

使用MIO(0.3.5)时,如何检测连接终止?

我尝试了以下内容:

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);
  }
}

运行它。使用nc localhost 1234之类的东西连接到它。使用Ctrl-C终止连接。我的代码会认为有新数据可用(hint==ReadHint::data())。尝试读取它将导致零字节可用。

提示不应该是ReadHint::hup()吗?

1 个答案:

答案 0 :(得分:5)

在mio v0.3.5上调用register时的默认设置是仅注册readable Interest,这是您将获得的唯一提示。

如果您也想要警告hup,您需要使用函数register_opt并将InterestPollOpt作为参数,因此您的代码变为:

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);
    }
}

我认为方便函数register的默认值已在v0.4.0中更改为Interest::all(),因此将来这不应该是一个问题。