15120
|
1 |
use std::{
|
|
2 |
fs::{File, OpenOptions},
|
|
3 |
io::{Error, ErrorKind, Read, Result, Write},
|
|
4 |
sync::mpsc,
|
|
5 |
thread,
|
|
6 |
};
|
|
7 |
|
|
8 |
use crate::{
|
|
9 |
handlers::{IoResult, IoTask},
|
|
10 |
server::database::Database,
|
|
11 |
};
|
|
12 |
use log::*;
|
|
13 |
use mio::{Evented, Poll, PollOpt};
|
|
14 |
use mio_extras::channel;
|
|
15 |
|
|
16 |
pub type RequestId = u32;
|
|
17 |
|
|
18 |
pub struct IoThread {
|
|
19 |
core_tx: mpsc::Sender<(RequestId, IoTask)>,
|
|
20 |
core_rx: channel::Receiver<(RequestId, IoResult)>,
|
|
21 |
}
|
|
22 |
|
|
23 |
impl IoThread {
|
|
24 |
pub fn new() -> Self {
|
|
25 |
let (core_tx, io_rx) = mpsc::channel();
|
|
26 |
let (io_tx, core_rx) = channel::channel();
|
|
27 |
|
|
28 |
let mut db = Database::new();
|
|
29 |
db.connect("localhost");
|
|
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 |
);
|
|
87 |
match load_file(&filename) {
|
|
88 |
Ok(contents) => Some(unimplemented!()),
|
|
89 |
Err(e) => {
|
|
90 |
warn!(
|
|
91 |
"Error while writing the room config file \"{}\": {}",
|
|
92 |
filename, e
|
|
93 |
);
|
|
94 |
None
|
|
95 |
}
|
|
96 |
}
|
|
97 |
}
|
|
98 |
Ok(None) => None,
|
|
99 |
Err(e) => {
|
|
100 |
warn!("Unable to get replay name: {}", e);
|
|
101 |
None
|
|
102 |
}
|
|
103 |
};
|
|
104 |
IoResult::Replay(result)
|
|
105 |
}
|
|
106 |
|
|
107 |
IoTask::SaveRoom {
|
|
108 |
room_id,
|
|
109 |
filename,
|
|
110 |
contents,
|
|
111 |
} => {
|
|
112 |
let result = match save_file(&filename, &contents) {
|
|
113 |
Ok(()) => true,
|
|
114 |
Err(e) => {
|
|
115 |
warn!(
|
|
116 |
"Error while writing the room config file \"{}\": {}",
|
|
117 |
filename, e
|
|
118 |
);
|
|
119 |
false
|
|
120 |
}
|
|
121 |
};
|
|
122 |
IoResult::SaveRoom(room_id, result)
|
|
123 |
}
|
|
124 |
|
|
125 |
IoTask::LoadRoom { room_id, filename } => {
|
|
126 |
let result = match load_file(&filename) {
|
|
127 |
Ok(contents) => Some(contents),
|
|
128 |
Err(e) => {
|
|
129 |
warn!(
|
|
130 |
"Error while writing the room config file \"{}\": {}",
|
|
131 |
filename, e
|
|
132 |
);
|
|
133 |
None
|
|
134 |
}
|
|
135 |
};
|
|
136 |
IoResult::LoadRoom(room_id, result)
|
|
137 |
}
|
|
138 |
};
|
|
139 |
io_tx.send((request_id, response));
|
|
140 |
}
|
|
141 |
});
|
|
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 |
pub fn register_rx(&self, poll: &mio::Poll, token: mio::Token) -> Result<()> {
|
|
159 |
self.core_rx
|
|
160 |
.register(poll, token, mio::Ready::readable(), PollOpt::edge())
|
|
161 |
}
|
|
162 |
}
|
|
163 |
|
|
164 |
fn save_file(filename: &str, contents: &str) -> Result<()> {
|
|
165 |
let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
|
|
166 |
writer.write_all(contents.as_bytes())
|
|
167 |
}
|
|
168 |
|
|
169 |
fn load_file(filename: &str) -> Result<String> {
|
|
170 |
let mut reader = File::open(filename)?;
|
|
171 |
let mut result = String::new();
|
|
172 |
reader.read_to_string(&mut result)?;
|
|
173 |
Ok(result)
|
|
174 |
}
|