From 84cccf3fb45c2bb97201b550f09c2aaa103600b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 4 Sep 2016 18:17:31 +0300 Subject: Update to mio 0.6 Next destination, tokio(-core)? --- Cargo.toml | 5 +---- src/main.rs | 55 +++++++++++++++++++++++++++++-------------------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ec2b2dc..001388b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,5 @@ version = "0.1.0" authors = ["Sebastian Dröge "] [dependencies] -mio = "0.5" - -#[dependencies.mio] -#git = "https://github.com/carllerche/mio" +mio = "0.6" diff --git a/src/main.rs b/src/main.rs index 5e3ef10..2a73de4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,15 +48,17 @@ fn slice_map_range_in_place(s: &mut [T], range: Range, mut f: F) } impl PtpReflectorHandler { - fn new(event_loop: &mut EventLoop) -> Result { + fn new(poll: &mut Poll) -> Result { let multicast_group = "224.0.1.129".parse().unwrap(); + let any_addr = "0.0.0.0".parse().unwrap(); let event_bind_addr = "0.0.0.0:319".parse().unwrap(); - let event_socket = try!(UdpSocket::bound(&event_bind_addr).map_err(|e| e.to_string())); - try!(event_socket.join_multicast(&multicast_group).map_err(|e| e.to_string())); - try!(event_loop.register(&event_socket, + let event_socket = try!(UdpSocket::bind(&event_bind_addr).map_err(|e| e.to_string())); + try!(event_socket.join_multicast_v4(&multicast_group, &any_addr) + .map_err(|e| e.to_string())); + try!(poll.register(&event_socket, SOCKET_EVENT, - EventSet::readable(), + Ready::readable(), PollOpt::level()) .map_err(|e| e.to_string())); @@ -65,11 +67,12 @@ impl PtpReflectorHandler { let event_addr = "224.0.1.129:319".parse().unwrap(); let general_bind_addr = "0.0.0.0:320".parse().unwrap(); - let general_socket = try!(UdpSocket::bound(&general_bind_addr).map_err(|e| e.to_string())); - try!(general_socket.join_multicast(&multicast_group).map_err(|e| e.to_string())); - try!(event_loop.register(&general_socket, + let general_socket = try!(UdpSocket::bind(&general_bind_addr).map_err(|e| e.to_string())); + try!(general_socket.join_multicast_v4(&multicast_group, &any_addr) + .map_err(|e| e.to_string())); + try!(poll.register(&general_socket, SOCKET_GENERAL, - EventSet::readable(), + Ready::readable(), PollOpt::level()) .map_err(|e| e.to_string())); @@ -84,21 +87,16 @@ impl PtpReflectorHandler { general_addr: general_addr, }) } -} - -impl Handler for PtpReflectorHandler { - type Timeout = usize; - type Message = (); - fn ready(&mut self, _: &mut EventLoop, token: Token, events: EventSet) { - if !events.is_readable() { + fn ready(&mut self, event: &Event) { + if !event.kind().is_readable() { return; } - let (socket, addr) = match token { + let (socket, addr) = match event.token() { SOCKET_EVENT => (&self.event_socket, &self.event_addr), SOCKET_GENERAL => (&self.general_socket, &self.general_addr), - _ => panic!("unexpected token"), + _ => unreachable!(), }; let msg = &mut [0; 1024]; @@ -163,16 +161,21 @@ impl Handler for PtpReflectorHandler { } fn main() { - let mut event_loop = EventLoop::new().unwrap(); - let res = PtpReflectorHandler::new(&mut event_loop) - .map_err(|e| e.to_string()) - .and_then(|mut handler| { - event_loop.run(&mut handler) - .map_err(|e| e.to_string()) - }); + let mut poll = Poll::new().unwrap(); + let res = PtpReflectorHandler::new(&mut poll).and_then(|mut handler| { + let mut events = Events::with_capacity(1024); + + loop { + try!(poll.poll(&mut events, None).or_else(|err| Err(err.to_string()))); + + for event in events.iter() { + handler.ready(&event); + } + } + }); match res { - Ok(_) => (), + Ok(()) => (), Err(s) => panic!(s), }; } -- cgit v0.11.2-2-gd1dd