author | alfadur |
Wed, 10 Apr 2019 23:56:53 +0300 | |
changeset 14785 | a1077e8d26f4 |
parent 14784 | 8390d5e4e39c |
child 14786 | 8ecdb5c6bb2a |
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, |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
7 |
coretypes::{ClientId, Replay, RoomId}, |
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::{ |
14781 | 11 |
protocol::messages::{server_chat, 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; |
14784 | 26 |
use crate::protocol::messages::global_chat; |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
27 |
use crate::protocol::messages::HWProtocolMessage::EngineMessage; |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
28 |
use crate::server::coretypes::{GameCfg, TeamInfo}; |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
29 |
use std::fmt::{Formatter, LowerHex}; |
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 |
#[derive(PartialEq)] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
32 |
pub struct Sha1Digest([u8; 20]); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
33 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
34 |
impl Sha1Digest { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
35 |
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
|
36 |
Self(digest) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
37 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
38 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
39 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
40 |
impl LowerHex for Sha1Digest { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
41 |
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
|
42 |
for byte in &self.0 { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
43 |
write!(f, "{:02x}", byte)?; |
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 |
Ok(()) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
46 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
47 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
48 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
49 |
pub struct AccountInfo { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
50 |
pub is_registered: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
51 |
pub is_admin: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
52 |
pub is_contributor: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
53 |
pub server_hash: Sha1Digest, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
54 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
55 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
56 |
pub enum IoTask { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
57 |
GetAccount { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
58 |
nick: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
59 |
protocol: u16, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
60 |
password_hash: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
61 |
client_salt: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
62 |
server_salt: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
63 |
}, |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
64 |
GetReplay { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
65 |
id: u32, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
66 |
}, |
14780 | 67 |
SaveRoom { |
68 |
room_id: RoomId, |
|
69 |
filename: String, |
|
70 |
contents: String, |
|
71 |
}, |
|
72 |
LoadRoom { |
|
73 |
room_id: RoomId, |
|
14781 | 74 |
filename: String, |
75 |
}, |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
76 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
77 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
78 |
pub enum IoResult { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
79 |
Account(Option<AccountInfo>), |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
80 |
Replay(Option<Replay>), |
14780 | 81 |
SaveRoom(RoomId, bool), |
14781 | 82 |
LoadRoom(RoomId, Option<String>), |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
83 |
} |
14693 | 84 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
85 |
pub struct Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
86 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
87 |
messages: Vec<PendingMessage>, |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
88 |
io_tasks: Vec<IoTask>, |
14696 | 89 |
removed_clients: Vec<ClientId>, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
90 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
91 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
92 |
impl Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
93 |
pub fn new(client_id: ClientId) -> Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
94 |
Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
95 |
client_id, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
96 |
messages: vec![], |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
97 |
io_tasks: vec![], |
14696 | 98 |
removed_clients: vec![], |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
99 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
100 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
101 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
102 |
#[inline] |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
103 |
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
|
104 |
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
|
105 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
106 |
|
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
107 |
#[inline] |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
108 |
pub fn len(&self) -> usize { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
109 |
self.messages.len() |
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 |
|
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
112 |
#[inline] |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
113 |
pub fn client_id(&self) -> ClientId { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
114 |
self.client_id |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
115 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
116 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
117 |
#[inline] |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
118 |
pub fn add(&mut self, message: PendingMessage) { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
119 |
self.messages.push(message) |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
120 |
} |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
121 |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
122 |
#[inline] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
123 |
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
|
124 |
self.io_tasks.push(task) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
125 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
126 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
127 |
pub fn extract_messages<'a, 'b: 'a>( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
128 |
&'b mut self, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
129 |
server: &'a HWServer, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
130 |
) -> impl Iterator<Item = (Vec<ClientId>, HWServerMessage)> + 'a { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
131 |
let client_id = self.client_id; |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
132 |
self.messages.drain(..).map(move |m| { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
133 |
let ids = get_recipients(server, client_id, &m.destination); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
134 |
(ids, m.message) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
135 |
}) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
136 |
} |
14696 | 137 |
|
138 |
pub fn remove_client(&mut self, client_id: ClientId) { |
|
139 |
self.removed_clients.push(client_id); |
|
140 |
} |
|
141 |
||
142 |
pub fn extract_removed_clients(&mut self) -> impl Iterator<Item = ClientId> + '_ { |
|
143 |
self.removed_clients.drain(..) |
|
144 |
} |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
145 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
146 |
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
|
147 |
self.io_tasks.drain(..) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
148 |
} |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
149 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
150 |
|
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
151 |
impl Extend<PendingMessage> for Response { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
152 |
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
|
153 |
for msg in iter { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
154 |
self.add(msg) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
155 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
156 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
157 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
158 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
159 |
fn get_recipients( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
160 |
server: &HWServer, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
161 |
client_id: ClientId, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
162 |
destination: &Destination, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
163 |
) -> Vec<ClientId> { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
164 |
let mut ids = match *destination { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
165 |
Destination::ToSelf => vec![client_id], |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
166 |
Destination::ToId(id) => vec![id], |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
167 |
Destination::ToAll { |
14694 | 168 |
room_id: DestinationRoom::Lobby, |
169 |
.. |
|
14781 | 170 |
} => server.collect_lobby_clients(), |
14694 | 171 |
Destination::ToAll { |
172 |
room_id: DestinationRoom::Room(id), |
|
173 |
.. |
|
14781 | 174 |
} => server.collect_room_clients(id), |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
175 |
Destination::ToAll { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
176 |
protocol: Some(proto), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
177 |
.. |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
178 |
} => server.protocol_clients(proto), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
179 |
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
|
180 |
}; |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
181 |
if let Destination::ToAll { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
182 |
skip_self: true, .. |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
183 |
} = destination |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
184 |
{ |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
185 |
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
|
186 |
ids.remove(index); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
187 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
188 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
189 |
ids |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
190 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
191 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
192 |
pub fn handle( |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
193 |
server: &mut HWServer, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
194 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
195 |
response: &mut Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
196 |
message: HWProtocolMessage, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
197 |
) { |
12149 | 198 |
match message { |
14693 | 199 |
HWProtocolMessage::Ping => response.add(Pong.send_self()), |
12149 | 200 |
HWProtocolMessage::Malformed => warn!("Malformed/unknown message"), |
201 |
HWProtocolMessage::Empty => warn!("Empty message"), |
|
14693 | 202 |
_ => { |
203 |
if server.anteroom.clients.contains(client_id) { |
|
14781 | 204 |
match loggingin::handle(server, client_id, response, message) { |
14693 | 205 |
LoginResult::Unchanged => (), |
206 |
LoginResult::Complete => { |
|
207 |
if let Some(client) = server.anteroom.remove_client(client_id) { |
|
208 |
server.add_client(client_id, client); |
|
14781 | 209 |
common::join_lobby(server, response); |
14693 | 210 |
} |
211 |
} |
|
212 |
LoginResult::Exit => { |
|
213 |
server.anteroom.remove_client(client_id); |
|
14696 | 214 |
response.remove_client(client_id); |
14693 | 215 |
} |
216 |
} |
|
217 |
} else { |
|
218 |
match message { |
|
219 |
HWProtocolMessage::Quit(Some(msg)) => { |
|
220 |
common::remove_client(server, response, "User quit: ".to_string() + &msg); |
|
221 |
} |
|
222 |
HWProtocolMessage::Quit(None) => { |
|
223 |
common::remove_client(server, response, "User quit".to_string()); |
|
224 |
} |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
225 |
HWProtocolMessage::Global(msg) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
226 |
if !server.clients[client_id].is_admin() { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
227 |
response.add(Warning("Access denied.".to_string()).send_self()); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
228 |
} else { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
229 |
response.add(global_chat(msg).send_all()) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
230 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
231 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
232 |
HWProtocolMessage::Watch(id) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
233 |
#[cfg(feature = "official-server")] |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
234 |
{ |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
235 |
response.request_io(IoTask::GetReplay { id }) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
236 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
237 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
238 |
#[cfg(not(feature = "official-server"))] |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
239 |
{ |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
240 |
response.add( |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
241 |
Warning("This server does not support replays!".to_string()) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
242 |
.send_self(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
243 |
); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
244 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
245 |
} |
14693 | 246 |
_ => match server.clients[client_id].room_id { |
247 |
None => lobby::handle(server, client_id, response, message), |
|
248 |
Some(room_id) => { |
|
249 |
inroom::handle(server, client_id, response, room_id, message) |
|
250 |
} |
|
251 |
}, |
|
252 |
} |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
253 |
} |
14693 | 254 |
} |
12149 | 255 |
} |
256 |
} |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
257 |
|
14693 | 258 |
pub fn handle_client_accept(server: &mut HWServer, client_id: ClientId, response: &mut Response) { |
259 |
let mut salt = [0u8; 18]; |
|
260 |
thread_rng().fill_bytes(&mut salt); |
|
261 |
||
262 |
server.anteroom.add_client(client_id, encode(&salt)); |
|
263 |
||
14783 | 264 |
response.add(HWServerMessage::Connected(utils::SERVER_VERSION).send_self()); |
14693 | 265 |
} |
266 |
||
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
267 |
pub fn handle_client_loss(server: &mut HWServer, client_id: ClientId, response: &mut Response) { |
14696 | 268 |
if server.anteroom.remove_client(client_id).is_none() { |
269 |
common::remove_client(server, response, "Connection reset".to_string()); |
|
270 |
} |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
271 |
} |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
272 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
273 |
pub fn handle_io_result( |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
274 |
server: &mut HWServer, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
275 |
client_id: ClientId, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
276 |
response: &mut Response, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
277 |
io_result: IoResult, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
278 |
) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
279 |
match io_result { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
280 |
IoResult::Account(Some(info)) => { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
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
|
287 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
288 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
289 |
IoResult::Account(None) => { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
290 |
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
|
291 |
response.remove_client(client_id); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
292 |
} |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
293 |
IoResult::Replay(Some(replay)) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
294 |
response.add(RoomJoined(vec![server.clients[client_id].nick.clone()]).send_self()); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
295 |
common::get_room_config_impl(&replay.config, client_id, response); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
296 |
common::get_teams(replay.teams.iter(), client_id, response); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
297 |
response.add(RunGame.send_self()); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
298 |
response.add(ForwardEngineMessage(replay.message_log).send_self()); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
299 |
response.add(Kicked.send_self()); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
300 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
301 |
IoResult::Replay(None) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
302 |
response.add(Warning("Could't load the replay".to_string()).send_self()) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
303 |
} |
14780 | 304 |
IoResult::SaveRoom(_, true) => { |
305 |
response.add(server_chat("Room configs saved successfully.".to_string()).send_self()); |
|
306 |
} |
|
307 |
IoResult::SaveRoom(_, false) => { |
|
14781 | 308 |
response.add(Warning("Unable to save the room configs.".to_string()).send_self()); |
14780 | 309 |
} |
310 |
IoResult::LoadRoom(room_id, Some(contents)) => { |
|
311 |
if let Some(ref mut room) = server.rooms.get_mut(room_id) { |
|
312 |
match room.set_saves(&contents) { |
|
313 |
Ok(_) => response.add( |
|
14781 | 314 |
server_chat("Room configs loaded successfully.".to_string()).send_self(), |
14780 | 315 |
), |
316 |
Err(e) => { |
|
317 |
warn!("Error while deserializing the room configs: {}", e); |
|
318 |
response.add( |
|
319 |
Warning("Unable to deserialize the room configs.".to_string()) |
|
320 |
.send_self(), |
|
321 |
); |
|
322 |
} |
|
323 |
} |
|
324 |
} |
|
325 |
} |
|
14781 | 326 |
IoResult::LoadRoom(_, None) => { |
327 |
response.add(Warning("Unable to load the room configs.".to_string()).send_self()); |
|
14780 | 328 |
} |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
329 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
330 |
} |