diff options
-rw-r--r-- | src/main.rs | 121 |
1 files changed, 64 insertions, 57 deletions
diff --git a/src/main.rs b/src/main.rs index 6894e50..22a49bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,41 +1,41 @@ -/* - * Copyright (C) 2015 Sebastian Dröge <sebastian@centricular.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ +// Copyright (C) 2015 Sebastian Dröge <sebastian@centricular.com> +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Library General Public +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this library; if not, write to the +// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, +// Boston, MA 02110-1301, USA. +// extern crate mio; use mio::*; use mio::udp::*; -use std::net::{SocketAddr}; -use std::ops::{Range}; +use std::net::SocketAddr; +use std::ops::Range; -const SOCKET_EVENT : Token = Token(0); +const SOCKET_EVENT: Token = Token(0); const SOCKET_GENERAL: Token = Token(1); struct PtpReflectorHandler { - event_socket : UdpSocket, - event_addr : SocketAddr, - general_socket : UdpSocket, - general_addr : SocketAddr, + event_socket: UdpSocket, + event_addr: SocketAddr, + general_socket: UdpSocket, + general_addr: SocketAddr, } -fn slice_map_range_in_place<T,F>(s: &mut [T], range: Range<usize>, mut f: F) - where F: FnMut(&T) -> T { +fn slice_map_range_in_place<T, F>(s: &mut [T], range: Range<usize>, mut f: F) + where F: FnMut(&T) -> T +{ let n = s.len(); if range.end > n { @@ -53,23 +53,31 @@ impl PtpReflectorHandler { 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, SOCKET_EVENT, EventSet::readable(), PollOpt::level()).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, + SOCKET_EVENT, + EventSet::readable(), + PollOpt::level()) + .map_err(|e| e.to_string())); // FIXME: How can we create this from the multicast group? - //let event_addr = SocketAddr::new(multicast_group, 319); + // let event_addr = SocketAddr::new(multicast_group, 319); 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, SOCKET_GENERAL, EventSet::readable(), PollOpt::level()).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, + SOCKET_GENERAL, + EventSet::readable(), + PollOpt::level()) + .map_err(|e| e.to_string())); // FIXME: How can we create this from the multicast group? - //let general_addr = SocketAddr::new(multicast_group, 320); + // let general_addr = SocketAddr::new(multicast_group, 320); let general_addr = "224.0.1.129:320".parse().unwrap(); - Ok (PtpReflectorHandler { + Ok(PtpReflectorHandler { event_socket: event_socket, event_addr: event_addr, general_socket: general_socket, @@ -88,20 +96,20 @@ impl Handler for PtpReflectorHandler { } let (socket, addr) = match token { - SOCKET_EVENT => (&self.event_socket, &self.event_addr), + SOCKET_EVENT => (&self.event_socket, &self.event_addr), SOCKET_GENERAL => (&self.general_socket, &self.general_addr), - _ => panic!("unexpected token") + _ => panic!("unexpected token"), }; let msg = &mut [0; 1024]; let length = match socket.recv_from(msg) { - Err(e) => { + Err(e) => { print!("Error while receiving message: {}\n", e.to_string()); return; - }, - Ok(Some((l,_))) => l, - Ok(None) => { + } + Ok(Some((l, _))) => l, + Ok(None) => { print!("Error while receiving message\n"); return; } @@ -122,25 +130,25 @@ impl Handler for PtpReflectorHandler { slice_map_range_in_place(msg, 20..28, |&x| x ^ 0xff); match msg_type { - 0x0 | 0x08 | 0xb => true, + 0x0 | 0x08 | 0xb => true, 0x9 if msg.len() >= 54 => { slice_map_range_in_place(msg, 44..52, |&x| x ^ 0xff); true - }, - _ => false + } + _ => false, } - }, + } 1 => { msg[4] = 0; slice_map_range_in_place(msg, 20..28, |&x| x ^ 0xff); match msg_type { 0x1 => true, - _ => false + _ => false, } - }, - _ => false + } + _ => false, }; if forward { @@ -148,8 +156,8 @@ impl Handler for PtpReflectorHandler { Err(e) => { print!("Error while sending message: {}\n", e.to_string()); return; - }, - _ => () + } + _ => (), }; print!("Sent message of size {}\n", length); @@ -159,16 +167,15 @@ 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 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()) }); match res { - Ok(_) => (), - Err(s) => panic!(s) + Ok(_) => (), + Err(s) => panic!(s), }; } |