Add traffic stats callback to FFI (iOS + Android)#134
Conversation
358795b to
d9bffc6
Compare
|
|
||
| match result { | ||
| Ok((client_to_remote, remote_to_client)) => { | ||
| traffic::add_upload_bytes(client_to_remote); |
There was a problem hiding this comment.
one downside to this is that it waits for copy_bidirectional to finish - which means if it's a large download, you won't see it immediately and it won't be useful if we ever want to display speeds. we probably need some wiring inside of copy_bidirectional to do better.
There was a problem hiding this comment.
Good point! Fixed — replaced the post-hoc counting with a TrafficCountingStream wrapper that intercepts poll_read/poll_write on the local TUN connection. Bytes are now reported to the atomic counters in real time as they flow through copy_bidirectional, so long-lived connections show traffic immediately and this would support speed display in the future.
The TCP counting lines in mod.rs are replaced with:
let mut counting = traffic::TrafficCountingStream::new(connection);
let result = tokio::io::copy_bidirectional(&mut counting, &mut remote).await;UDP counting remains per-message (already incremental).
26f5a78 to
e289cfc
Compare
Wire cumulative upload/download byte counters through the TUN server. TCP bytes are counted after copy_bidirectional completes; UDP bytes are counted per-message in destination tasks. A 1-second timer in the TUN event loop invokes the platform callback with current totals. iOS: shoes_start now takes a ShoesTrafficCallback parameter. Android: ShoesNative.start now takes a TrafficListener parameter. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
e289cfc to
8096e26
Compare
Summary
src/tun/traffic.rsmodulecopy_bidirectionalcompletes (both directions)tokio::time::intervaltimer inrun_tun_serverinvokes the platform callback with current totalsshoes_startnow takes aShoesTrafficCallbackC function pointer parameterShoesNative.startnow takes aTrafficListenerinterface parameterArchitecture
src/tun/traffic.rsprovides:AtomicU64globals (UPLOAD_BYTES/DOWNLOAD_BYTES)RwLock<Option<TrafficCallback>>for the platform callbackadd_upload_bytes,add_download_bytes,reset_traffic_counters,set/clear_traffic_callback,report_trafficPlatform notes
weak var activeProviderglobal as an indirect capture; the closure reads it at call timeTrafficListenerstored asGlobalRef<JObject>, JVM attached per call viaattach_current_threadFiles changed
src/tun/traffic.rssrc/tun/mod.rssrc/tun/udp_manager.rssrc/ffi/ios.rsShoesTrafficCallbackparametersrc/ffi/android.rsTrafficListenerJNI integrationinclude/shoes.hShoesNative.ktTest plan
cargo test --lib tun::traffic— 4 unit tests pass (counters, callback, clear, no-panic)ShoesTrafficCallbackis invoked every ~1s with increasing byte countsTrafficListener.onTrafficUpdatefires with correct totals🤖 Generated with Claude Code