author | alfadur <mail@none> |
Sat, 02 Feb 2019 15:06:39 +0300 | |
changeset 14671 | 455865ccd36c |
parent 14457 | 98ef2913ec73 |
child 14672 | 6e6632068a33 |
permissions | -rw-r--r-- |
12149 | 1 |
use mio; |
13521 | 2 |
use std::{io, io::Write}; |
12149 | 3 |
|
13521 | 4 |
use super::{ |
14457 | 5 |
actions::{Action, Action::*}, |
14375 | 6 |
core::HWServer, |
14457 | 7 |
coretypes::ClientId, |
13521 | 8 |
}; |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
9 |
use crate::{ |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
10 |
protocol::messages::{HWProtocolMessage, HWServerMessage::*}, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
11 |
server::actions::PendingMessage, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
12 |
}; |
13805 | 13 |
use log::*; |
13666 | 14 |
|
14457 | 15 |
mod checker; |
16 |
mod common; |
|
12149 | 17 |
mod inroom; |
14457 | 18 |
mod lobby; |
19 |
mod loggingin; |
|
12149 | 20 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
21 |
pub struct Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
22 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
23 |
messages: Vec<PendingMessage>, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
24 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
25 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
26 |
impl Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
27 |
pub fn new(client_id: ClientId) -> Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
28 |
Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
29 |
client_id, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
30 |
messages: vec![], |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
31 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
32 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
33 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
34 |
pub fn client_id(&self) -> ClientId { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
35 |
self.client_id |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
36 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
37 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
38 |
pub fn add(&mut self, message: PendingMessage) { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
39 |
self.messages.push(message) |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
40 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
41 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
42 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
43 |
pub fn handle( |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
44 |
server: &mut HWServer, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
45 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
46 |
response: &mut Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
47 |
message: HWProtocolMessage, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
48 |
) { |
12149 | 49 |
match message { |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
50 |
HWProtocolMessage::Ping => { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
51 |
response.add(Pong.send_self()); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
52 |
server.react(client_id, vec![Pong.send_self().action()]) |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
53 |
} |
14457 | 54 |
HWProtocolMessage::Quit(Some(msg)) => { |
55 |
server.react(client_id, vec![ByeClient("User quit: ".to_string() + &msg)]) |
|
56 |
} |
|
57 |
HWProtocolMessage::Quit(None) => { |
|
58 |
server.react(client_id, vec![ByeClient("User quit".to_string())]) |
|
59 |
} |
|
12149 | 60 |
HWProtocolMessage::Malformed => warn!("Malformed/unknown message"), |
61 |
HWProtocolMessage::Empty => warn!("Empty message"), |
|
14457 | 62 |
_ => match server.clients[client_id].room_id { |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
63 |
None => loggingin::handle(server, client_id, response, message), |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
64 |
Some(id) if id == server.lobby_id => { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
65 |
lobby::handle(server, client_id, response, message) |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
66 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
67 |
Some(id) => inroom::handle(server, client_id, response, id, message), |
12149 | 68 |
}, |
69 |
} |
|
70 |
} |