author | alfadur |
Thu, 07 Feb 2019 22:26:56 +0300 | |
changeset 14696 | 8a45c90f4580 |
parent 14694 | 25c564f77b7d |
child 14779 | f43ab2bd76ae |
permissions | -rw-r--r-- |
12149 | 1 |
use mio; |
13521 | 2 |
use std::{io, io::Write}; |
12149 | 3 |
|
14694 | 4 |
use super::{ |
5 |
actions::{Destination, DestinationRoom}, |
|
6 |
core::HWServer, |
|
7 |
coretypes::ClientId, |
|
8 |
}; |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
9 |
use crate::{ |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
10 |
protocol::messages::{HWProtocolMessage, HWServerMessage, HWServerMessage::*}, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
11 |
server::actions::PendingMessage, |
14693 | 12 |
utils, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
13 |
}; |
14693 | 14 |
use base64::encode; |
13805 | 15 |
use log::*; |
14693 | 16 |
use rand::{thread_rng, RngCore}; |
13666 | 17 |
|
14457 | 18 |
mod checker; |
19 |
mod common; |
|
12149 | 20 |
mod inroom; |
14457 | 21 |
mod lobby; |
22 |
mod loggingin; |
|
12149 | 23 |
|
14693 | 24 |
use self::loggingin::LoginResult; |
25 |
||
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
26 |
pub struct Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
27 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
28 |
messages: Vec<PendingMessage>, |
14696 | 29 |
removed_clients: Vec<ClientId>, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
30 |
} |
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 |
impl Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
33 |
pub fn new(client_id: ClientId) -> Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
34 |
Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
35 |
client_id, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
36 |
messages: vec![], |
14696 | 37 |
removed_clients: vec![], |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
38 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
39 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
40 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
41 |
#[inline] |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
42 |
pub fn is_empty(&self) -> bool { |
14696 | 43 |
self.messages.is_empty() && self.removed_clients.is_empty() |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
44 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
45 |
|
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
46 |
#[inline] |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
47 |
pub fn len(&self) -> usize { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
48 |
self.messages.len() |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
49 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
50 |
|
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
51 |
#[inline] |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
52 |
pub fn client_id(&self) -> ClientId { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
53 |
self.client_id |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
54 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
55 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
56 |
#[inline] |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
57 |
pub fn add(&mut self, message: PendingMessage) { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
58 |
self.messages.push(message) |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
59 |
} |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
60 |
|
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
61 |
pub fn extract_messages<'a, 'b: 'a>( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
62 |
&'b mut self, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
63 |
server: &'a HWServer, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
64 |
) -> impl Iterator<Item = (Vec<ClientId>, HWServerMessage)> + 'a { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
65 |
let client_id = self.client_id; |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
66 |
self.messages.drain(..).map(move |m| { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
67 |
let ids = get_recipients(server, client_id, &m.destination); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
68 |
(ids, m.message) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
69 |
}) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
70 |
} |
14696 | 71 |
|
72 |
pub fn remove_client(&mut self, client_id: ClientId) { |
|
73 |
self.removed_clients.push(client_id); |
|
74 |
} |
|
75 |
||
76 |
pub fn extract_removed_clients(&mut self) -> impl Iterator<Item = ClientId> + '_ { |
|
77 |
self.removed_clients.drain(..) |
|
78 |
} |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
79 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
80 |
|
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
81 |
impl Extend<PendingMessage> for Response { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
82 |
fn extend<T: IntoIterator<Item = PendingMessage>>(&mut self, iter: T) { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
83 |
for msg in iter { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
84 |
self.add(msg) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
85 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
86 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
87 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
88 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
89 |
fn get_recipients( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
90 |
server: &HWServer, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
91 |
client_id: ClientId, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
92 |
destination: &Destination, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
93 |
) -> Vec<ClientId> { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
94 |
let mut ids = match *destination { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
95 |
Destination::ToSelf => vec![client_id], |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
96 |
Destination::ToId(id) => vec![id], |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
97 |
Destination::ToAll { |
14694 | 98 |
room_id: DestinationRoom::Lobby, |
99 |
.. |
|
100 |
} => server.lobby_clients(), |
|
101 |
Destination::ToAll { |
|
102 |
room_id: DestinationRoom::Room(id), |
|
103 |
.. |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
104 |
} => server.room_clients(id), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
105 |
Destination::ToAll { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
106 |
protocol: Some(proto), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
107 |
.. |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
108 |
} => server.protocol_clients(proto), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
109 |
Destination::ToAll { .. } => server.clients.iter().map(|(id, _)| id).collect::<Vec<_>>(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
110 |
}; |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
111 |
if let Destination::ToAll { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
112 |
skip_self: true, .. |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
113 |
} = destination |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
114 |
{ |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
115 |
if let Some(index) = ids.iter().position(|id| *id == client_id) { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
116 |
ids.remove(index); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
117 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
118 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
119 |
ids |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
120 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
121 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
122 |
pub fn handle( |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
123 |
server: &mut HWServer, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
124 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
125 |
response: &mut Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
126 |
message: HWProtocolMessage, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
127 |
) { |
12149 | 128 |
match message { |
14693 | 129 |
HWProtocolMessage::Ping => response.add(Pong.send_self()), |
12149 | 130 |
HWProtocolMessage::Malformed => warn!("Malformed/unknown message"), |
131 |
HWProtocolMessage::Empty => warn!("Empty message"), |
|
14693 | 132 |
_ => { |
133 |
if server.anteroom.clients.contains(client_id) { |
|
134 |
match loggingin::handle(&mut server.anteroom, client_id, response, message) { |
|
135 |
LoginResult::Unchanged => (), |
|
136 |
LoginResult::Complete => { |
|
137 |
if let Some(client) = server.anteroom.remove_client(client_id) { |
|
138 |
server.add_client(client_id, client); |
|
139 |
} |
|
140 |
} |
|
141 |
LoginResult::Exit => { |
|
142 |
server.anteroom.remove_client(client_id); |
|
14696 | 143 |
response.remove_client(client_id); |
14693 | 144 |
} |
145 |
} |
|
146 |
} else { |
|
147 |
match message { |
|
148 |
HWProtocolMessage::Quit(Some(msg)) => { |
|
149 |
common::remove_client(server, response, "User quit: ".to_string() + &msg); |
|
150 |
} |
|
151 |
HWProtocolMessage::Quit(None) => { |
|
152 |
common::remove_client(server, response, "User quit".to_string()); |
|
153 |
} |
|
154 |
_ => match server.clients[client_id].room_id { |
|
155 |
None => lobby::handle(server, client_id, response, message), |
|
156 |
Some(room_id) => { |
|
157 |
inroom::handle(server, client_id, response, room_id, message) |
|
158 |
} |
|
159 |
}, |
|
160 |
} |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
161 |
} |
14693 | 162 |
} |
12149 | 163 |
} |
164 |
} |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
165 |
|
14693 | 166 |
pub fn handle_client_accept(server: &mut HWServer, client_id: ClientId, response: &mut Response) { |
167 |
let mut salt = [0u8; 18]; |
|
168 |
thread_rng().fill_bytes(&mut salt); |
|
169 |
||
170 |
server.anteroom.add_client(client_id, encode(&salt)); |
|
171 |
||
172 |
response.add(HWServerMessage::Connected(utils::PROTOCOL_VERSION).send_self()); |
|
173 |
} |
|
174 |
||
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
175 |
pub fn handle_client_loss(server: &mut HWServer, client_id: ClientId, response: &mut Response) { |
14696 | 176 |
if server.anteroom.remove_client(client_id).is_none() { |
177 |
common::remove_client(server, response, "Connection reset".to_string()); |
|
178 |
} |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
179 |
} |