author | alfadur |
Tue, 04 Jun 2019 22:25:28 +0300 | |
changeset 15116 | cce6e707172f |
parent 15114 | a7841105493e |
child 15118 | 0e59abde6766 |
permissions | -rw-r--r-- |
13431
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13423
diff
changeset
|
1 |
/** The parsers for the chat and multiplayer protocol. The main parser is `message`. |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13423
diff
changeset
|
2 |
* # Protocol |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13423
diff
changeset
|
3 |
* All messages consist of `\n`-separated strings. The end of a message is |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13423
diff
changeset
|
4 |
* indicated by a double newline - `\n\n`. |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13423
diff
changeset
|
5 |
* |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13423
diff
changeset
|
6 |
* For example, a nullary command like PING will be actually sent as `PING\n\n`. |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13423
diff
changeset
|
7 |
* A unary command, such as `START_GAME nick` will be actually sent as `START_GAME\nnick\n\n`. |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13423
diff
changeset
|
8 |
*/ |
15114 | 9 |
use nom::{ |
10 |
branch::alt, |
|
11 |
bytes::complete::{tag, tag_no_case, take_until, take_while}, |
|
12 |
character::complete::{newline, not_line_ending}, |
|
13 |
combinator::peek, |
|
14 |
error::{ErrorKind, ParseError}, |
|
15 |
multi::separated_list, |
|
16 |
sequence::{pairc, precededc, terminatedc}, |
|
17 |
Err, IResult, |
|
18 |
}; |
|
19 |
||
14775 | 20 |
use std::{ |
21 |
num::ParseIntError, |
|
22 |
ops::Range, |
|
23 |
str, |
|
24 |
str::{FromStr, Utf8Error}, |
|
25 |
}; |
|
12133 | 26 |
|
15075 | 27 |
use super::messages::{HwProtocolMessage, HwProtocolMessage::*}; |
15074 | 28 |
use crate::core::types::{ |
14783 | 29 |
GameCfg, HedgehogInfo, ServerVar, TeamInfo, VoteType, MAX_HEDGEHOGS_PER_TEAM, |
30 |
}; |
|
14775 | 31 |
|
32 |
#[derive(Debug, PartialEq)] |
|
15075 | 33 |
pub struct HwProtocolError {} |
14775 | 34 |
|
15075 | 35 |
impl HwProtocolError { |
14775 | 36 |
fn new() -> Self { |
15075 | 37 |
HwProtocolError {} |
14775 | 38 |
} |
39 |
} |
|
40 |
||
15075 | 41 |
impl<I> ParseError<I> for HwProtocolError { |
14775 | 42 |
fn from_error_kind(input: I, kind: ErrorKind) -> Self { |
15075 | 43 |
HwProtocolError::new() |
14775 | 44 |
} |
45 |
||
46 |
fn append(input: I, kind: ErrorKind, other: Self) -> Self { |
|
15075 | 47 |
HwProtocolError::new() |
14775 | 48 |
} |
49 |
} |
|
50 |
||
15075 | 51 |
impl From<Utf8Error> for HwProtocolError { |
14775 | 52 |
fn from(_: Utf8Error) -> Self { |
15075 | 53 |
HwProtocolError::new() |
14775 | 54 |
} |
55 |
} |
|
56 |
||
15075 | 57 |
impl From<ParseIntError> for HwProtocolError { |
14775 | 58 |
fn from(_: ParseIntError) -> Self { |
15075 | 59 |
HwProtocolError::new() |
14775 | 60 |
} |
61 |
} |
|
62 |
||
15075 | 63 |
pub type HwResult<'a, O> = IResult<&'a [u8], O, HwProtocolError>; |
14775 | 64 |
|
15075 | 65 |
fn end_of_message(input: &[u8]) -> HwResult<&[u8]> { |
14775 | 66 |
tag("\n\n")(input) |
67 |
} |
|
68 |
||
15075 | 69 |
fn convert_utf8(input: &[u8]) -> HwResult<&str> { |
14775 | 70 |
match str::from_utf8(input) { |
71 |
Ok(str) => Ok((b"", str)), |
|
72 |
Err(utf_err) => Result::Err(Err::Failure(utf_err.into())), |
|
73 |
} |
|
74 |
} |
|
75 |
||
15075 | 76 |
fn convert_from_str<T>(str: &str) -> HwResult<T> |
14775 | 77 |
where |
78 |
T: FromStr<Err = ParseIntError>, |
|
79 |
{ |
|
80 |
match T::from_str(str) { |
|
81 |
Ok(x) => Ok((b"", x)), |
|
82 |
Err(format_err) => Result::Err(Err::Failure(format_err.into())), |
|
83 |
} |
|
84 |
} |
|
85 |
||
15075 | 86 |
fn str_line(input: &[u8]) -> HwResult<&str> { |
15116 | 87 |
let (i, text) = not_line_ending(input.clone())?; |
88 |
if i != input { |
|
89 |
Ok((i, convert_utf8(text)?.1)) |
|
90 |
} else { |
|
91 |
Err(Err::Error(HwProtocolError::new())) |
|
92 |
} |
|
14775 | 93 |
} |
94 |
||
15075 | 95 |
fn a_line(input: &[u8]) -> HwResult<String> { |
14775 | 96 |
let (i, str) = str_line(input)?; |
97 |
Ok((i, str.to_string())) |
|
98 |
} |
|
99 |
||
15075 | 100 |
fn hw_tag<'a>(tag_str: &'a str) -> impl Fn(&'a [u8]) -> HwResult<'a, ()> { |
14775 | 101 |
move |i| tag(tag_str)(i).map(|(i, _)| (i, ())) |
102 |
} |
|
103 |
||
15075 | 104 |
fn hw_tag_no_case<'a>(tag_str: &'a str) -> impl Fn(&'a [u8]) -> HwResult<'a, ()> { |
14775 | 105 |
move |i| tag_no_case(tag_str)(i).map(|(i, _)| (i, ())) |
106 |
} |
|
107 |
||
15075 | 108 |
fn cmd_arg(input: &[u8]) -> HwResult<String> { |
14775 | 109 |
let delimiters = b" \n"; |
15116 | 110 |
let (i, str) = take_while(move |c| !delimiters.contains(&c))(input.clone())?; |
111 |
if i != input { |
|
112 |
Ok((i, convert_utf8(str)?.1.to_string())) |
|
113 |
} else { |
|
114 |
Err(Err::Error(HwProtocolError::new())) |
|
115 |
} |
|
14775 | 116 |
} |
117 |
||
15075 | 118 |
fn u8_line(input: &[u8]) -> HwResult<u8> { |
14775 | 119 |
let (i, str) = str_line(input)?; |
120 |
Ok((i, convert_from_str(str)?.1)) |
|
121 |
} |
|
122 |
||
15075 | 123 |
fn u16_line(input: &[u8]) -> HwResult<u16> { |
14775 | 124 |
let (i, str) = str_line(input)?; |
125 |
Ok((i, convert_from_str(str)?.1)) |
|
126 |
} |
|
127 |
||
15075 | 128 |
fn u32_line(input: &[u8]) -> HwResult<u32> { |
14775 | 129 |
let (i, str) = str_line(input)?; |
130 |
Ok((i, convert_from_str(str)?.1)) |
|
131 |
} |
|
132 |
||
15075 | 133 |
fn yes_no_line(input: &[u8]) -> HwResult<bool> { |
14775 | 134 |
alt(( |
135 |
|i| tag_no_case(b"YES")(i).map(|(i, _)| (i, true)), |
|
136 |
|i| tag_no_case(b"NO")(i).map(|(i, _)| (i, false)), |
|
137 |
))(input) |
|
138 |
} |
|
139 |
||
15075 | 140 |
fn opt_arg<'a>(input: &'a [u8]) -> HwResult<'a, Option<String>> { |
14775 | 141 |
alt(( |
15114 | 142 |
|i| peek(end_of_message)(i).map(|(i, _)| (i, None)), |
14775 | 143 |
|i| precededc(i, hw_tag("\n"), a_line).map(|(i, v)| (i, Some(v))), |
144 |
))(input) |
|
145 |
} |
|
146 |
||
15075 | 147 |
fn spaces(input: &[u8]) -> HwResult<&[u8]> { |
14775 | 148 |
precededc(input, hw_tag(" "), |i| take_while(|c| c == b' ')(i)) |
149 |
} |
|
150 |
||
15075 | 151 |
fn opt_space_arg<'a>(input: &'a [u8]) -> HwResult<'a, Option<String>> { |
14775 | 152 |
alt(( |
15114 | 153 |
|i| peek(end_of_message)(i).map(|(i, _)| (i, None)), |
14775 | 154 |
|i| precededc(i, spaces, a_line).map(|(i, v)| (i, Some(v))), |
155 |
))(input) |
|
156 |
} |
|
12134 | 157 |
|
15075 | 158 |
fn hedgehog_array(input: &[u8]) -> HwResult<[HedgehogInfo; 8]> { |
159 |
fn hedgehog_line(input: &[u8]) -> HwResult<HedgehogInfo> { |
|
15114 | 160 |
let (i, name) = terminatedc(input, a_line, newline)?; |
14775 | 161 |
let (i, hat) = a_line(i)?; |
162 |
Ok((i, HedgehogInfo { name, hat })) |
|
163 |
} |
|
164 |
||
15114 | 165 |
let (i, h1) = terminatedc(input, hedgehog_line, newline)?; |
166 |
let (i, h2) = terminatedc(i, hedgehog_line, newline)?; |
|
167 |
let (i, h3) = terminatedc(i, hedgehog_line, newline)?; |
|
168 |
let (i, h4) = terminatedc(i, hedgehog_line, newline)?; |
|
169 |
let (i, h5) = terminatedc(i, hedgehog_line, newline)?; |
|
170 |
let (i, h6) = terminatedc(i, hedgehog_line, newline)?; |
|
171 |
let (i, h7) = terminatedc(i, hedgehog_line, newline)?; |
|
14775 | 172 |
let (i, h8) = hedgehog_line(i)?; |
173 |
||
174 |
Ok((i, [h1, h2, h3, h4, h5, h6, h7, h8])) |
|
175 |
} |
|
12133 | 176 |
|
15075 | 177 |
fn voting(input: &[u8]) -> HwResult<VoteType> { |
14775 | 178 |
alt(( |
179 |
|i| tag_no_case("PAUSE")(i).map(|(i, _)| (i, VoteType::Pause)), |
|
180 |
|i| tag_no_case("NEWSEED")(i).map(|(i, _)| (i, VoteType::NewSeed)), |
|
181 |
|i| { |
|
182 |
precededc(i, |i| precededc(i, hw_tag_no_case("KICK"), spaces), a_line) |
|
183 |
.map(|(i, s)| (i, VoteType::Kick(s))) |
|
184 |
}, |
|
185 |
|i| { |
|
186 |
precededc( |
|
187 |
i, |
|
188 |
|i| precededc(i, hw_tag_no_case("HEDGEHOGS"), spaces), |
|
189 |
u8_line, |
|
190 |
) |
|
191 |
.map(|(i, n)| (i, VoteType::HedgehogsPerTeam(n))) |
|
192 |
}, |
|
193 |
|i| precededc(i, hw_tag_no_case("MAP"), opt_space_arg).map(|(i, v)| (i, VoteType::Map(v))), |
|
194 |
))(input) |
|
195 |
} |
|
12135 | 196 |
|
15075 | 197 |
fn no_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> { |
14775 | 198 |
fn messagec<'a>( |
199 |
input: &'a [u8], |
|
200 |
name: &'a str, |
|
15075 | 201 |
msg: HwProtocolMessage, |
202 |
) -> HwResult<'a, HwProtocolMessage> { |
|
14775 | 203 |
tag(name)(input).map(|(i, _)| (i, msg.clone())) |
204 |
} |
|
205 |
||
206 |
alt(( |
|
207 |
|i| messagec(i, "PING", Ping), |
|
208 |
|i| messagec(i, "PONG", Pong), |
|
209 |
|i| messagec(i, "LIST", List), |
|
210 |
|i| messagec(i, "BANLIST", BanList), |
|
211 |
|i| messagec(i, "GET_SERVER_VAR", GetServerVar), |
|
212 |
|i| messagec(i, "TOGGLE_READY", ToggleReady), |
|
213 |
|i| messagec(i, "START_GAME", StartGame), |
|
214 |
|i| messagec(i, "TOGGLE_RESTRICT_JOINS", ToggleRestrictJoin), |
|
215 |
|i| messagec(i, "TOGGLE_RESTRICT_TEAMS", ToggleRestrictTeams), |
|
216 |
|i| messagec(i, "TOGGLE_REGISTERED_ONLY", ToggleRegisteredOnly), |
|
217 |
))(input) |
|
218 |
} |
|
12135 | 219 |
|
15075 | 220 |
fn single_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> { |
14775 | 221 |
fn messagec<'a, T, F, G>( |
222 |
input: &'a [u8], |
|
223 |
name: &'a str, |
|
224 |
parser: F, |
|
225 |
constructor: G, |
|
15075 | 226 |
) -> HwResult<'a, HwProtocolMessage> |
14775 | 227 |
where |
15075 | 228 |
F: Fn(&[u8]) -> HwResult<T>, |
229 |
G: Fn(T) -> HwProtocolMessage, |
|
14775 | 230 |
{ |
231 |
precededc(input, hw_tag(name), parser).map(|(i, v)| (i, constructor(v))) |
|
232 |
} |
|
233 |
||
234 |
alt(( |
|
235 |
|i| messagec(i, "NICK\n", a_line, Nick), |
|
236 |
|i| messagec(i, "INFO\n", a_line, Info), |
|
237 |
|i| messagec(i, "CHAT\n", a_line, Chat), |
|
238 |
|i| messagec(i, "PART", opt_arg, Part), |
|
239 |
|i| messagec(i, "FOLLOW\n", a_line, Follow), |
|
240 |
|i| messagec(i, "KICK\n", a_line, Kick), |
|
241 |
|i| messagec(i, "UNBAN\n", a_line, Unban), |
|
242 |
|i| messagec(i, "EM\n", a_line, EngineMessage), |
|
243 |
|i| messagec(i, "TEAMCHAT\n", a_line, TeamChat), |
|
244 |
|i| messagec(i, "ROOM_NAME\n", a_line, RoomName), |
|
245 |
|i| messagec(i, "REMOVE_TEAM\n", a_line, RemoveTeam), |
|
246 |
|i| messagec(i, "ROUNDFINISHED", opt_arg, |_| RoundFinished), |
|
247 |
|i| messagec(i, "PROTO\n", u16_line, Proto), |
|
248 |
|i| messagec(i, "QUIT", opt_arg, Quit), |
|
249 |
))(input) |
|
250 |
} |
|
12135 | 251 |
|
15075 | 252 |
fn cmd_message<'a>(input: &'a [u8]) -> HwResult<'a, HwProtocolMessage> { |
14775 | 253 |
fn cmdc_no_arg<'a>( |
254 |
input: &'a [u8], |
|
255 |
name: &'a str, |
|
15075 | 256 |
msg: HwProtocolMessage, |
257 |
) -> HwResult<'a, HwProtocolMessage> { |
|
14775 | 258 |
tag_no_case(name)(input).map(|(i, _)| (i, msg.clone())) |
259 |
} |
|
260 |
||
261 |
fn cmdc_single_arg<'a, T, F, G>( |
|
262 |
input: &'a [u8], |
|
263 |
name: &'a str, |
|
264 |
parser: F, |
|
265 |
constructor: G, |
|
15075 | 266 |
) -> HwResult<'a, HwProtocolMessage> |
14775 | 267 |
where |
15075 | 268 |
F: Fn(&'a [u8]) -> HwResult<'a, T>, |
269 |
G: Fn(T) -> HwProtocolMessage, |
|
14775 | 270 |
{ |
271 |
precededc(input, |i| pairc(i, hw_tag_no_case(name), spaces), parser) |
|
272 |
.map(|(i, v)| (i, constructor(v))) |
|
273 |
} |
|
274 |
||
15075 | 275 |
fn cmd_no_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> { |
14775 | 276 |
alt(( |
277 |
|i| cmdc_no_arg(i, "STATS", Stats), |
|
278 |
|i| cmdc_no_arg(i, "FIX", Fix), |
|
279 |
|i| cmdc_no_arg(i, "UNFIX", Unfix), |
|
280 |
|i| cmdc_no_arg(i, "REGISTERED_ONLY", ToggleServerRegisteredOnly), |
|
281 |
|i| cmdc_no_arg(i, "SUPER_POWER", SuperPower), |
|
282 |
))(input) |
|
283 |
} |
|
12135 | 284 |
|
15075 | 285 |
fn cmd_single_arg_message(input: &[u8]) -> HwResult<HwProtocolMessage> { |
14775 | 286 |
alt(( |
287 |
|i| cmdc_single_arg(i, "RESTART_SERVER", |i| tag("YES")(i), |_| RestartServer), |
|
288 |
|i| cmdc_single_arg(i, "DELEGATE", a_line, Delegate), |
|
289 |
|i| cmdc_single_arg(i, "DELETE", a_line, Delete), |
|
290 |
|i| cmdc_single_arg(i, "SAVEROOM", a_line, SaveRoom), |
|
291 |
|i| cmdc_single_arg(i, "LOADROOM", a_line, LoadRoom), |
|
292 |
|i| cmdc_single_arg(i, "GLOBAL", a_line, Global), |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
293 |
|i| cmdc_single_arg(i, "WATCH", u32_line, Watch), |
14775 | 294 |
|i| cmdc_single_arg(i, "VOTE", yes_no_line, Vote), |
295 |
|i| cmdc_single_arg(i, "FORCE", yes_no_line, ForceVote), |
|
296 |
|i| cmdc_single_arg(i, "INFO", a_line, Info), |
|
297 |
|i| cmdc_single_arg(i, "MAXTEAMS", u8_line, MaxTeams), |
|
15112 | 298 |
|i| cmdc_single_arg(i, "CALLVOTE", voting, |v| CallVote(Some(v))), |
14775 | 299 |
))(input) |
300 |
} |
|
301 |
||
302 |
precededc( |
|
303 |
input, |
|
304 |
hw_tag("CMD\n"), |
|
305 |
alt(( |
|
306 |
cmd_no_arg_message, |
|
307 |
cmd_single_arg_message, |
|
15112 | 308 |
|i| tag_no_case("CALLVOTE")(i).map(|(i, _)| (i, CallVote(None))), |
15111 | 309 |
|i| { |
310 |
precededc(i, hw_tag_no_case("GREETING"), opt_space_arg) |
|
311 |
.map(|(i, s)| (i, Greeting(s))) |
|
312 |
}, |
|
14775 | 313 |
|i| precededc(i, hw_tag_no_case("PART"), opt_space_arg).map(|(i, s)| (i, Part(s))), |
314 |
|i| precededc(i, hw_tag_no_case("QUIT"), opt_space_arg).map(|(i, s)| (i, Quit(s))), |
|
315 |
|i| { |
|
316 |
precededc(i, hw_tag_no_case("SAVE"), |i| { |
|
317 |
pairc( |
|
318 |
i, |
|
319 |
|i| precededc(i, spaces, cmd_arg), |
|
320 |
|i| precededc(i, spaces, cmd_arg), |
|
321 |
) |
|
322 |
}) |
|
323 |
.map(|(i, (n, l))| (i, Save(n, l))) |
|
324 |
}, |
|
325 |
|i| { |
|
326 |
let (i, _) = tag_no_case("RND")(i)?; |
|
15116 | 327 |
let (i, v) = alt(( |
328 |
|i| peek(end_of_message)(i).map(|(i, _)| (i, vec![])), |
|
329 |
|i| { |
|
330 |
let (i, _) = spaces(i)?; |
|
331 |
let (i, v) = separated_list(spaces, cmd_arg)(i)?; |
|
332 |
Ok((i, v)) |
|
333 |
}, |
|
334 |
))(i)?; |
|
335 |
Ok((i, Rnd(v))) |
|
14775 | 336 |
}, |
337 |
)), |
|
338 |
) |
|
339 |
} |
|
340 |
||
15075 | 341 |
fn config_message<'a>(input: &'a [u8]) -> HwResult<'a, HwProtocolMessage> { |
14775 | 342 |
fn cfgc_single_arg<'a, T, F, G>( |
343 |
input: &'a [u8], |
|
344 |
name: &'a str, |
|
345 |
parser: F, |
|
346 |
constructor: G, |
|
15075 | 347 |
) -> HwResult<'a, GameCfg> |
14775 | 348 |
where |
15075 | 349 |
F: Fn(&[u8]) -> HwResult<T>, |
14775 | 350 |
G: Fn(T) -> GameCfg, |
351 |
{ |
|
15114 | 352 |
precededc(input, |i| terminatedc(i, hw_tag(name), newline), parser) |
14775 | 353 |
.map(|(i, v)| (i, constructor(v))) |
354 |
} |
|
355 |
||
356 |
let (i, cfg) = precededc( |
|
357 |
input, |
|
358 |
hw_tag("CFG\n"), |
|
359 |
alt(( |
|
360 |
|i| cfgc_single_arg(i, "THEME", a_line, GameCfg::Theme), |
|
361 |
|i| cfgc_single_arg(i, "SCRIPT", a_line, GameCfg::Script), |
|
362 |
|i| cfgc_single_arg(i, "MAP", a_line, GameCfg::MapType), |
|
363 |
|i| cfgc_single_arg(i, "MAPGEN", u32_line, GameCfg::MapGenerator), |
|
364 |
|i| cfgc_single_arg(i, "MAZE_SIZE", u32_line, GameCfg::MazeSize), |
|
365 |
|i| cfgc_single_arg(i, "TEMPLATE", u32_line, GameCfg::Template), |
|
366 |
|i| cfgc_single_arg(i, "FEATURE_SIZE", u32_line, GameCfg::FeatureSize), |
|
367 |
|i| cfgc_single_arg(i, "SEED", a_line, GameCfg::Seed), |
|
368 |
|i| cfgc_single_arg(i, "DRAWNMAP", a_line, GameCfg::DrawnMap), |
|
369 |
|i| { |
|
370 |
precededc( |
|
371 |
i, |
|
15114 | 372 |
|i| terminatedc(i, hw_tag("AMMO"), newline), |
14775 | 373 |
|i| { |
374 |
let (i, name) = a_line(i)?; |
|
375 |
let (i, value) = opt_arg(i)?; |
|
376 |
Ok((i, GameCfg::Ammo(name, value))) |
|
377 |
}, |
|
378 |
) |
|
379 |
}, |
|
380 |
|i| { |
|
381 |
precededc( |
|
382 |
i, |
|
15114 | 383 |
|i| terminatedc(i, hw_tag("SCHEME"), newline), |
14775 | 384 |
|i| { |
385 |
let (i, name) = a_line(i)?; |
|
386 |
let (i, values) = alt(( |
|
15114 | 387 |
|i| peek(end_of_message)(i).map(|(i, _)| (i, None)), |
14775 | 388 |
|i| { |
15114 | 389 |
precededc(i, newline, |i| separated_list(newline, a_line)(i)) |
14775 | 390 |
.map(|(i, v)| (i, Some(v))) |
391 |
}, |
|
392 |
))(i)?; |
|
393 |
Ok((i, GameCfg::Scheme(name, values.unwrap_or_default()))) |
|
394 |
}, |
|
395 |
) |
|
396 |
}, |
|
397 |
)), |
|
398 |
)?; |
|
399 |
Ok((i, Cfg(cfg))) |
|
400 |
} |
|
12133 | 401 |
|
15075 | 402 |
fn server_var_message(input: &[u8]) -> HwResult<HwProtocolMessage> { |
14783 | 403 |
precededc( |
404 |
input, |
|
405 |
hw_tag("SET_SERVER_VAR\n"), |
|
406 |
alt(( |
|
407 |
|i| { |
|
408 |
precededc(i, hw_tag("MOTD_NEW\n"), a_line) |
|
409 |
.map(|(i, s)| (i, SetServerVar(ServerVar::MOTDNew(s)))) |
|
410 |
}, |
|
411 |
|i| { |
|
412 |
precededc(i, hw_tag("MOTD_OLD\n"), a_line) |
|
413 |
.map(|(i, s)| (i, SetServerVar(ServerVar::MOTDOld(s)))) |
|
414 |
}, |
|
415 |
|i| { |
|
416 |
precededc(i, hw_tag("LATEST_PROTO\n"), u16_line) |
|
417 |
.map(|(i, n)| (i, SetServerVar(ServerVar::LatestProto(n)))) |
|
418 |
}, |
|
419 |
)), |
|
420 |
) |
|
421 |
} |
|
422 |
||
15075 | 423 |
fn complex_message(input: &[u8]) -> HwResult<HwProtocolMessage> { |
14775 | 424 |
alt(( |
425 |
|i| { |
|
426 |
precededc( |
|
427 |
i, |
|
15114 | 428 |
|i| terminatedc(i, hw_tag("PASSWORD"), newline), |
14775 | 429 |
|i| { |
15114 | 430 |
let (i, pass) = terminatedc(i, a_line, newline)?; |
14775 | 431 |
let (i, salt) = a_line(i)?; |
432 |
Ok((i, Password(pass, salt))) |
|
433 |
}, |
|
434 |
) |
|
435 |
}, |
|
436 |
|i| { |
|
437 |
precededc( |
|
438 |
i, |
|
15114 | 439 |
|i| terminatedc(i, hw_tag("CHECKER"), newline), |
14775 | 440 |
|i| { |
15114 | 441 |
let (i, protocol) = terminatedc(i, u16_line, newline)?; |
442 |
let (i, name) = terminatedc(i, a_line, newline)?; |
|
14775 | 443 |
let (i, pass) = a_line(i)?; |
444 |
Ok((i, Checker(protocol, name, pass))) |
|
445 |
}, |
|
446 |
) |
|
447 |
}, |
|
448 |
|i| { |
|
449 |
precededc( |
|
450 |
i, |
|
15114 | 451 |
|i| terminatedc(i, hw_tag("CREATE_ROOM"), newline), |
14775 | 452 |
|i| { |
453 |
let (i, name) = a_line(i)?; |
|
454 |
let (i, pass) = opt_arg(i)?; |
|
455 |
Ok((i, CreateRoom(name, pass))) |
|
456 |
}, |
|
457 |
) |
|
458 |
}, |
|
459 |
|i| { |
|
460 |
precededc( |
|
461 |
i, |
|
15114 | 462 |
|i| terminatedc(i, hw_tag("JOIN_ROOM"), newline), |
14775 | 463 |
|i| { |
464 |
let (i, name) = a_line(i)?; |
|
465 |
let (i, pass) = opt_arg(i)?; |
|
466 |
Ok((i, JoinRoom(name, pass))) |
|
467 |
}, |
|
468 |
) |
|
469 |
}, |
|
470 |
|i| { |
|
471 |
precededc( |
|
472 |
i, |
|
15114 | 473 |
|i| terminatedc(i, hw_tag("ADD_TEAM"), newline), |
14775 | 474 |
|i| { |
15114 | 475 |
let (i, name) = terminatedc(i, a_line, newline)?; |
476 |
let (i, color) = terminatedc(i, u8_line, newline)?; |
|
477 |
let (i, grave) = terminatedc(i, a_line, newline)?; |
|
478 |
let (i, fort) = terminatedc(i, a_line, newline)?; |
|
479 |
let (i, voice_pack) = terminatedc(i, a_line, newline)?; |
|
480 |
let (i, flag) = terminatedc(i, a_line, newline)?; |
|
481 |
let (i, difficulty) = terminatedc(i, u8_line, newline)?; |
|
14775 | 482 |
let (i, hedgehogs) = hedgehog_array(i)?; |
483 |
Ok(( |
|
484 |
i, |
|
485 |
AddTeam(Box::new(TeamInfo { |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
486 |
owner: String::new(), |
14775 | 487 |
name, |
488 |
color, |
|
489 |
grave, |
|
490 |
fort, |
|
491 |
voice_pack, |
|
492 |
flag, |
|
493 |
difficulty, |
|
494 |
hedgehogs, |
|
495 |
hedgehogs_number: 0, |
|
496 |
})), |
|
497 |
)) |
|
498 |
}, |
|
499 |
) |
|
500 |
}, |
|
501 |
|i| { |
|
502 |
precededc( |
|
503 |
i, |
|
15114 | 504 |
|i| terminatedc(i, hw_tag("HH_NUM"), newline), |
14775 | 505 |
|i| { |
15114 | 506 |
let (i, name) = terminatedc(i, a_line, newline)?; |
14775 | 507 |
let (i, count) = u8_line(i)?; |
508 |
Ok((i, SetHedgehogsNumber(name, count))) |
|
509 |
}, |
|
510 |
) |
|
511 |
}, |
|
512 |
|i| { |
|
513 |
precededc( |
|
514 |
i, |
|
15114 | 515 |
|i| terminatedc(i, hw_tag("TEAM_COLOR"), newline), |
14775 | 516 |
|i| { |
15114 | 517 |
let (i, name) = terminatedc(i, a_line, newline)?; |
14775 | 518 |
let (i, color) = u8_line(i)?; |
519 |
Ok((i, SetTeamColor(name, color))) |
|
520 |
}, |
|
521 |
) |
|
522 |
}, |
|
523 |
|i| { |
|
524 |
precededc( |
|
525 |
i, |
|
15114 | 526 |
|i| terminatedc(i, hw_tag("BAN"), newline), |
14775 | 527 |
|i| { |
15114 | 528 |
let (i, n) = terminatedc(i, a_line, newline)?; |
529 |
let (i, r) = terminatedc(i, a_line, newline)?; |
|
14775 | 530 |
let (i, t) = u32_line(i)?; |
531 |
Ok((i, Ban(n, r, t))) |
|
532 |
}, |
|
533 |
) |
|
534 |
}, |
|
535 |
|i| { |
|
536 |
precededc( |
|
537 |
i, |
|
15114 | 538 |
|i| terminatedc(i, hw_tag("BAN_IP"), newline), |
14775 | 539 |
|i| { |
15114 | 540 |
let (i, n) = terminatedc(i, a_line, newline)?; |
541 |
let (i, r) = terminatedc(i, a_line, newline)?; |
|
14775 | 542 |
let (i, t) = u32_line(i)?; |
543 |
Ok((i, BanIP(n, r, t))) |
|
544 |
}, |
|
545 |
) |
|
546 |
}, |
|
547 |
|i| { |
|
548 |
precededc( |
|
549 |
i, |
|
15114 | 550 |
|i| terminatedc(i, hw_tag("BAN_NICK"), newline), |
14775 | 551 |
|i| { |
15114 | 552 |
let (i, n) = terminatedc(i, a_line, newline)?; |
553 |
let (i, r) = terminatedc(i, a_line, newline)?; |
|
14775 | 554 |
let (i, t) = u32_line(i)?; |
555 |
Ok((i, BanNick(n, r, t))) |
|
556 |
}, |
|
557 |
) |
|
558 |
}, |
|
559 |
))(input) |
|
560 |
} |
|
13422 | 561 |
|
15075 | 562 |
pub fn malformed_message(input: &[u8]) -> HwResult<()> { |
14795 | 563 |
let (i, _) = terminatedc(input, |i| take_until(&b"\n\n"[..])(i), end_of_message)?; |
564 |
Ok((i, ())) |
|
14775 | 565 |
} |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
566 |
|
15075 | 567 |
pub fn message(input: &[u8]) -> HwResult<HwProtocolMessage> { |
14795 | 568 |
precededc( |
569 |
input, |
|
570 |
|i| take_while(|c| c == b'\n')(i), |
|
14775 | 571 |
|i| { |
572 |
terminatedc( |
|
573 |
i, |
|
574 |
alt(( |
|
575 |
no_arg_message, |
|
576 |
single_arg_message, |
|
577 |
cmd_message, |
|
578 |
config_message, |
|
14783 | 579 |
server_var_message, |
14775 | 580 |
complex_message, |
581 |
)), |
|
582 |
end_of_message, |
|
583 |
) |
|
584 |
}, |
|
14795 | 585 |
) |
14775 | 586 |
} |
12133 | 587 |
|
13796 | 588 |
#[cfg(test)] |
14777 | 589 |
mod test { |
15112 | 590 |
use super::message; |
15116 | 591 |
use crate::{ |
592 |
core::types::GameCfg, |
|
593 |
protocol::{messages::HwProtocolMessage::*, parser::HwProtocolError, test::gen_proto_msg}, |
|
15112 | 594 |
}; |
14777 | 595 |
use proptest::{proptest, proptest_helper}; |
596 |
||
597 |
#[cfg(test)] |
|
598 |
proptest! { |
|
599 |
#[test] |
|
600 |
fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) { |
|
601 |
println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes()); |
|
602 |
assert_eq!(message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone()))) |
|
603 |
} |
|
604 |
} |
|
605 |
||
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
606 |
#[test] |
14777 | 607 |
fn parse_test() { |
608 |
assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping))); |
|
609 |
assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame))); |
|
610 |
assert_eq!( |
|
611 |
message(b"NICK\nit's me\n\n"), |
|
612 |
Ok((&b""[..], Nick("it's me".to_string()))) |
|
613 |
); |
|
614 |
assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51)))); |
|
615 |
assert_eq!( |
|
616 |
message(b"QUIT\nbye-bye\n\n"), |
|
617 |
Ok((&b""[..], Quit(Some("bye-bye".to_string())))) |
|
618 |
); |
|
619 |
assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None)))); |
|
620 |
assert_eq!( |
|
14795 | 621 |
message(b"CMD\nwatch 49471\n\n"), |
622 |
Ok((&b""[..], Watch(49471))) |
|
14777 | 623 |
); |
624 |
assert_eq!( |
|
625 |
message(b"BAN\nme\nbad\n77\n\n"), |
|
626 |
Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77))) |
|
627 |
); |
|
628 |
||
629 |
assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None)))); |
|
630 |
assert_eq!( |
|
631 |
message(b"CMD\nPART _msg_\n\n"), |
|
632 |
Ok((&b""[..], Part(Some("_msg_".to_string())))) |
|
633 |
); |
|
634 |
||
635 |
assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![])))); |
|
636 |
assert_eq!( |
|
637 |
message(b"CMD\nRND A B\n\n"), |
|
638 |
Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")]))) |
|
639 |
); |
|
640 |
||
641 |
assert_eq!( |
|
15116 | 642 |
message(b"CFG\nSCHEME\na\nA\n\n"), |
643 |
Ok(( |
|
644 |
&b""[..], |
|
645 |
Cfg(GameCfg::Scheme("a".to_string(), vec!["A".to_string()])) |
|
646 |
)) |
|
647 |
); |
|
648 |
||
649 |
assert_eq!( |
|
14795 | 650 |
message(b"QUIT\n1\n2\n\n"), |
15075 | 651 |
Err(nom::Err::Error(HwProtocolError::new())) |
14777 | 652 |
); |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
653 |
} |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
654 |
} |