author | alfadur |
Mon, 03 Jun 2019 01:15:57 +0300 | |
changeset 15103 | 823052e66611 |
parent 15098 | 9397e07b3086 |
child 15120 | febccab419b1 |
permissions | -rw-r--r-- |
14392 | 1 |
use std::{ |
2 |
fs::{File, OpenOptions}, |
|
14457 | 3 |
io::{Error, ErrorKind, Read, Result, Write}, |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
4 |
sync::mpsc, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
5 |
thread, |
14392 | 6 |
}; |
7 |
||
15074 | 8 |
use crate::{ |
15075 | 9 |
handlers::{IoResult, IoTask}, |
15074 | 10 |
server::database::Database, |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
11 |
}; |
14781 | 12 |
use log::*; |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
13 |
use mio::{Evented, Poll, PollOpt}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
14 |
use mio_extras::channel; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
15 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
16 |
pub type RequestId = u32; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
17 |
|
15098 | 18 |
pub struct IoThread { |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
19 |
core_tx: mpsc::Sender<(RequestId, IoTask)>, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
20 |
core_rx: channel::Receiver<(RequestId, IoResult)>, |
14392 | 21 |
} |
22 |
||
15098 | 23 |
impl IoThread { |
14392 | 24 |
pub fn new() -> Self { |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
25 |
let (core_tx, io_rx) = mpsc::channel(); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
26 |
let (io_tx, core_rx) = channel::channel(); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
27 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
28 |
let mut db = Database::new(); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
29 |
db.connect("localhost"); |
14392 | 30 |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
31 |
thread::spawn(move || { |
15098 | 32 |
while let Ok((request_id, task)) = io_rx.recv() { |
14780 | 33 |
let response = match task { |
15103
823052e66611
check for account existence before asking passwords
alfadur
parents:
15098
diff
changeset
|
34 |
IoTask::CheckRegistered { nick } => match db.is_registered(&nick) { |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15098
diff
changeset
|
35 |
Ok(is_registered) => IoResult::AccountRegistered(is_registered), |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15098
diff
changeset
|
36 |
Err(e) => { |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15098
diff
changeset
|
37 |
warn!("Unable to check account's existence: {}", e); |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15098
diff
changeset
|
38 |
IoResult::AccountRegistered(false) |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15098
diff
changeset
|
39 |
} |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15098
diff
changeset
|
40 |
}, |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15098
diff
changeset
|
41 |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
42 |
IoTask::GetAccount { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
43 |
nick, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
44 |
protocol, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
45 |
password_hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
46 |
client_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
47 |
server_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
48 |
} => { |
14780 | 49 |
match db.get_account( |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
50 |
&nick, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
51 |
protocol, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
52 |
&password_hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
53 |
&client_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
54 |
&server_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
55 |
) { |
14781 | 56 |
Ok(account) => IoResult::Account(account), |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
57 |
Err(e) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
58 |
warn!("Unable to get account data: {}", e); |
14780 | 59 |
IoResult::Account(None) |
60 |
} |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
61 |
} |
14781 | 62 |
} |
14780 | 63 |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
64 |
IoTask::GetReplay { id } => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
65 |
let result = match db.get_replay_name(id) { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
66 |
Ok(Some(filename)) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
67 |
let filename = format!( |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
68 |
"checked/{}", |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
69 |
if filename.starts_with("replays/") { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
70 |
&filename[8..] |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
71 |
} else { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
72 |
&filename |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
73 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
74 |
); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
75 |
match load_file(&filename) { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
76 |
Ok(contents) => Some(unimplemented!()), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
77 |
Err(e) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
78 |
warn!( |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
79 |
"Error while writing the room config file \"{}\": {}", |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
80 |
filename, e |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
81 |
); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
82 |
None |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
83 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
84 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
85 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
86 |
Ok(None) => None, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
87 |
Err(e) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
88 |
warn!("Unable to get replay name: {}", e); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
89 |
None |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
90 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
91 |
}; |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
92 |
IoResult::Replay(result) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
93 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
94 |
|
14781 | 95 |
IoTask::SaveRoom { |
96 |
room_id, |
|
97 |
filename, |
|
98 |
contents, |
|
99 |
} => { |
|
14780 | 100 |
let result = match save_file(&filename, &contents) { |
101 |
Ok(()) => true, |
|
102 |
Err(e) => { |
|
103 |
warn!( |
|
104 |
"Error while writing the room config file \"{}\": {}", |
|
105 |
filename, e |
|
106 |
); |
|
107 |
false |
|
14781 | 108 |
} |
14780 | 109 |
}; |
110 |
IoResult::SaveRoom(room_id, result) |
|
14781 | 111 |
} |
14780 | 112 |
|
14781 | 113 |
IoTask::LoadRoom { room_id, filename } => { |
14780 | 114 |
let result = match load_file(&filename) { |
115 |
Ok(contents) => Some(contents), |
|
116 |
Err(e) => { |
|
117 |
warn!( |
|
118 |
"Error while writing the room config file \"{}\": {}", |
|
119 |
filename, e |
|
120 |
); |
|
121 |
None |
|
122 |
} |
|
123 |
}; |
|
124 |
IoResult::LoadRoom(room_id, result) |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
125 |
} |
14780 | 126 |
}; |
127 |
io_tx.send((request_id, response)); |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
128 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
129 |
}); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
130 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
131 |
Self { core_rx, core_tx } |
14392 | 132 |
} |
133 |
||
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
134 |
pub fn send(&self, request_id: RequestId, task: IoTask) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
135 |
self.core_tx.send((request_id, task)).unwrap(); |
14392 | 136 |
} |
137 |
||
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
138 |
pub fn try_recv(&self) -> Option<(RequestId, IoResult)> { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
139 |
match self.core_rx.try_recv() { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
140 |
Ok(result) => Some(result), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
141 |
Err(mpsc::TryRecvError::Empty) => None, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
142 |
Err(mpsc::TryRecvError::Disconnected) => unreachable!(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
143 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
144 |
} |
14392 | 145 |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
146 |
pub fn register_rx(&self, poll: &mio::Poll, token: mio::Token) -> Result<()> { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
147 |
self.core_rx |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
148 |
.register(poll, token, mio::Ready::readable(), PollOpt::edge()) |
14392 | 149 |
} |
150 |
} |
|
14780 | 151 |
|
152 |
fn save_file(filename: &str, contents: &str) -> Result<()> { |
|
153 |
let mut writer = OpenOptions::new().create(true).write(true).open(filename)?; |
|
154 |
writer.write_all(contents.as_bytes()) |
|
155 |
} |
|
156 |
||
157 |
fn load_file(filename: &str) -> Result<String> { |
|
158 |
let mut reader = File::open(filename)?; |
|
159 |
let mut result = String::new(); |
|
160 |
reader.read_to_string(&mut result)?; |
|
161 |
Ok(result) |
|
14781 | 162 |
} |