init
This commit is contained in:
55
crates/vpnshare-transport/src/lib.rs
Normal file
55
crates/vpnshare-transport/src/lib.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
//! Transport abstraction for USB, Wi-Fi, and hotspot-local VSHP sessions.
|
||||
|
||||
use std::io;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TransportKind {
|
||||
UsbAccessory,
|
||||
WifiLan,
|
||||
LocalOnlyHotspot,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TransportDescriptor {
|
||||
pub kind: TransportKind,
|
||||
pub label: String,
|
||||
pub mtu_hint: Option<u16>,
|
||||
}
|
||||
|
||||
pub trait PacketTransport {
|
||||
fn descriptor(&self) -> &TransportDescriptor;
|
||||
fn send_frame(&mut self, bytes: &[u8]) -> io::Result<()>;
|
||||
fn receive_frame(&mut self, buffer: &mut [u8]) -> io::Result<usize>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ResumeToken {
|
||||
pub session_id: [u8; 16],
|
||||
pub transport_generation: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Backpressure {
|
||||
pub queued_bytes: usize,
|
||||
pub max_queue_bytes: usize,
|
||||
}
|
||||
|
||||
impl Backpressure {
|
||||
pub fn should_pause_reads(&self) -> bool {
|
||||
self.queued_bytes >= self.max_queue_bytes
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn backpressure_pauses_when_queue_is_full() {
|
||||
assert!(Backpressure {
|
||||
queued_bytes: 1024,
|
||||
max_queue_bytes: 1024,
|
||||
}
|
||||
.should_pause_reads());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user