author | Wuzzy <Wuzzy2@mail.ru> |
Fri, 24 May 2019 14:55:48 +0200 | |
changeset 15038 | 01bb1d7ca14f |
parent 14785 | a1077e8d26f4 |
child 15074 | c5a6e8566425 |
permissions | -rw-r--r-- |
14392 | 1 |
use std::{ |
2 |
fs::{File, OpenOptions}, |
|
14457 | 3 |
io::{Error, ErrorKind, Read, Result, Write}, |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
4 |
sync::mpsc, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
5 |
thread, |
14392 | 6 |
}; |
7 |
||
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
8 |
use crate::server::{ |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
9 |
database::Database, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
10 |
handlers::{IoResult, IoTask}, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
11 |
}; |
14781 | 12 |
use log::*; |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
13 |
use mio::{Evented, Poll, PollOpt}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
14 |
use mio_extras::channel; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
15 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
16 |
pub type RequestId = u32; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
17 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
18 |
pub struct IOThread { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
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:
14457
diff
changeset
|
20 |
core_rx: channel::Receiver<(RequestId, IoResult)>, |
14392 | 21 |
} |
22 |
||
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
23 |
impl IOThread { |
14392 | 24 |
pub fn new() -> Self { |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
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:
14457
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:
14457
diff
changeset
|
27 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
28 |
let mut db = Database::new(); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
29 |
db.connect("localhost"); |
14392 | 30 |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
31 |
thread::spawn(move || { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
32 |
while let Ok((request_id, task)) = io_rx.try_recv() { |
14780 | 33 |
let response = match task { |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
34 |
IoTask::GetAccount { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
35 |
nick, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
36 |
protocol, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
37 |
password_hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
38 |
client_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
39 |
server_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
40 |
} => { |
14780 | 41 |
match db.get_account( |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
42 |
&nick, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
43 |
protocol, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
44 |
&password_hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
45 |
&client_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
46 |
&server_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
47 |
) { |
14781 | 48 |
Ok(account) => IoResult::Account(account), |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
49 |
Err(e) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
50 |
warn!("Unable to get account data: {}", e); |
14780 | 51 |
IoResult::Account(None) |
52 |
} |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
53 |
} |
14781 | 54 |
} |
14780 | 55 |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
56 |
IoTask::GetReplay { id } => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
57 |
let result = match db.get_replay_name(id) { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
58 |
Ok(Some(filename)) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
59 |
let filename = format!( |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
60 |
"checked/{}", |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
61 |
if filename.starts_with("replays/") { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
62 |
&filename[8..] |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
63 |
} else { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
64 |
&filename |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
65 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
66 |
); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
67 |
match load_file(&filename) { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
68 |
Ok(contents) => Some(unimplemented!()), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
69 |
Err(e) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
70 |
warn!( |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
71 |
"Error while writing the room config file \"{}\": {}", |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
72 |
filename, e |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
73 |
); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
74 |
None |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
75 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
76 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
77 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
78 |
Ok(None) => None, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
79 |
Err(e) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
80 |
warn!("Unable to get replay name: {}", e); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
81 |
None |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
82 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
83 |
}; |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
84 |
IoResult::Replay(result) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
85 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14781
diff
changeset
|
86 |
|
14781 | 87 |
IoTask::SaveRoom { |
88 |
room_id, |
|
89 |
filename, |
|
90 |
contents, |
|
91 |
} => { |
|
14780 | 92 |
let result = match save_file(&filename, &contents) { |
93 |
Ok(()) => true, |
|
94 |
Err(e) => { |
|
95 |
warn!( |
|
96 |
"Error while writing the room config file \"{}\": {}", |
|
97 |
filename, e |
|
98 |
); |
|
99 |
false |
|
14781 | 100 |
} |
14780 | 101 |
}; |
102 |
IoResult::SaveRoom(room_id, result) |
|
14781 | 103 |
} |
14780 | 104 |
|
14781 | 105 |
IoTask::LoadRoom { room_id, filename } => { |
14780 | 106 |
let result = match load_file(&filename) { |
107 |
Ok(contents) => Some(contents), |
|
108 |
Err(e) => { |
|
109 |
warn!( |
|
110 |
"Error while writing the room config file \"{}\": {}", |
|
111 |
filename, e |
|
112 |
); |
|
113 |
None |
|
114 |
} |
|
115 |
}; |
|
116 |
IoResult::LoadRoom(room_id, result) |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
117 |
} |
14780 | 118 |
}; |
119 |
io_tx.send((request_id, response)); |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
120 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
121 |
}); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
122 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
123 |
Self { core_rx, core_tx } |
14392 | 124 |
} |
125 |
||
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
126 |
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:
14457
diff
changeset
|
127 |
self.core_tx.send((request_id, task)).unwrap(); |
14392 | 128 |
} |
129 |
||
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
130 |
pub fn try_recv(&self) -> Option<(RequestId, IoResult)> { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
131 |
match self.core_rx.try_recv() { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
132 |
Ok(result) => Some(result), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
133 |
Err(mpsc::TryRecvError::Empty) => None, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
134 |
Err(mpsc::TryRecvError::Disconnected) => unreachable!(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
135 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
136 |
} |
14392 | 137 |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
138 |
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:
14457
diff
changeset
|
139 |
self.core_rx |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14457
diff
changeset
|
140 |
.register(poll, token, mio::Ready::readable(), PollOpt::edge()) |
14392 | 141 |
} |
142 |
} |
|
14780 | 143 |
|
144 |
fn save_file(filename: &str, contents: &str) -> Result<()> { |
|
145 |
let mut writer = OpenOptions::new().create(true).write(true).open(filename)?; |
|
146 |
writer.write_all(contents.as_bytes()) |
|
147 |
} |
|
148 |
||
149 |
fn load_file(filename: &str) -> Result<String> { |
|
150 |
let mut reader = File::open(filename)?; |
|
151 |
let mut result = String::new(); |
|
152 |
reader.read_to_string(&mut result)?; |
|
153 |
Ok(result) |
|
14781 | 154 |
} |