You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3KB

  1. extern crate rosc;
  2. use rosc::OscPacket;
  3. use std::env;
  4. use std::net::{SocketAddrV4, UdpSocket};
  5. use std::str::FromStr;
  6. fn main() {
  7. let args: Vec<String> = env::args().collect();
  8. let usage = format!("Usage {} IP:PORT", &args[0]);
  9. if args.len() < 2 {
  10. println!("{}", usage);
  11. ::std::process::exit(1)
  12. }
  13. let addr = match SocketAddrV4::from_str(&args[1]) {
  14. Ok(addr) => addr,
  15. Err(_) => panic!(usage),
  16. };
  17. let sock = UdpSocket::bind(addr).unwrap();
  18. println!("Listening to {}", addr);
  19. let mut buf = [0u8; rosc::decoder::MTU];
  20. loop {
  21. match sock.recv_from(&mut buf) {
  22. Ok((size, addr)) => {
  23. println!("Received packet with size {} from: {}", size, addr);
  24. let packet = rosc::decoder::decode(&buf[..size]).unwrap();
  25. handle_packet(packet);
  26. }
  27. Err(e) => {
  28. println!("Error receiving from socket: {}", e);
  29. break;
  30. }
  31. }
  32. }
  33. }
  34. fn handle_packet(packet: OscPacket) {
  35. match packet {
  36. OscPacket::Message(msg) => {
  37. println!("OSC address: {}", msg.addr);
  38. println!("OSC arguments: {:?}", msg.args);
  39. }
  40. OscPacket::Bundle(bundle) => {
  41. println!("OSC Bundle: {:?}", bundle);
  42. }
  43. }
  44. }