1
mod request;
2
mod datagram;
3
mod mode;
4
mod errors;
5
mod session;
6
mod oack;
7
mod xfer;
8

            
9
pub use datagram::Datagram;
10
use request::Request;
11
use mode::Mode;
12
use oack::Oack;
13
use xfer::Xfer;
14

            
15
pub use errors::{ RequestError, RequestResult };
16
pub use session::Session;
17
pub use session::Stats as SessionStats;
18

            
19
pub const SEQUENCE_SIZE: u32 = 65536;
20

            
21

            
22
20
#[derive(Copy, Clone, Default, PartialEq, Eq)]
23
pub struct SequenceId(u16);
24

            
25
impl SequenceId {
26
2
    pub fn new(v: u16) -> Self {
27
2
	Self(v)
28
2
    }
29

            
30
8
    pub fn delta(self, other: Self) -> u16 {
31
8
	if self.0 < other.0 {
32
1
	    u16::try_from(SEQUENCE_SIZE - (other.0 as u32) + (self.0 as u32)).unwrap()
33
	} else {
34
7
	    self.0 - other.0
35
	}
36
8
    }
37

            
38
    pub fn add(self, other: Self) -> Self {
39
	Self((((self.0 as u32) + (other.0 as u32)) % SEQUENCE_SIZE) as u16)
40
    }
41

            
42
20
    pub fn as_u16(self) -> u16 {
43
20
	self.0
44
20
    }
45

            
46
    pub fn as_slice(self) -> [u8;2] {
47
	[(self.0 >> 8) as u8, (self.0 & 0xff) as u8]
48
    }
49

            
50
    #[cfg(test)]
51
    pub fn in_range(self, a: Self, b: Self) -> bool
52
    {
53
	assert!(a.0 != b.0);
54

            
55
	(a.0 < self.0 && self.0 < b.0) ||
56
	    (self.0 < b.0 && b.0 < a.0)
57
    }
58
}
59

            
60
impl std::ops::AddAssign<u16> for SequenceId {
61
11
    fn add_assign(&mut self, rhs: u16) {
62
11
        self.0 = (*self + rhs).0;
63
11
    }
64
}
65

            
66
#[cfg(test)]
67
impl std::ops::SubAssign<u16> for SequenceId {
68
2
    fn sub_assign(&mut self, rhs: u16) {
69
2
        self.0 = ((SEQUENCE_SIZE + self.0 as u32 - rhs as u32) % SEQUENCE_SIZE) as u16;
70
2
    }
71
}
72

            
73
impl std::ops::Add<u16> for SequenceId {
74
    type Output = Self;
75

            
76
71
    fn add(self, rhs: u16) -> Self::Output {
77
71
	Self((((self.0 as u32) + (rhs as u32)) % SEQUENCE_SIZE) as u16)
78
71
    }
79
}
80

            
81
impl std::fmt::Debug for SequenceId {
82
20
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83
20
	self.0.fmt(f)
84
20
    }
85
}