Files
tether/core/examples/lan_p2p.rs
Patrick Ecord d3dd785921 core: file/image transfer over the RTC data channel (chunked, P2P)
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>
2026-06-21 11:54:29 -05:00

57 lines
1.9 KiB
Rust

//! Proves serverless LAN sync: two engines with a DEAD server URL (so SSE and
//! RTC cannot deliver) discover each other over mDNS and exchange a clipboard
//! over plain TCP.
//! cargo run --example lan_p2p
//!
//! If the message arrives, it can only have come via the LAN transport.
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, _connected: bool) {}
fn on_file(&self, _name: String, _mime: String, _data: Vec<u8>) {}
}
fn main() {
// Unroutable server → SSE/RTC are dead; only LAN can carry the message.
let dead = "http://127.0.0.1:1".to_string();
let room = "lan-spike".to_string();
let (tx, rx) = mpsc::channel();
let a = Engine::new(dead.clone(), room.clone(), "peer-a".into(), "lan-a".into(), "LAN A".into(), "".into());
a.start(Box::new(Collector(tx)));
let b_from = "peer-b";
let b = Engine::new(dead, room, b_from.into(), "lan-b".into(), "LAN B".into(), "".into());
b.start(Box::new(Collector(mpsc::channel().0)));
eprintln!("waiting for mDNS discovery + TCP connect…");
std::thread::sleep(Duration::from_secs(4));
let payload = "hello-over-lan";
b.send(payload.to_string());
match rx.recv_timeout(Duration::from_secs(6)) {
Ok(m) if m.text == payload => {
println!(
"✅ serverless LAN delivery: text={:?} from={} source={}",
m.text, m.from, m.source
);
}
Ok(m) => println!("⚠️ unexpected: {m:?}"),
Err(_) => {
println!("❌ nothing received — mDNS may be blocked, or peers didn't connect");
std::process::exit(1);
}
}
a.stop();
b.stop();
}