15125
|
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 |
|
|
64 |
IoTask::GetReplay { id } => {
|
|
65 |
let result = match db.get_replay_name(id) {
|
|
66 |
Ok(Some(filename)) => {
|
|
67 |
let filename = format!(
|
|
68 |
"checked/{}",
|
|
69 |
if filename.starts_with("replays/") {
|
|
70 |
&filename[8..]
|
|
71 |
} else {
|
|
72 |
&filename
|
|
73 |
}
|
|
74 |
);
|
|
75 |
match load_file(&filename) {
|
|
76 |
Ok(contents) => Some(unimplemented!()),
|
|
77 |
Err(e) => {
|
|
78 |
warn!(
|
|
79 |
"Error while writing the room config file \"{}\": {}",
|
|
80 |
filename, e
|
|
81 |
);
|
|
82 |
None
|
|
83 |
}
|
|
84 |
}
|
|
85 |
}
|
|
86 |
Ok(None) => None,
|
|
87 |
Err(e) => {
|
|
88 |
warn!("Unable to get replay name: {}", e);
|
|
89 |
None
|
|
90 |
}
|
|
91 |
};
|
|
92 |
IoResult::Replay(result)
|
|
93 |
}
|
|
94 |
|
|
95 |
IoTask::SaveRoom {
|
|
96 |
room_id,
|
|
97 |
filename,
|
|
98 |
contents,
|
|
99 |
} => {
|
|
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
|
|
108 |
}
|
|
109 |
};
|
|
110 |
IoResult::SaveRoom(room_id, result)
|
|
111 |
}
|
|
112 |
|
|
113 |
IoTask::LoadRoom { room_id, filename } => {
|
|
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)
|
|
125 |
}
|
|
126 |
};
|
|
127 |
io_tx.send((request_id, response));
|
|
128 |
}
|
|
129 |
});
|
|
130 |
|
|
131 |
Self { core_rx, core_tx }
|
|
132 |
}
|
|
133 |
|
|
134 |
pub fn send(&self, request_id: RequestId, task: IoTask) {
|
|
135 |
self.core_tx.send((request_id, task)).unwrap();
|
|
136 |
}
|
|
137 |
|
|
138 |
pub fn try_recv(&self) -> Option<(RequestId, IoResult)> {
|
|
139 |
match self.core_rx.try_recv() {
|
|
140 |
Ok(result) => Some(result),
|
|
141 |
Err(mpsc::TryRecvError::Empty) => None,
|
|
142 |
Err(mpsc::TryRecvError::Disconnected) => unreachable!(),
|
|
143 |
}
|
|
144 |
}
|
|
145 |
|
|
146 |
pub fn register_rx(&self, poll: &mio::Poll, token: mio::Token) -> Result<()> {
|
|
147 |
self.core_rx
|
|
148 |
.register(poll, token, mio::Ready::readable(), PollOpt::edge())
|
|
149 |
}
|
|
150 |
}
|
|
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)
|
|
162 |
}
|