Files
omnivox-rs/examples/omnivox-client.rs
T
michael 268b986bb9 Rust port of OmniVox, wire-compatible with the C++ original
Byte-exact cereal binary layout, RFC 8439 ChaCha20-Poly1305, RFC 7914
scrypt defaults; tokio transport preserving the original's observable
behavior (contract B1-B3, B5 quirks included). Parity evidence: golden
C++ vectors in tests/vectors.rs and loopback behavior tests; both
cross-directions verified against the unmodified C++ binaries.

Source: https://git.0xfeedc0de.com/michael/OmniVox
2026-09-30 06:35:24 -04:00

72 lines
2.3 KiB
Rust

//! Port of OmniVox-client.cpp: connects, sends "Hello", prints what the
//! server echoes, then stops after 15 seconds like the C++ example.
#[path = "common/mod.rs"]
mod common;
use common::{DatagramPayload, PASSWORD, SALT, SERVER_ADDR, SERVER_PORT};
use omnivox::{ConnectionContext, ConnectionStatus, OmniVox, derive_encrypt_key};
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_target(false)
.without_time()
.init();
tracing::info!("Connecting to server at {SERVER_ADDR}:{SERVER_PORT}");
let client = Arc::new(OmniVox::<DatagramPayload>::new());
let key = derive_encrypt_key(PASSWORD.as_bytes(), SALT, 17, 8, 1)?;
client.set_encrypt_key(key);
let data_cb = Arc::new(|_data: &DatagramPayload, ctx: &ConnectionContext| {
tracing::info!("Server ID: {}, sent data!", ctx.id);
});
let connector = Arc::clone(&client);
let connection_cb = Arc::new(move |ctx: &ConnectionContext| match ctx.connected {
ConnectionStatus::Connected => {
tracing::info!(
"[onConnect] Successfully connected to: {}:{}",
ctx.ip,
ctx.port
);
let client = Arc::clone(&connector);
tokio::spawn(async move {
let payload = DatagramPayload {
id: 1,
v1: 0,
payload: b"Hello".to_vec(),
};
if client.send_to_server(&payload).await {
tracing::info!("Sent initial data to server");
}
});
}
ConnectionStatus::ConnectionFailed => {
tracing::info!(
"[onConnect] Connection failed or timed out to: {}:{}",
ctx.ip,
ctx.port
);
}
_ => {
tracing::info!(
"[onConnect] Unexpected connection status to: {}:{}",
ctx.ip,
ctx.port
);
}
});
client
.connect_to_server(SERVER_ADDR, SERVER_PORT, Some(data_cb), connection_cb)
.await?;
tokio::time::sleep(std::time::Duration::from_secs(15)).await;
client.stop().await;
Ok(())
}