author | alfadur |
Mon, 18 Jun 2018 09:22:53 -0400 | |
changeset 13421 | cdf69667593b |
parent 13124 | 1e39b8749072 |
child 13424 | 81e0ed105f5d |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
3 |
use server::server::HWServer; |
|
4 |
use server::actions::Action; |
|
5 |
use server::actions::Action::*; |
|
6 |
use protocol::messages::HWProtocolMessage; |
|
7 |
use protocol::messages::HWServerMessage::*; |
|
13421 | 8 |
use utils::is_name_illegal; |
12152 | 9 |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
10 |
pub fn handle(server: & mut HWServer, token: usize, message: HWProtocolMessage) { |
12152 | 11 |
match message { |
13421 | 12 |
HWProtocolMessage::Nick(nick) => { |
13 |
let actions; |
|
14 |
{ |
|
15 |
let client = &mut server.clients[token]; |
|
16 |
debug!("{} {}", nick, is_name_illegal(&nick)); |
|
17 |
actions = if client.room_id != None { |
|
18 |
unreachable!() |
|
19 |
} |
|
20 |
else if !client.nick.is_empty() { |
|
21 |
vec![ProtocolError("Nickname already provided.".to_string())] |
|
22 |
} |
|
23 |
else if is_name_illegal(&nick) { |
|
24 |
vec![ByeClient("Illegal nickname! Nicknames must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())] |
|
25 |
} |
|
26 |
else { |
|
27 |
client.nick = nick.clone(); |
|
28 |
vec![SendMe(Nick(nick)), CheckRegistered] |
|
29 |
}; |
|
30 |
} |
|
31 |
server.react(token, actions); |
|
32 |
}, |
|
12152 | 33 |
HWProtocolMessage::Proto(proto) => { |
13421 | 34 |
let actions; |
35 |
{ |
|
36 |
let client = &mut server.clients[token]; |
|
37 |
actions = if client.protocol_number != 0 { |
|
38 |
vec![ProtocolError("Protocol already known.".to_string())] |
|
39 |
} |
|
40 |
else if proto == 0 { |
|
41 |
vec![ProtocolError("Bad number.".to_string())] |
|
42 |
} |
|
43 |
else { |
|
44 |
client.protocol_number = proto; |
|
45 |
vec![SendMe(Proto(proto)), CheckRegistered] |
|
46 |
}; |
|
47 |
} |
|
48 |
server.react(token, actions); |
|
12152 | 49 |
}, |
50 |
_ => warn!("Incorrect command in logging-in state"), |
|
51 |
} |
|
52 |
} |