-
Notifications
You must be signed in to change notification settings - Fork 5
/
read.rs
239 lines (225 loc) · 9.26 KB
/
read.rs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use std::io::Result;
use std::task::{Poll, ready};
use super::min_len;
use super::super::{Stream, RoleHelper};
use super::super::state::{ReadState, HeadStore};
use crate::frame::{FrameHead, Mask, OpCode};
use crate::frame::mask::apply_mask4;
use crate::error::FrameError;
pub fn read_some<F, IO, Role, Guard>(
stream: &mut Stream<IO, Role, Guard>,
mut read: F,
buf: &mut [u8],
) -> Poll<Result<usize>>
where
F: FnMut(&mut IO, &mut [u8]) -> Poll<Result<usize>>,
Role: RoleHelper,
{
debug_assert!(buf.len() >= 14);
loop {
match stream.read_state {
// always returns 0
ReadState::Eof => return Poll::Ready(Ok(0)),
ReadState::Close => return Poll::Ready(Ok(0)),
// read a new incoming frame
ReadState::ReadHead(head_store) => {
let head_store_len = head_store.rd_left();
// write stored data to user provided buffer
if !head_store.is_empty() {
let (left, _) = buf.split_at_mut(head_store_len);
left.copy_from_slice(head_store.read());
}
let read_n = ready!(read(&mut stream.io, &mut buf[head_store_len..]))?;
// EOF ?
if read_n == 0 {
stream.read_state = ReadState::Eof;
return Poll::Ready(Ok(0));
}
stream.read_state = ReadState::ProcessBuf {
beg: 0,
end: read_n + head_store_len,
processed: 0,
}
}
// continue to read data from the same frame
ReadState::ReadData { next, mask } => {
let read_n = ready!(read(&mut stream.io, buf))?;
// EOF ?
if read_n == 0 {
stream.read_state = ReadState::Eof;
return Poll::Ready(Ok(0));
}
let len = min_len(read_n, next);
// unmask if server receives data from client
// this operation can be skipped if mask key is 0
if let Mask::Key(key) = mask {
apply_mask4(key, &mut buf[..len])
};
// read complete ?
if next > read_n as u64 {
// need to read more
stream.read_state = ReadState::ReadData {
next: next - read_n as u64,
mask,
};
return Poll::Ready(Ok(read_n));
} else {
// continue to process
stream.read_state = ReadState::ProcessBuf {
beg: len,
end: read_n,
processed: len,
}
}
}
// continue to read data from a ctrl frame
ReadState::ReadPing { next, mask } => {
let (buf, _) = stream
.heartbeat
.ping_store
.write()
.split_at_mut(next as usize);
let read_n = ready!(read(&mut stream.io, buf))?;
// EOF ?
if read_n == 0 {
stream.read_state = ReadState::Eof;
return Poll::Ready(Ok(0));
}
// unmask if server receives data from client
// this operation can be skipped if mask key is 0
if let Mask::Key(key) = mask {
apply_mask4(key, buf);
};
stream.heartbeat.ping_store.advance_wr_pos(read_n);
// read complete ?
if next == read_n as u8 {
stream.heartbeat.is_complete = true;
stream.read_state = ReadState::new();
} else {
stream.read_state = ReadState::ReadPing {
next: next - read_n as u8,
mask,
};
}
return Poll::Ready(Ok(0));
}
// handle the read data in user provided buffer
ReadState::ProcessBuf {
mut beg,
end,
mut processed,
} => {
// parse head, fin is ignored
let (
FrameHead {
opcode,
mask,
length,
..
},
parse_n,
) = match FrameHead::decode(&buf[beg..end]) {
Ok(x) => x,
Err(ref e) if *e == FrameError::NotEnoughData => {
if beg == end {
stream.read_state = ReadState::new();
} else {
stream.read_state =
ReadState::ReadHead(HeadStore::new_with_data(&buf[beg..end]));
}
return Poll::Ready(Ok(processed));
}
Err(e) => return Poll::Ready(Err(e.into())),
};
// point to payload
beg += parse_n;
// may read a frame without payload
let frame_len = length.to_num();
let buf_len = end - beg;
let data_len = min_len(buf_len, frame_len);
match opcode {
// text is not allowed
// we never send a ping, so we ignore the pong
OpCode::Text | OpCode::Pong => {
return Poll::Ready(Err(FrameError::UnsupportedOpcode.into()));
}
// ignore fin flag
OpCode::Binary | OpCode::Continue => {
if data_len != 0 {
// unmask payload data from client
if let Mask::Key(key) = mask {
apply_mask4(key, &mut buf[beg..beg + data_len]);
}
// move forward
unsafe {
std::ptr::copy(
buf.as_ptr().add(beg),
buf.as_mut_ptr().add(processed),
data_len,
);
};
}
beg += data_len;
processed += data_len;
// need to read more payload
if frame_len > buf_len as u64 {
stream.read_state = ReadState::ReadData {
next: frame_len - data_len as u64,
mask,
};
return Poll::Ready(Ok(processed));
}
// continue to process
stream.read_state = ReadState::ProcessBuf {
beg,
end,
processed,
};
}
OpCode::Ping => {
// a ping frame must not have extened data
if frame_len > 125 {
return Poll::Ready(Err(FrameError::IllegalData.into()));
}
if data_len != 0 {
// unmask payload data from client
if let Mask::Key(key) = mask {
apply_mask4(key, &mut buf[beg..beg + data_len]);
}
// save ping data
stream
.heartbeat
.ping_store
.replace_with_data(&buf[beg..beg + data_len]);
} else {
// no payload
stream.heartbeat.ping_store.reset();
}
// processed does not increase;
beg += data_len;
// need to read more payload
if frame_len > buf_len as u64 {
stream.heartbeat.is_complete = false;
stream.read_state = ReadState::ReadPing {
next: frame_len as u8 - data_len as u8,
mask,
};
return Poll::Ready(Ok(processed));
}
// continue to process
stream.heartbeat.is_complete = true;
stream.read_state = ReadState::ProcessBuf {
beg,
end,
processed,
};
}
OpCode::Close => {
stream.read_state = ReadState::Close;
return Poll::Ready(Ok(processed));
}
}
}
}
}
}