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);
|
|
90 |
self.recover();
|
14816
|
91 |
}
|
14478
|
92 |
}
|
12136
|
93 |
}
|
15853
|
94 |
None
|
|
95 |
}
|
|
96 |
|
|
97 |
pub async fn read_from<R: AsyncReadExt + Unpin>(
|
|
98 |
&mut self,
|
|
99 |
stream: &mut R,
|
15854
|
100 |
) -> Result<HwProtocolMessage> {
|
|
101 |
use ProtocolError::*;
|
|
102 |
|
15853
|
103 |
loop {
|
|
104 |
if !self.buffer.has_remaining() {
|
15854
|
105 |
match timeout(self.read_timeout, stream.read_buf(&mut self.buffer)).await {
|
|
106 |
Err(_) => return Err(Timeout),
|
|
107 |
Ok(Err(e)) => return Err(Network(Box::new(e))),
|
|
108 |
Ok(Ok(0)) => return Err(Eof),
|
|
109 |
Ok(Ok(_)) => (),
|
|
110 |
};
|
15853
|
111 |
}
|
|
112 |
while !self.buffer.is_empty() {
|
|
113 |
if let Some(result) = self.extract_message() {
|
15854
|
114 |
return Ok(result);
|
15853
|
115 |
}
|
|
116 |
}
|
|
117 |
}
|
12129
|
118 |
}
|
|
119 |
}
|