shadowsocks_service/net/
flow.rs

1//! Server flow statistic
2
3use std::sync::atomic::Ordering;
4
5#[cfg(target_has_atomic = "64")]
6type FlowCounter = std::sync::atomic::AtomicU64;
7#[cfg(not(target_has_atomic = "64"))]
8type FlowCounter = std::sync::atomic::AtomicU32;
9
10/// Connection flow statistic
11pub struct FlowStat {
12    tx: FlowCounter,
13    rx: FlowCounter,
14}
15
16impl Default for FlowStat {
17    fn default() -> Self {
18        Self {
19            tx: FlowCounter::new(0),
20            rx: FlowCounter::new(0),
21        }
22    }
23}
24
25impl FlowStat {
26    /// Create an empty flow statistic
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    /// Transmitted bytes count
32    pub fn tx(&self) -> u64 {
33        self.tx.load(Ordering::Relaxed) as _
34    }
35
36    /// Increase transmitted bytes
37    pub fn incr_tx(&self, n: u64) {
38        self.tx.fetch_add(n as _, Ordering::AcqRel);
39    }
40
41    /// Received bytes count
42    pub fn rx(&self) -> u64 {
43        self.rx.load(Ordering::Relaxed) as _
44    }
45
46    /// Increase received bytes
47    pub fn incr_rx(&self, n: u64) {
48        self.rx.fetch_add(n as _, Ordering::AcqRel);
49    }
50}