use rosc::{OscMessage, OscPacket, OscType, encoder};
use std::net::UdpSocket;
#[derive(Debug, Default)]
pub struct Osc {
pub port: u16,
pub stack: Vec<(String, String)>,
}
impl Osc {
pub fn new(port: u16) -> Self {
Self {
port,
stack: Vec::new(),
}
}
pub fn run(&mut self, socket: Option<&UdpSocket>, ip: &str) {
let Some(sock) = socket else {
self.stack.clear();
return;
};
for (path, msg) in self.stack.drain(..) {
let args: Vec<OscType> = msg
.chars()
.map(|c| OscType::Int(c.to_digit(36).unwrap_or(0) as i32))
.collect();
let packet = OscPacket::Message(OscMessage {
addr: format!("/{}", path),
args,
});
if let Ok(bytes) = encoder::encode(&packet) {
let _ = sock.send_to(&bytes, (ip, self.port));
}
}
}
}