author | alfadur |
Wed, 10 Apr 2019 01:13:29 +0300 | |
changeset 14802 | 01f8ab45f806 |
parent 14801 | 65861ba8b4e8 |
child 14806 | a1077e8d26f4 |
permissions | -rw-r--r-- |
14413 | 1 |
use std::{ |
2 |
fs::{File, OpenOptions}, |
|
14478 | 3 |
io::{Error, ErrorKind, Read, Result, Write}, |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
4 |
sync::mpsc, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
5 |
thread, |
14413 | 6 |
}; |
7 |
||
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
8 |
use crate::server::{ |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
9 |
database::Database, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
10 |
handlers::{IoResult, IoTask}, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
11 |
}; |
14802 | 12 |
use log::*; |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
13 |
use mio::{Evented, Poll, PollOpt}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
14 |
use mio_extras::channel; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
15 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
16 |
pub type RequestId = u32; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
17 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
18 |
pub struct IOThread { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
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:
14478
diff
changeset
|
20 |
core_rx: channel::Receiver<(RequestId, IoResult)>, |
14413 | 21 |
} |
22 |
||
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
23 |
impl IOThread { |
14413 | 24 |
pub fn new() -> Self { |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
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:
14478
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:
14478
diff
changeset
|
27 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
28 |
let mut db = Database::new(); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
29 |
db.connect("localhost"); |
14413 | 30 |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
31 |
thread::spawn(move || { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
32 |
while let Ok((request_id, task)) = io_rx.try_recv() { |
14801 | 33 |
let response = match task { |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
34 |
IoTask::GetAccount { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
35 |
nick, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
36 |
protocol, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
37 |
password_hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
38 |
client_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
39 |
server_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
40 |
} => { |
14801 | 41 |
match db.get_account( |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
42 |
&nick, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
43 |
protocol, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
44 |
&password_hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
45 |
&client_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
46 |
&server_salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
47 |
) { |
14802 | 48 |
Ok(account) => IoResult::Account(account), |
14801 | 49 |
Err(..) => { |
50 |
warn!("Unable to get account data: {}", 0); |
|
51 |
IoResult::Account(None) |
|
52 |
} |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
53 |
} |
14802 | 54 |
} |
14801 | 55 |
|
14802 | 56 |
IoTask::SaveRoom { |
57 |
room_id, |
|
58 |
filename, |
|
59 |
contents, |
|
60 |
} => { |
|
14801 | 61 |
let result = match save_file(&filename, &contents) { |
62 |
Ok(()) => true, |
|
63 |
Err(e) => { |
|
64 |
warn!( |
|
65 |
"Error while writing the room config file \"{}\": {}", |
|
66 |
filename, e |
|
67 |
); |
|
68 |
false |
|
14802 | 69 |
} |
14801 | 70 |
}; |
71 |
IoResult::SaveRoom(room_id, result) |
|
14802 | 72 |
} |
14801 | 73 |
|
14802 | 74 |
IoTask::LoadRoom { room_id, filename } => { |
14801 | 75 |
let result = match load_file(&filename) { |
76 |
Ok(contents) => Some(contents), |
|
77 |
Err(e) => { |
|
78 |
warn!( |
|
79 |
"Error while writing the room config file \"{}\": {}", |
|
80 |
filename, e |
|
81 |
); |
|
82 |
None |
|
83 |
} |
|
84 |
}; |
|
85 |
IoResult::LoadRoom(room_id, result) |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
86 |
} |
14801 | 87 |
}; |
88 |
io_tx.send((request_id, response)); |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
89 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
90 |
}); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
91 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
92 |
Self { core_rx, core_tx } |
14413 | 93 |
} |
94 |
||
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
95 |
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:
14478
diff
changeset
|
96 |
self.core_tx.send((request_id, task)).unwrap(); |
14413 | 97 |
} |
98 |
||
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
99 |
pub fn try_recv(&self) -> Option<(RequestId, IoResult)> { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
100 |
match self.core_rx.try_recv() { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
101 |
Ok(result) => Some(result), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
102 |
Err(mpsc::TryRecvError::Empty) => None, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
103 |
Err(mpsc::TryRecvError::Disconnected) => unreachable!(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
104 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
105 |
} |
14413 | 106 |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
107 |
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:
14478
diff
changeset
|
108 |
self.core_rx |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14478
diff
changeset
|
109 |
.register(poll, token, mio::Ready::readable(), PollOpt::edge()) |
14413 | 110 |
} |
111 |
} |
|
14801 | 112 |
|
113 |
fn save_file(filename: &str, contents: &str) -> Result<()> { |
|
114 |
let mut writer = OpenOptions::new().create(true).write(true).open(filename)?; |
|
115 |
writer.write_all(contents.as_bytes()) |
|
116 |
} |
|
117 |
||
118 |
fn load_file(filename: &str) -> Result<String> { |
|
119 |
let mut reader = File::open(filename)?; |
|
120 |
let mut result = String::new(); |
|
121 |
reader.read_to_string(&mut result)?; |
|
122 |
Ok(result) |
|
14802 | 123 |
} |