15853
|
1 |
use bytes::{Buf, BufMut, BytesMut};
|
|
2 |
use log::*;
|
15854
|
3 |
use std::{
|
|
4 |
error::Error,
|
|
5 |
fmt::{Debug, Display, Formatter},
|
|
6 |
io,
|
|
7 |
io::ErrorKind,
|
|
8 |
marker::Unpin,
|
|
9 |
time::Duration,
|
|
10 |
};
|
|
11 |
use tokio::{io::AsyncReadExt, time::timeout};
|
15853
|
12 |
|
15854
|
13 |
use crate::protocol::ProtocolError::Timeout;
|
15826
|
14 |
use hedgewars_network_protocol::{
|
|
15 |
messages::HwProtocolMessage,
|
15854
|
16 |
parser::HwProtocolError,
|
15826
|
17 |
parser::{malformed_message, message},
|
|
18 |
};
|
12129
|
19 |
|
15854
|
20 |
#[derive(Debug)]
|
|
21 |
pub enum ProtocolError {
|
|
22 |
Eof,
|
|
23 |
Timeout,
|
|
24 |
Network(Box<dyn Error + Send>),
|
|
25 |
}
|
|
26 |
|
|
27 |
impl Display for ProtocolError {
|
|
28 |
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
29 |
match self {
|
|
30 |
ProtocolError::Eof => write!(f, "Connection reset by peer"),
|
|
31 |
ProtocolError::Timeout => write!(f, "Read operation timed out"),
|
|
32 |
ProtocolError::Network(source) => write!(f, "{:?}", source),
|
|
33 |
}
|
|
34 |
}
|
|
35 |
}
|
|
36 |
|
|
37 |
impl Error for ProtocolError {
|
|
38 |
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
39 |
if let Self::Network(source) = self {
|
|
40 |
Some(source.as_ref())
|
|
41 |
} else {
|
|
42 |
None
|
|
43 |
}
|
|
44 |
}
|
|
45 |
}
|
|
46 |
|
|
47 |
pub type Result<T> = std::result::Result<T, ProtocolError>;
|
|
48 |
|
12136
|
49 |
pub struct ProtocolDecoder {
|
15853
|
50 |
buffer: BytesMut,
|
15854
|
51 |
read_timeout: Duration,
|
14816
|
52 |
is_recovering: bool,
|
12129
|
53 |
}
|
|
54 |
|
12136
|
55 |
impl ProtocolDecoder {
|
15854
|
56 |
pub fn new(read_timeout: Duration) -> ProtocolDecoder {
|
12136
|
57 |
ProtocolDecoder {
|
15853
|
58 |
buffer: BytesMut::with_capacity(1024),
|
15854
|
59 |
read_timeout,
|
14816
|
60 |
is_recovering: false,
|
12129
|
61 |
}
|
|
62 |
}
|
|
63 |
|
14816
|
64 |
fn recover(&mut self) -> bool {
|
15853
|
65 |
self.is_recovering = match malformed_message(&self.buffer[..]) {
|
14816
|
66 |
Ok((tail, ())) => {
|
15853
|
67 |
let remaining = tail.len();
|
|
68 |
self.buffer.advance(self.buffer.len() - remaining);
|
14816
|
69 |
false
|
|
70 |
}
|
|
71 |
_ => {
|
15853
|
72 |
self.buffer.clear();
|
14816
|
73 |
true
|
|
74 |
}
|
|
75 |
};
|
|
76 |
!self.is_recovering
|
|
77 |
}
|
|
78 |
|
15853
|
79 |
fn extract_message(&mut self) -> Option<HwProtocolMessage> {
|
|
80 |
if !self.is_recovering || self.recover() {
|
|
81 |
match message(&self.buffer[..]) {
|
|
82 |
Ok((tail, message)) => {
|
|
83 |
let remaining = tail.len();
|
|
84 |
self.buffer.advance(self.buffer.len() - remaining);
|
|
85 |
return Some(message);
|
|
86 |
}
|
|
87 |
Err(nom::Err::Incomplete(_)) => {}
|
|
88 |
Err(nom::Err::Failure(e) | nom::Err::Error(e)) => {
|
|
89 |
debug!("Invalid message: {:?}", e);
|
16029
|
90 |
trace!(
|
|
91 |
"Buffer content: {:?}",
|
|
92 |
String::from_utf8_lossy(&self.buffer[..])
|
|
93 |
);
|
15853
|
94 |
self.recover();
|
14816
|
95 |
}
|
14478
|
96 |
}
|
12136
|
97 |
}
|
15853
|
98 |
None
|
|
99 |
}
|
|
100 |
|
|
101 |
pub async fn read_from<R: AsyncReadExt + Unpin>(
|
|
102 |
&mut self,
|
|
103 |
stream: &mut R,
|
15854
|
104 |
) -> Result<HwProtocolMessage> {
|
|
105 |
use ProtocolError::*;
|
|
106 |
|
15853
|
107 |
loop {
|
16028
|
108 |
let remaining = self.buffer.capacity() - self.buffer.len();
|
|
109 |
if remaining < 1024 {
|
|
110 |
self.buffer.reserve(2048 - remaining);
|
16027
|
111 |
}
|
|
112 |
|
|
113 |
if !self.buffer.has_remaining() || self.is_recovering {
|
16018
|
114 |
//todo!("ensure the buffer doesn't grow indefinitely")
|
15854
|
115 |
match timeout(self.read_timeout, stream.read_buf(&mut self.buffer)).await {
|
|
116 |
Err(_) => return Err(Timeout),
|
|
117 |
Ok(Err(e)) => return Err(Network(Box::new(e))),
|
|
118 |
Ok(Ok(0)) => return Err(Eof),
|
|
119 |
Ok(Ok(_)) => (),
|
|
120 |
};
|
15853
|
121 |
}
|
|
122 |
while !self.buffer.is_empty() {
|
|
123 |
if let Some(result) = self.extract_message() {
|
15854
|
124 |
return Ok(result);
|
15853
|
125 |
}
|
|
126 |
}
|
|
127 |
}
|
12129
|
128 |
}
|
|
129 |
}
|