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