Add Engine.send_file(name, mime, data) and MessageHandler.on_file. Files stream over each open RTC data channel as binary frames — M<json meta> · C<16-byte id><16KB chunk>… · E<id> — and reassemble on the receiver in arrival order (the channel is reliable+ordered). The data channel now distinguishes string (clipboard) from binary (file) messages. Files are direct-only — never relayed, matching prefer-direct. Transport trait gains start(...files) + a default-noop send_file (only RTC implements it). The agent saves received files to ~/Downloads; the apps stub on_file (receive UI is a follow-up). All MessageHandler impls updated. Proven by examples/file_demo.rs: A sends a 50KB blob (4 chunks), B reassembles it to identical bytes over RTC. Follow-ups: file transfer over LAN (same-network without RTC); attach/preview UI; progress + size cap. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
52 lines
1.8 KiB
Rust
52 lines
1.8 KiB
Rust
//! Proves account-based LAN pairing: two devices in DIFFERENT rooms but with the
|
|
//! SAME account auto-connect over the LAN and sync — no shared room, no server.
|
|
//! cargo run --example account_pair
|
|
//!
|
|
//! This is the "same account, same network → just pair" behavior.
|
|
|
|
use std::sync::mpsc;
|
|
use std::time::Duration;
|
|
|
|
use tethercore::{Engine, Message, MessageHandler};
|
|
|
|
struct Collector(mpsc::Sender<Message>);
|
|
impl MessageHandler for Collector {
|
|
fn on_message(&self, msg: Message) {
|
|
let _ = self.0.send(msg);
|
|
}
|
|
fn on_status(&self, _c: bool) {}
|
|
fn on_file(&self, _name: String, _mime: String, _data: Vec<u8>) {}
|
|
}
|
|
|
|
fn main() {
|
|
let dead = "http://127.0.0.1:1".to_string(); // no server — LAN only
|
|
let account = "patrick@example.com"; // same identity on both devices
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
// Note the DIFFERENT rooms — they should still pair via the shared account.
|
|
let a = Engine::new(dead.clone(), "room-alpha".into(), "dev-a".into(), "macos".into(), "Mac".into(), account.into());
|
|
a.start(Box::new(Collector(tx)));
|
|
|
|
let b = Engine::new(dead, "room-bravo".into(), "dev-b".into(), "ios".into(), "Phone".into(), account.into());
|
|
b.start(Box::new(Collector(mpsc::channel().0)));
|
|
|
|
eprintln!("different rooms, same account — waiting for LAN pair…");
|
|
std::thread::sleep(Duration::from_secs(5));
|
|
|
|
let payload = "synced-by-account";
|
|
b.send(payload.to_string());
|
|
|
|
match rx.recv_timeout(Duration::from_secs(6)) {
|
|
Ok(m) if m.text == payload => {
|
|
println!("✅ same-account devices in different rooms synced over LAN: {:?}", m.text);
|
|
}
|
|
Ok(m) => println!("⚠️ unexpected: {m:?}"),
|
|
Err(_) => {
|
|
println!("❌ no sync — account pairing failed");
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
a.stop();
|
|
b.stop();
|
|
}
|