15120
|
1 |
use std::{
|
|
2 |
fs::{File, OpenOptions},
|
|
3 |
io::{Error, ErrorKind, Read, Result, Write},
|
15800
|
4 |
sync::{mpsc, Arc},
|
15937
|
5 |
task::Waker,
|
15120
|
6 |
thread,
|
|
7 |
};
|
|
8 |
|
|
9 |
use crate::{
|
|
10 |
handlers::{IoResult, IoTask},
|
|
11 |
server::database::Database,
|
|
12 |
};
|
|
13 |
use log::*;
|
|
14 |
|
|
15 |
pub type RequestId = u32;
|
|
16 |
|
|
17 |
pub struct IoThread {
|
|
18 |
core_tx: mpsc::Sender<(RequestId, IoTask)>,
|
15800
|
19 |
core_rx: mpsc::Receiver<(RequestId, IoResult)>,
|
15120
|
20 |
}
|
|
21 |
|
|
22 |
impl IoThread {
|
15800
|
23 |
pub fn new(waker: Waker) -> Self {
|
15120
|
24 |
let (core_tx, io_rx) = mpsc::channel();
|
15800
|
25 |
let (io_tx, core_rx) = mpsc::channel();
|
15120
|
26 |
|
15937
|
27 |
/*let mut db = Database::new("localhost");
|
15120
|
28 |
|
|
29 |
thread::spawn(move || {
|
|
30 |
while let Ok((request_id, task)) = io_rx.recv() {
|
|
31 |
let response = match task {
|
|
32 |
IoTask::CheckRegistered { nick } => match db.is_registered(&nick) {
|
|
33 |
Ok(is_registered) => IoResult::AccountRegistered(is_registered),
|
|
34 |
Err(e) => {
|
|
35 |
warn!("Unable to check account's existence: {}", e);
|
|
36 |
IoResult::AccountRegistered(false)
|
|
37 |
}
|
|
38 |
},
|
|
39 |
|
|
40 |
IoTask::GetAccount {
|
|
41 |
nick,
|
|
42 |
protocol,
|
|
43 |
password_hash,
|
|
44 |
client_salt,
|
|
45 |
server_salt,
|
|
46 |
} => {
|
|
47 |
match db.get_account(
|
|
48 |
&nick,
|
|
49 |
protocol,
|
|
50 |
&password_hash,
|
|
51 |
&client_salt,
|
|
52 |
&server_salt,
|
|
53 |
) {
|
|
54 |
Ok(account) => IoResult::Account(account),
|
|
55 |
Err(e) => {
|
|
56 |
warn!("Unable to get account data: {}", e);
|
|
57 |
IoResult::Account(None)
|
|
58 |
}
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|
15532
|
62 |
IoTask::GetCheckerAccount { nick, password } => {
|
|
63 |
match db.get_checker_account(&nick, &password) {
|
|
64 |
Ok(is_registered) => IoResult::CheckerAccount { is_registered },
|
|
65 |
Err(e) => {
|
|
66 |
warn!("Unable to get checker account data: {}", e);
|
|
67 |
IoResult::CheckerAccount {
|
|
68 |
is_registered: false,
|
|
69 |
}
|
|
70 |
}
|
|
71 |
}
|
|
72 |
}
|
|
73 |
|
15120
|
74 |
IoTask::GetReplay { id } => {
|
|
75 |
let result = match db.get_replay_name(id) {
|
|
76 |
Ok(Some(filename)) => {
|
|
77 |
let filename = format!(
|
|
78 |
"checked/{}",
|
|
79 |
if filename.starts_with("replays/") {
|
|
80 |
&filename[8..]
|
|
81 |
} else {
|
|
82 |
&filename
|
|
83 |
}
|
|
84 |
);
|
15796
|
85 |
|
|
86 |
match crate::core::types::Replay::load(&filename) {
|
|
87 |
Ok(replay) => Some(replay),
|
15120
|
88 |
Err(e) => {
|
|
89 |
warn!(
|
15797
|
90 |
"Error while reading replay file \"{}\": {}",
|
15120
|
91 |
filename, e
|
|
92 |
);
|
|
93 |
None
|
|
94 |
}
|
|
95 |
}
|
|
96 |
}
|
|
97 |
Ok(None) => None,
|
|
98 |
Err(e) => {
|
|
99 |
warn!("Unable to get replay name: {}", e);
|
|
100 |
None
|
|
101 |
}
|
|
102 |
};
|
|
103 |
IoResult::Replay(result)
|
|
104 |
}
|
|
105 |
|
|
106 |
IoTask::SaveRoom {
|
|
107 |
room_id,
|
|
108 |
filename,
|
|
109 |
contents,
|
|
110 |
} => {
|
|
111 |
let result = match save_file(&filename, &contents) {
|
|
112 |
Ok(()) => true,
|
|
113 |
Err(e) => {
|
|
114 |
warn!(
|
|
115 |
"Error while writing the room config file \"{}\": {}",
|
|
116 |
filename, e
|
|
117 |
);
|
|
118 |
false
|
|
119 |
}
|
|
120 |
};
|
|
121 |
IoResult::SaveRoom(room_id, result)
|
|
122 |
}
|
|
123 |
|
|
124 |
IoTask::LoadRoom { room_id, filename } => {
|
|
125 |
let result = match load_file(&filename) {
|
|
126 |
Ok(contents) => Some(contents),
|
|
127 |
Err(e) => {
|
|
128 |
warn!(
|
|
129 |
"Error while writing the room config file \"{}\": {}",
|
|
130 |
filename, e
|
|
131 |
);
|
|
132 |
None
|
|
133 |
}
|
|
134 |
};
|
|
135 |
IoResult::LoadRoom(room_id, result)
|
|
136 |
}
|
|
137 |
};
|
|
138 |
io_tx.send((request_id, response));
|
15800
|
139 |
waker.wake();
|
15120
|
140 |
}
|
15937
|
141 |
});*/
|
15120
|
142 |
|
|
143 |
Self { core_rx, core_tx }
|
|
144 |
}
|
|
145 |
|
|
146 |
pub fn send(&self, request_id: RequestId, task: IoTask) {
|
|
147 |
self.core_tx.send((request_id, task)).unwrap();
|
|
148 |
}
|
|
149 |
|
|
150 |
pub fn try_recv(&self) -> Option<(RequestId, IoResult)> {
|
|
151 |
match self.core_rx.try_recv() {
|
|
152 |
Ok(result) => Some(result),
|
|
153 |
Err(mpsc::TryRecvError::Empty) => None,
|
|
154 |
Err(mpsc::TryRecvError::Disconnected) => unreachable!(),
|
|
155 |
}
|
|
156 |
}
|
|
157 |
}
|
|
158 |
|
|
159 |
fn save_file(filename: &str, contents: &str) -> Result<()> {
|
|
160 |
let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
|
|
161 |
writer.write_all(contents.as_bytes())
|
|
162 |
}
|
|
163 |
|
|
164 |
fn load_file(filename: &str) -> Result<String> {
|
|
165 |
let mut reader = File::open(filename)?;
|
|
166 |
let mut result = String::new();
|
|
167 |
reader.read_to_string(&mut result)?;
|
|
168 |
Ok(result)
|
|
169 |
}
|