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