From 82739f693a8f982cb9c7fa37752fc4c3e36927c0 Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Sun, 22 Oct 2023 11:10:37 +0800 Subject: [PATCH] Limit wintun read speed --- src/wintuninterface.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/wintuninterface.rs b/src/wintuninterface.rs index 462abeb..ab4e4eb 100644 --- a/src/wintuninterface.rs +++ b/src/wintuninterface.rs @@ -109,13 +109,20 @@ impl WinTunInterface { let reader_thread = std::thread::spawn(move || { let block = || -> Result<(), Box> { loop { - // read data from tunnel interface - let packet = reader_session.receive_blocking()?; - let bytes = packet.bytes().to_vec(); - // Take the old data from pipe_client_cache and append the new data - let old_data = pipe_client_cache_clone.lock()?.drain(..).collect::>(); - let bytes = old_data.into_iter().chain(bytes).collect::>(); + let cached_data = pipe_client_cache_clone.lock()?.drain(..).collect::>(); + let bytes = if cached_data.len() >= mtu { + // if the cached data is greater than mtu, then sleep 1ms and return the data + std::thread::sleep(std::time::Duration::from_millis(1)); + cached_data + } else { + // read data from tunnel interface + let packet = reader_session.receive_blocking()?; + let bytes = packet.bytes().to_vec(); + // and append to the end of cached data + cached_data.into_iter().chain(bytes).collect::>() + }; + if bytes.is_empty() { continue; }