rust/hedgewars-server/src/server/io.rs
changeset 15800 6af892a0a4b8
parent 15797 655d0e0d612a
child 15831 7d0f747afcb8
equal deleted inserted replaced
15799:ed3b510b860c 15800:6af892a0a4b8
     1 use std::{
     1 use std::{
     2     fs::{File, OpenOptions},
     2     fs::{File, OpenOptions},
     3     io::{Error, ErrorKind, Read, Result, Write},
     3     io::{Error, ErrorKind, Read, Result, Write},
     4     sync::mpsc,
     4     sync::{mpsc, Arc},
     5     thread,
     5     thread,
     6 };
     6 };
     7 
     7 
     8 use crate::{
     8 use crate::{
     9     handlers::{IoResult, IoTask},
     9     handlers::{IoResult, IoTask},
    10     server::database::Database,
    10     server::database::Database,
    11 };
    11 };
    12 use log::*;
    12 use log::*;
    13 use mio::{Evented, Poll, PollOpt};
    13 use mio::{Poll, Waker};
    14 use mio_extras::channel;
       
    15 
    14 
    16 pub type RequestId = u32;
    15 pub type RequestId = u32;
    17 
    16 
    18 pub struct IoThread {
    17 pub struct IoThread {
    19     core_tx: mpsc::Sender<(RequestId, IoTask)>,
    18     core_tx: mpsc::Sender<(RequestId, IoTask)>,
    20     core_rx: channel::Receiver<(RequestId, IoResult)>,
    19     core_rx: mpsc::Receiver<(RequestId, IoResult)>,
    21 }
    20 }
    22 
    21 
    23 impl IoThread {
    22 impl IoThread {
    24     pub fn new() -> Self {
    23     pub fn new(waker: Waker) -> Self {
    25         let (core_tx, io_rx) = mpsc::channel();
    24         let (core_tx, io_rx) = mpsc::channel();
    26         let (io_tx, core_rx) = channel::channel();
    25         let (io_tx, core_rx) = mpsc::channel();
    27 
    26 
    28         let mut db = Database::new();
    27         let mut db = Database::new();
    29         db.connect("localhost");
    28         db.connect("localhost");
    30 
    29 
    31         thread::spawn(move || {
    30         thread::spawn(move || {
   136                         };
   135                         };
   137                         IoResult::LoadRoom(room_id, result)
   136                         IoResult::LoadRoom(room_id, result)
   138                     }
   137                     }
   139                 };
   138                 };
   140                 io_tx.send((request_id, response));
   139                 io_tx.send((request_id, response));
       
   140                 waker.wake();
   141             }
   141             }
   142         });
   142         });
   143 
   143 
   144         Self { core_rx, core_tx }
   144         Self { core_rx, core_tx }
   145     }
   145     }
   153             Ok(result) => Some(result),
   153             Ok(result) => Some(result),
   154             Err(mpsc::TryRecvError::Empty) => None,
   154             Err(mpsc::TryRecvError::Empty) => None,
   155             Err(mpsc::TryRecvError::Disconnected) => unreachable!(),
   155             Err(mpsc::TryRecvError::Disconnected) => unreachable!(),
   156         }
   156         }
   157     }
   157     }
   158 
       
   159     pub fn register_rx(&self, poll: &mio::Poll, token: mio::Token) -> Result<()> {
       
   160         self.core_rx
       
   161             .register(poll, token, mio::Ready::readable(), PollOpt::edge())
       
   162     }
       
   163 }
   158 }
   164 
   159 
   165 fn save_file(filename: &str, contents: &str) -> Result<()> {
   160 fn save_file(filename: &str, contents: &str) -> Result<()> {
   166     let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
   161     let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
   167     writer.write_all(contents.as_bytes())
   162     writer.write_all(contents.as_bytes())