rust/hedgewars-server/src/server/io.rs
author alfadur
Tue, 09 Apr 2019 21:08:35 +0300
changeset 14800 f43ab2bd76ae
parent 14478 98ef2913ec73
child 14801 65861ba8b4e8
permissions -rw-r--r--
add a thread for internal server IO and implement account checking with it
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14413
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     1
use std::{
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     2
    fs::{File, OpenOptions},
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     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
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     6
};
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     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
};
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    12
use mio::{Evented, Poll, PollOpt};
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    13
use mio_extras::channel;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    14
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    15
pub type RequestId = u32;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    16
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    17
pub struct IOThread {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    18
    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
    19
    core_rx: channel::Receiver<(RequestId, IoResult)>,
14413
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    20
}
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    21
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    22
impl IOThread {
14413
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    23
    pub fn new() -> Self {
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    24
        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
    25
        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
    26
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    27
        let mut db = Database::new();
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    28
        db.connect("localhost");
14413
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    29
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    30
        thread::spawn(move || {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    31
            while let Ok((request_id, task)) = io_rx.try_recv() {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    32
                match task {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    33
                    IoTask::GetAccount {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    34
                        nick,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    35
                        protocol,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    36
                        password_hash,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    37
                        client_salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    38
                        server_salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    39
                    } => {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    40
                        if let Ok(account) = db.get_account(
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    41
                            &nick,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    42
                            protocol,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    43
                            &password_hash,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    44
                            &client_salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    45
                            &server_salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    46
                        ) {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    47
                            io_tx.send((request_id, IoResult::Account(account)));
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    48
                        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    49
                    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    50
                }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    51
            }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    52
        });
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    53
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    54
        Self { core_rx, core_tx }
14413
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    55
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    56
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    57
    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
    58
        self.core_tx.send((request_id, task)).unwrap();
14413
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    59
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    60
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    61
    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
    62
        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
    63
            Ok(result) => Some(result),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    64
            Err(mpsc::TryRecvError::Empty) => None,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    65
            Err(mpsc::TryRecvError::Disconnected) => unreachable!(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    66
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    67
    }
14413
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    68
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    69
    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
    70
        self.core_rx
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    71
            .register(poll, token, mio::Ready::readable(), PollOpt::edge())
14413
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    72
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    73
}