Hello,
I am trying to use rosc from tokio. My first step is to decode and display incoming osc messages. I have made the server:
use rosc::decoder;
use rosc::{OscMessage, OscPacket};
use tokio::io::AsyncReadExt;
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("start");
let listener = TcpListener::bind("0.0.0.0:9000").await?;
println!("listening");
loop {
match listener.accept().await {
Ok((mut socket, addr)) => {
println!("new client: {:?}", addr);
println!("ready");
tokio::spawn(async move {
let mut buf = [0; 1024];
// In a loop, read data from the socket and write the data back.
loop {
let _ = match socket.read(&mut buf).await {
Ok(n) if n == 0 => {
eprintln!("end tran");
return;
}
Ok(n) => {
println!("{:?}", &buf[..n]);
println!("read {} bytes", n);
match decoder::decode_tcp(&buf[..n]) {
Ok((_, payload)) => {
println!("{:?}", payload);
return;
}
_ => {
eprintln!("decoding error");
return;
}
}
}
Err(e) => {
eprintln!("failed to read from socket; err = {:?}", e);
return;
}
};
}
});
}
Err(e) => eprintln!("could not get client: {:?}", e),
}
}
}
and a simple client inspired from documentation:
use rosc::encoder;
use rosc::{OscMessage, OscPacket, OscType};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "127.0.0.1:9000";
let mut stream = TcpStream::connect(addr).await?;
let osc_packet = OscPacket::Message(OscMessage {
addr: "/greet/me".to_string(),
args: vec![OscType::String("hi!".to_string())],
});
if let Ok(buf) = encoder::encode(&osc_packet) {
println!("send packet");
println!("{:?}", buf);
stream.write_all(&buf).await?;
println!("transmitted");
}
Ok(())
}
When the server receives the packet it seems to fail decoding it, and put everything in the left first tuple element:
([47, 103, 114, 101, 101, 116, 47, 109, 101, 0, 0, 0, 44, 115, 0, 0, 104, 105, 33, 0], None)
I have made a little program to see what happens:
use rosc::{encoder,decoder};
use rosc::{OscMessage, OscPacket, OscType};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let osc_packet = OscPacket::Message(OscMessage {
addr: "/greet/me".to_string(),
args: vec![OscType::String("hi!".to_string())],
});
if let Ok(buf) = encoder::encode(&osc_packet) {
println!("{:?}", buf);
let payload = decoder::decode_tcp(&buf)?;
println!("{:?}", payload);
}
Ok(())
}
got this output:
mik@debian:~/hello$ cargo run --example test_decode
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/examples/test_decode`
[47, 103, 114, 101, 101, 116, 47, 109, 101, 0, 0, 0, 44, 115, 0, 0, 104, 105, 33, 0]
([47, 103, 114, 101, 101, 116, 47, 109, 101, 0, 0, 0, 44, 115, 0, 0, 104, 105, 33, 0], None)
mik@debian:~/hello$
I guess I missuse the api. I tried to find something to decode directly from a buffer in the API without succeed.