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