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