author | alfadur |
Wed, 10 Apr 2019 18:12:30 +0300 | |
changeset 14804 | b3adc030104b |
parent 14802 | 01f8ab45f806 |
child 14805 | 8390d5e4e39c |
permissions | -rw-r--r-- |
12149 | 1 |
use mio; |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
2 |
use std::{collections::HashMap, io, io::Write}; |
12149 | 3 |
|
14715 | 4 |
use super::{ |
5 |
actions::{Destination, DestinationRoom}, |
|
6 |
core::HWServer, |
|
14801 | 7 |
coretypes::{ClientId, RoomId}, |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
8 |
room::RoomSave, |
14715 | 9 |
}; |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
10 |
use crate::{ |
14802 | 11 |
protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage, HWServerMessage::*}, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
12 |
server::actions::PendingMessage, |
14714 | 13 |
utils, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
14 |
}; |
14714 | 15 |
use base64::encode; |
13810 | 16 |
use log::*; |
14714 | 17 |
use rand::{thread_rng, RngCore}; |
13666 | 18 |
|
14478 | 19 |
mod checker; |
20 |
mod common; |
|
12149 | 21 |
mod inroom; |
14478 | 22 |
mod lobby; |
23 |
mod loggingin; |
|
12149 | 24 |
|
14714 | 25 |
use self::loggingin::LoginResult; |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
26 |
use std::fmt::{Formatter, LowerHex}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
27 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
28 |
#[derive(PartialEq)] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
29 |
pub struct Sha1Digest([u8; 20]); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
30 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
31 |
impl Sha1Digest { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
32 |
pub fn new(digest: [u8; 20]) -> Self { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
33 |
Self(digest) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
34 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
35 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
36 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
37 |
impl LowerHex for Sha1Digest { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
38 |
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
39 |
for byte in &self.0 { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
40 |
write!(f, "{:02x}", byte)?; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
41 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
42 |
Ok(()) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
43 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
44 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
45 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
46 |
pub struct AccountInfo { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
47 |
pub is_registered: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
48 |
pub is_admin: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
49 |
pub is_contributor: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
50 |
pub server_hash: Sha1Digest, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
51 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
52 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
53 |
pub enum IoTask { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
54 |
GetAccount { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
55 |
nick: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
56 |
protocol: u16, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
57 |
password_hash: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
58 |
client_salt: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
59 |
server_salt: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
60 |
}, |
14801 | 61 |
SaveRoom { |
62 |
room_id: RoomId, |
|
63 |
filename: String, |
|
64 |
contents: String, |
|
65 |
}, |
|
66 |
LoadRoom { |
|
67 |
room_id: RoomId, |
|
14802 | 68 |
filename: String, |
69 |
}, |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
70 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
71 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
72 |
pub enum IoResult { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
73 |
Account(Option<AccountInfo>), |
14801 | 74 |
SaveRoom(RoomId, bool), |
14802 | 75 |
LoadRoom(RoomId, Option<String>), |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
76 |
} |
14714 | 77 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
78 |
pub struct Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
79 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
80 |
messages: Vec<PendingMessage>, |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
81 |
io_tasks: Vec<IoTask>, |
14717 | 82 |
removed_clients: Vec<ClientId>, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
83 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
84 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
85 |
impl Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
86 |
pub fn new(client_id: ClientId) -> Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
87 |
Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
88 |
client_id, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
89 |
messages: vec![], |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
90 |
io_tasks: vec![], |
14717 | 91 |
removed_clients: vec![], |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
92 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
93 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
94 |
|
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
95 |
#[inline] |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
96 |
pub fn is_empty(&self) -> bool { |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
97 |
self.messages.is_empty() && self.removed_clients.is_empty() && self.io_tasks.is_empty() |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
98 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
99 |
|
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
100 |
#[inline] |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
101 |
pub fn len(&self) -> usize { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
102 |
self.messages.len() |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
103 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
104 |
|
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
105 |
#[inline] |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
106 |
pub fn client_id(&self) -> ClientId { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
107 |
self.client_id |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
108 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
109 |
|
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
110 |
#[inline] |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
111 |
pub fn add(&mut self, message: PendingMessage) { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
112 |
self.messages.push(message) |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
113 |
} |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
114 |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
115 |
#[inline] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
116 |
pub fn request_io(&mut self, task: IoTask) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
117 |
self.io_tasks.push(task) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
118 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
119 |
|
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
120 |
pub fn extract_messages<'a, 'b: 'a>( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
121 |
&'b mut self, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
122 |
server: &'a HWServer, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
123 |
) -> impl Iterator<Item = (Vec<ClientId>, HWServerMessage)> + 'a { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
124 |
let client_id = self.client_id; |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
125 |
self.messages.drain(..).map(move |m| { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
126 |
let ids = get_recipients(server, client_id, &m.destination); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
127 |
(ids, m.message) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
128 |
}) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
129 |
} |
14717 | 130 |
|
131 |
pub fn remove_client(&mut self, client_id: ClientId) { |
|
132 |
self.removed_clients.push(client_id); |
|
133 |
} |
|
134 |
||
135 |
pub fn extract_removed_clients(&mut self) -> impl Iterator<Item = ClientId> + '_ { |
|
136 |
self.removed_clients.drain(..) |
|
137 |
} |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
138 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
139 |
pub fn extract_io_tasks(&mut self) -> impl Iterator<Item = IoTask> + '_ { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
140 |
self.io_tasks.drain(..) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
141 |
} |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
142 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
143 |
|
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
144 |
impl Extend<PendingMessage> for Response { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
145 |
fn extend<T: IntoIterator<Item = PendingMessage>>(&mut self, iter: T) { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
146 |
for msg in iter { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
147 |
self.add(msg) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
148 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
149 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
150 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
151 |
|
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
152 |
fn get_recipients( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
153 |
server: &HWServer, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
154 |
client_id: ClientId, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
155 |
destination: &Destination, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
156 |
) -> Vec<ClientId> { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
157 |
let mut ids = match *destination { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
158 |
Destination::ToSelf => vec![client_id], |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
159 |
Destination::ToId(id) => vec![id], |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
160 |
Destination::ToAll { |
14715 | 161 |
room_id: DestinationRoom::Lobby, |
162 |
.. |
|
14802 | 163 |
} => server.collect_lobby_clients(), |
14715 | 164 |
Destination::ToAll { |
165 |
room_id: DestinationRoom::Room(id), |
|
166 |
.. |
|
14802 | 167 |
} => server.collect_room_clients(id), |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
168 |
Destination::ToAll { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
169 |
protocol: Some(proto), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
170 |
.. |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
171 |
} => server.protocol_clients(proto), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
172 |
Destination::ToAll { .. } => server.clients.iter().map(|(id, _)| id).collect::<Vec<_>>(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
173 |
}; |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
174 |
if let Destination::ToAll { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
175 |
skip_self: true, .. |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
176 |
} = destination |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
177 |
{ |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
178 |
if let Some(index) = ids.iter().position(|id| *id == client_id) { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
179 |
ids.remove(index); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
180 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
181 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
182 |
ids |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
183 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
184 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
185 |
pub fn handle( |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
186 |
server: &mut HWServer, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
187 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
188 |
response: &mut Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
189 |
message: HWProtocolMessage, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
190 |
) { |
12149 | 191 |
match message { |
14714 | 192 |
HWProtocolMessage::Ping => response.add(Pong.send_self()), |
12149 | 193 |
HWProtocolMessage::Malformed => warn!("Malformed/unknown message"), |
194 |
HWProtocolMessage::Empty => warn!("Empty message"), |
|
14714 | 195 |
_ => { |
196 |
if server.anteroom.clients.contains(client_id) { |
|
14802 | 197 |
match loggingin::handle(server, client_id, response, message) { |
14714 | 198 |
LoginResult::Unchanged => (), |
199 |
LoginResult::Complete => { |
|
200 |
if let Some(client) = server.anteroom.remove_client(client_id) { |
|
201 |
server.add_client(client_id, client); |
|
14802 | 202 |
common::join_lobby(server, response); |
14714 | 203 |
} |
204 |
} |
|
205 |
LoginResult::Exit => { |
|
206 |
server.anteroom.remove_client(client_id); |
|
14717 | 207 |
response.remove_client(client_id); |
14714 | 208 |
} |
209 |
} |
|
210 |
} else { |
|
211 |
match message { |
|
212 |
HWProtocolMessage::Quit(Some(msg)) => { |
|
213 |
common::remove_client(server, response, "User quit: ".to_string() + &msg); |
|
214 |
} |
|
215 |
HWProtocolMessage::Quit(None) => { |
|
216 |
common::remove_client(server, response, "User quit".to_string()); |
|
217 |
} |
|
218 |
_ => match server.clients[client_id].room_id { |
|
219 |
None => lobby::handle(server, client_id, response, message), |
|
220 |
Some(room_id) => { |
|
221 |
inroom::handle(server, client_id, response, room_id, message) |
|
222 |
} |
|
223 |
}, |
|
224 |
} |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
225 |
} |
14714 | 226 |
} |
12149 | 227 |
} |
228 |
} |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
229 |
|
14714 | 230 |
pub fn handle_client_accept(server: &mut HWServer, client_id: ClientId, response: &mut Response) { |
231 |
let mut salt = [0u8; 18]; |
|
232 |
thread_rng().fill_bytes(&mut salt); |
|
233 |
||
234 |
server.anteroom.add_client(client_id, encode(&salt)); |
|
235 |
||
14804 | 236 |
response.add(HWServerMessage::Connected(utils::SERVER_VERSION).send_self()); |
14714 | 237 |
} |
238 |
||
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
239 |
pub fn handle_client_loss(server: &mut HWServer, client_id: ClientId, response: &mut Response) { |
14717 | 240 |
if server.anteroom.remove_client(client_id).is_none() { |
241 |
common::remove_client(server, response, "Connection reset".to_string()); |
|
242 |
} |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
243 |
} |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
244 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
245 |
pub fn handle_io_result( |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
246 |
server: &mut HWServer, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
247 |
client_id: ClientId, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
248 |
response: &mut Response, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
249 |
io_result: IoResult, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
250 |
) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
251 |
match io_result { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
252 |
IoResult::Account(Some(info)) => { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
253 |
response.add(ServerAuth(format!("{:x}", info.server_hash)).send_self()); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
254 |
if let Some(client) = server.anteroom.remove_client(client_id) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
255 |
server.add_client(client_id, client); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
256 |
let client = &mut server.clients[client_id]; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
257 |
client.set_is_admin(info.is_admin); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
258 |
client.set_is_contributor(info.is_admin) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
259 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
260 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
261 |
IoResult::Account(None) => { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
262 |
response.add(Error("Authentication failed.".to_string()).send_self()); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
263 |
response.remove_client(client_id); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
264 |
} |
14801 | 265 |
IoResult::SaveRoom(_, true) => { |
266 |
response.add(server_chat("Room configs saved successfully.".to_string()).send_self()); |
|
267 |
} |
|
268 |
IoResult::SaveRoom(_, false) => { |
|
14802 | 269 |
response.add(Warning("Unable to save the room configs.".to_string()).send_self()); |
14801 | 270 |
} |
271 |
IoResult::LoadRoom(room_id, Some(contents)) => { |
|
272 |
if let Some(ref mut room) = server.rooms.get_mut(room_id) { |
|
273 |
match room.set_saves(&contents) { |
|
274 |
Ok(_) => response.add( |
|
14802 | 275 |
server_chat("Room configs loaded successfully.".to_string()).send_self(), |
14801 | 276 |
), |
277 |
Err(e) => { |
|
278 |
warn!("Error while deserializing the room configs: {}", e); |
|
279 |
response.add( |
|
280 |
Warning("Unable to deserialize the room configs.".to_string()) |
|
281 |
.send_self(), |
|
282 |
); |
|
283 |
} |
|
284 |
} |
|
285 |
} |
|
286 |
} |
|
14802 | 287 |
IoResult::LoadRoom(_, None) => { |
288 |
response.add(Warning("Unable to load the room configs.".to_string()).send_self()); |
|
14801 | 289 |
} |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
290 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
291 |
} |