rust/hedgewars-server/src/server/core.rs
changeset 14780 65861ba8b4e8
parent 14779 f43ab2bd76ae
child 14781 01f8ab45f806
equal deleted inserted replaced
14779:f43ab2bd76ae 14780:65861ba8b4e8
     6 };
     6 };
     7 use crate::utils;
     7 use crate::utils;
     8 
     8 
     9 use log::*;
     9 use log::*;
    10 use slab;
    10 use slab;
    11 use std::fs::{File, OpenOptions};
       
    12 use std::io::{Read, Write};
       
    13 use std::{borrow::BorrowMut, iter, num::NonZeroU16};
    11 use std::{borrow::BorrowMut, iter, num::NonZeroU16};
    14 
    12 
    15 type Slab<T> = slab::Slab<T>;
    13 type Slab<T> = slab::Slab<T>;
    16 
    14 
    17 pub struct HWAnteClient {
    15 pub struct HWAnteClient {
    46 }
    44 }
    47 
    45 
    48 pub struct HWServer {
    46 pub struct HWServer {
    49     pub clients: IndexSlab<HWClient>,
    47     pub clients: IndexSlab<HWClient>,
    50     pub rooms: Slab<HWRoom>,
    48     pub rooms: Slab<HWRoom>,
    51     pub io: Box<dyn HWServerIO>,
       
    52     pub anteroom: HWAnteroom,
    49     pub anteroom: HWAnteroom,
    53 }
    50 }
    54 
    51 
    55 impl HWServer {
    52 impl HWServer {
    56     pub fn new(clients_limit: usize, rooms_limit: usize, io: Box<dyn HWServerIO>) -> Self {
    53     pub fn new(clients_limit: usize, rooms_limit: usize) -> Self {
    57         let rooms = Slab::with_capacity(rooms_limit);
    54         let rooms = Slab::with_capacity(rooms_limit);
    58         let clients = IndexSlab::with_capacity(clients_limit);
    55         let clients = IndexSlab::with_capacity(clients_limit);
    59         Self {
    56         Self {
    60             clients,
    57             clients,
    61             rooms,
    58             rooms,
    62             io,
       
    63             anteroom: HWAnteroom::new(clients_limit),
    59             anteroom: HWAnteroom::new(clients_limit),
    64         }
    60         }
    65     }
    61     }
    66 
    62 
    67     pub fn add_client(&mut self, client_id: ClientId, data: HWAnteClient) {
    63     pub fn add_client(&mut self, client_id: ClientId, data: HWAnteClient) {
   203                 .cloned()
   199                 .cloned()
   204                 .collect();
   200                 .collect();
   205         }
   201         }
   206     }
   202     }
   207 }
   203 }
   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 }