rust/hedgewars-server/src/server/core.rs
changeset 14800 f43ab2bd76ae
parent 14718 f64e21f164a5
child 14801 65861ba8b4e8
equal deleted inserted replaced
14799:bbec6b28d072 14800:f43ab2bd76ae
     1 use super::{
     1 use super::{
     2     client::HWClient,
     2     client::HWClient,
     3     coretypes::{ClientId, RoomId},
     3     coretypes::{ClientId, RoomId},
     4     indexslab::IndexSlab,
     4     indexslab::IndexSlab,
     5     io::HWServerIO,
       
     6     room::HWRoom,
     5     room::HWRoom,
     7 };
     6 };
     8 use crate::utils;
     7 use crate::utils;
     9 
     8 
    10 use log::*;
     9 use log::*;
    11 use slab;
    10 use slab;
       
    11 use std::fs::{File, OpenOptions};
       
    12 use std::io::{Read, Write};
    12 use std::{borrow::BorrowMut, iter, num::NonZeroU16};
    13 use std::{borrow::BorrowMut, iter, num::NonZeroU16};
    13 
    14 
    14 type Slab<T> = slab::Slab<T>;
    15 type Slab<T> = slab::Slab<T>;
    15 
    16 
    16 pub struct HWAnteClient {
    17 pub struct HWAnteClient {
    17     pub nick: Option<String>,
    18     pub nick: Option<String>,
    18     pub protocol_number: Option<NonZeroU16>,
    19     pub protocol_number: Option<NonZeroU16>,
    19     pub web_password: Option<String>,
       
    20     pub server_salt: String,
    20     pub server_salt: String,
    21 }
    21 }
    22 
    22 
    23 pub struct HWAnteroom {
    23 pub struct HWAnteroom {
    24     pub clients: IndexSlab<HWAnteClient>,
    24     pub clients: IndexSlab<HWAnteClient>,
    33     pub fn add_client(&mut self, client_id: ClientId, salt: String) {
    33     pub fn add_client(&mut self, client_id: ClientId, salt: String) {
    34         let client = HWAnteClient {
    34         let client = HWAnteClient {
    35             nick: None,
    35             nick: None,
    36             protocol_number: None,
    36             protocol_number: None,
    37             server_salt: salt,
    37             server_salt: salt,
    38             web_password: None,
       
    39         };
    38         };
    40         self.clients.insert(client_id, client);
    39         self.clients.insert(client_id, client);
    41     }
    40     }
    42 
    41 
    43     pub fn remove_client(&mut self, client_id: ClientId) -> Option<HWAnteClient> {
    42     pub fn remove_client(&mut self, client_id: ClientId) -> Option<HWAnteClient> {
    44         let mut client = self.clients.remove(client_id);
    43         let mut client = self.clients.remove(client_id);
    45         if let Some(HWAnteClient {
       
    46             web_password: Some(ref mut password),
       
    47             ..
       
    48         }) = client
       
    49         {
       
    50             password.replace_range(.., "🦔🦔🦔🦔🦔🦔🦔🦔");
       
    51         }
       
    52         client
    44         client
    53     }
    45     }
    54 }
    46 }
    55 
    47 
    56 pub struct HWServer {
    48 pub struct HWServer {
   211                 .cloned()
   203                 .cloned()
   212                 .collect();
   204                 .collect();
   213         }
   205         }
   214     }
   206     }
   215 }
   207 }
       
   208 
       
   209 pub trait HWServerIO {
       
   210     fn write_file(&mut self, name: &str, content: &str) -> std::io::Result<()>;
       
   211     fn read_file(&mut self, name: &str) -> std::io::Result<String>;
       
   212 }
       
   213 
       
   214 pub struct EmptyServerIO {}
       
   215 
       
   216 impl EmptyServerIO {
       
   217     pub fn new() -> Self {
       
   218         Self {}
       
   219     }
       
   220 }
       
   221 
       
   222 impl HWServerIO for EmptyServerIO {
       
   223     fn write_file(&mut self, _name: &str, _content: &str) -> std::io::Result<()> {
       
   224         Ok(())
       
   225     }
       
   226 
       
   227     fn read_file(&mut self, _name: &str) -> std::io::Result<String> {
       
   228         Ok("".to_string())
       
   229     }
       
   230 }
       
   231 
       
   232 pub struct FileServerIO {}
       
   233 
       
   234 impl FileServerIO {
       
   235     pub fn new() -> Self {
       
   236         Self {}
       
   237     }
       
   238 }
       
   239 
       
   240 impl HWServerIO for FileServerIO {
       
   241     fn write_file(&mut self, name: &str, content: &str) -> std::io::Result<()> {
       
   242         let mut writer = OpenOptions::new().create(true).write(true).open(name)?;
       
   243         writer.write_all(content.as_bytes())
       
   244     }
       
   245 
       
   246     fn read_file(&mut self, name: &str) -> std::io::Result<String> {
       
   247         let mut reader = File::open(name)?;
       
   248         let mut result = String::new();
       
   249         reader.read_to_string(&mut result)?;
       
   250         Ok(result)
       
   251     }
       
   252 }