rust/hedgewars-server/src/server/io.rs
author unC0Rr
Mon, 02 Oct 2023 21:36:13 +0200
changeset 15979 1635ce22b214
parent 15938 ce47259d5c86
child 15989 fb389df02e3e
permissions -rw-r--r--
Adopt more recent versions of dependencies, apply clippy fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
     1
use std::{
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
     2
    fs::{File, OpenOptions},
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
     3
    io::{Error, ErrorKind, Read, Result, Write},
15800
6af892a0a4b8 update mio
alfadur
parents: 15797
diff changeset
     4
    sync::{mpsc, Arc},
15937
e514ceb5e7d6 separate out DB task
alfadur
parents: 15831
diff changeset
     5
    task::Waker,
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
     6
    thread,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
     7
};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
     8
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
     9
use crate::{
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    10
    handlers::{IoResult, IoTask},
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    11
    server::database::Database,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    12
};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    13
use log::*;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    14
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    15
pub type RequestId = u32;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    16
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    17
pub struct IoThread {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    18
    core_tx: mpsc::Sender<(RequestId, IoTask)>,
15800
6af892a0a4b8 update mio
alfadur
parents: 15797
diff changeset
    19
    core_rx: mpsc::Receiver<(RequestId, IoResult)>,
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    20
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    21
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    22
impl IoThread {
15800
6af892a0a4b8 update mio
alfadur
parents: 15797
diff changeset
    23
    pub fn new(waker: Waker) -> Self {
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    24
        let (core_tx, io_rx) = mpsc::channel();
15800
6af892a0a4b8 update mio
alfadur
parents: 15797
diff changeset
    25
        let (io_tx, core_rx) = mpsc::channel();
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    26
15938
ce47259d5c86 add some server todos
alfadur
parents: 15937
diff changeset
    27
        todo!("convert into an IO task");
ce47259d5c86 add some server todos
alfadur
parents: 15937
diff changeset
    28
15937
e514ceb5e7d6 separate out DB task
alfadur
parents: 15831
diff changeset
    29
        /*let mut db = Database::new("localhost");
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    30
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    31
        thread::spawn(move || {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    32
            while let Ok((request_id, task)) = io_rx.recv() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    33
                let response = match task {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    34
                    IoTask::CheckRegistered { nick } => match db.is_registered(&nick) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    35
                        Ok(is_registered) => IoResult::AccountRegistered(is_registered),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    36
                        Err(e) => {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    37
                            warn!("Unable to check account's existence: {}", e);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    38
                            IoResult::AccountRegistered(false)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    39
                        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    40
                    },
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    41
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    42
                    IoTask::GetAccount {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    43
                        nick,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    44
                        protocol,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    45
                        password_hash,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    46
                        client_salt,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    47
                        server_salt,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    48
                    } => {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    49
                        match db.get_account(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    50
                            &nick,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    51
                            protocol,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    52
                            &password_hash,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    53
                            &client_salt,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    54
                            &server_salt,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    55
                        ) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    56
                            Ok(account) => IoResult::Account(account),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    57
                            Err(e) => {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    58
                                warn!("Unable to get account data: {}", e);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    59
                                IoResult::Account(None)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    60
                            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    61
                        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    62
                    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    63
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    64
                    IoTask::GetCheckerAccount { nick, password } => {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    65
                        match db.get_checker_account(&nick, &password) {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    66
                            Ok(is_registered) => IoResult::CheckerAccount { is_registered },
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    67
                            Err(e) => {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    68
                                warn!("Unable to get checker account data: {}", e);
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    69
                                IoResult::CheckerAccount {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    70
                                    is_registered: false,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    71
                                }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    72
                            }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    73
                        }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    74
                    }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15120
diff changeset
    75
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    76
                    IoTask::GetReplay { id } => {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    77
                        let result = match db.get_replay_name(id) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    78
                            Ok(Some(filename)) => {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    79
                                let filename = format!(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    80
                                    "checked/{}",
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    81
                                    if filename.starts_with("replays/") {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    82
                                        &filename[8..]
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    83
                                    } else {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    84
                                        &filename
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    85
                                    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    86
                                );
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15532
diff changeset
    87
2939d8599418 load haskell directly into Replay
alfadur
parents: 15532
diff changeset
    88
                                match crate::core::types::Replay::load(&filename) {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15532
diff changeset
    89
                                    Ok(replay) => Some(replay),
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    90
                                    Err(e) => {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    91
                                        warn!(
15797
655d0e0d612a fix error string
alfadur
parents: 15796
diff changeset
    92
                                            "Error while reading replay file \"{}\": {}",
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    93
                                            filename, e
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    94
                                        );
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    95
                                        None
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    96
                                    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    97
                                }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    98
                            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
    99
                            Ok(None) => None,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   100
                            Err(e) => {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   101
                                warn!("Unable to get replay name: {}", e);
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   102
                                None
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   103
                            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   104
                        };
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   105
                        IoResult::Replay(result)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   106
                    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   107
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   108
                    IoTask::SaveRoom {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   109
                        room_id,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   110
                        filename,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   111
                        contents,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   112
                    } => {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   113
                        let result = match save_file(&filename, &contents) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   114
                            Ok(()) => true,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   115
                            Err(e) => {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   116
                                warn!(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   117
                                    "Error while writing the room config file \"{}\": {}",
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   118
                                    filename, e
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   119
                                );
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   120
                                false
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   121
                            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   122
                        };
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   123
                        IoResult::SaveRoom(room_id, result)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   124
                    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   125
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   126
                    IoTask::LoadRoom { room_id, filename } => {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   127
                        let result = match load_file(&filename) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   128
                            Ok(contents) => Some(contents),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   129
                            Err(e) => {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   130
                                warn!(
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   131
                                    "Error while writing the room config file \"{}\": {}",
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   132
                                    filename, e
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   133
                                );
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   134
                                None
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   135
                            }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   136
                        };
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   137
                        IoResult::LoadRoom(room_id, result)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   138
                    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   139
                };
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   140
                io_tx.send((request_id, response));
15800
6af892a0a4b8 update mio
alfadur
parents: 15797
diff changeset
   141
                waker.wake();
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   142
            }
15937
e514ceb5e7d6 separate out DB task
alfadur
parents: 15831
diff changeset
   143
        });*/
15120
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   144
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   145
        Self { core_rx, core_tx }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   146
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   147
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   148
    pub fn send(&self, request_id: RequestId, task: IoTask) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   149
        self.core_tx.send((request_id, task)).unwrap();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   150
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   151
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   152
    pub fn try_recv(&self) -> Option<(RequestId, IoResult)> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   153
        match self.core_rx.try_recv() {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   154
            Ok(result) => Some(result),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   155
            Err(mpsc::TryRecvError::Empty) => None,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   156
            Err(mpsc::TryRecvError::Disconnected) => unreachable!(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   157
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   158
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   159
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   160
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   161
fn save_file(filename: &str, contents: &str) -> Result<()> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   162
    let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   163
    writer.write_all(contents.as_bytes())
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   164
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   165
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   166
fn load_file(filename: &str) -> Result<String> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   167
    let mut reader = File::open(filename)?;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   168
    let mut result = String::new();
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   169
    reader.read_to_string(&mut result)?;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   170
    Ok(result)
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 15103
diff changeset
   171
}