1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::chunks::Chunks;
use parking_lot::Mutex;
use std::{fmt, sync::Arc, u32};
pub(crate) const CONNECTION_ID: Id = Id(0);
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Id(u32);
impl Id {
pub(crate) fn new(id: u32) -> Id {
Id(id)
}
pub fn is_server(self) -> bool {
self.0 % 2 == 0
}
pub fn is_client(self) -> bool {
!self.is_server()
}
pub fn is_session(self) -> bool {
self.0 == 0
}
pub fn as_u32(self) -> u32 {
self.0
}
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum State {
Open,
SendClosed,
RecvClosed,
Closed
}
impl State {
pub fn can_read(self) -> bool {
match self {
State::RecvClosed | State::Closed => false,
_ => true
}
}
pub fn can_write(self) -> bool {
match self {
State::SendClosed | State::Closed => false,
_ => true
}
}
}
#[derive(Debug)]
pub(crate) struct StreamEntry {
state: State,
pub(crate) window: u32,
pub(crate) credit: u32,
pub(crate) buffer: Arc<Mutex<Chunks>>
}
impl StreamEntry {
pub(crate) fn new(window: u32, credit: u32) -> Self {
StreamEntry {
state: State::Open,
buffer: Arc::new(Mutex::new(Chunks::new())),
window,
credit
}
}
pub(crate) fn state(&self) -> State {
self.state
}
pub(crate) fn update_state(&mut self, next: State) {
use self::State::*;
let current = self.state;
match (current, next) {
(Closed, _) => {}
(Open, _) => self.state = next,
(RecvClosed, Closed) => self.state = Closed,
(RecvClosed, Open) => {}
(RecvClosed, RecvClosed) => {}
(RecvClosed, SendClosed) => self.state = Closed,
(SendClosed, Closed) => self.state = Closed,
(SendClosed, Open) => {}
(SendClosed, RecvClosed) => self.state = Closed,
(SendClosed, SendClosed) => {}
}
}
}