rust/hedgewars-server/src/core/server.rs
changeset 15520 fd3a20e9d095
parent 15519 b3157d218ae2
child 15522 4a0b06b03199
--- a/rust/hedgewars-server/src/core/server.rs	Sat Dec 21 23:33:50 2019 +0300
+++ b/rust/hedgewars-server/src/core/server.rs	Mon Dec 23 18:08:55 2019 +0300
@@ -1,4 +1,5 @@
 use super::{
+    anteroom::HwAnteroomClient,
     client::HwClient,
     indexslab::IndexSlab,
     room::HwRoom,
@@ -8,10 +9,9 @@
 
 use bitflags::_core::hint::unreachable_unchecked;
 use bitflags::*;
-use chrono::{offset, DateTime};
 use log::*;
 use slab::Slab;
-use std::{borrow::BorrowMut, collections::HashSet, iter, mem::replace, num::NonZeroU16};
+use std::{borrow::BorrowMut, collections::HashSet, iter, mem::replace};
 
 #[derive(Debug)]
 pub enum CreateRoomError {
@@ -106,91 +106,6 @@
 #[derive(Debug)]
 pub struct AccessError();
 
-pub struct HwAnteClient {
-    pub nick: Option<String>,
-    pub protocol_number: Option<NonZeroU16>,
-    pub server_salt: String,
-    pub is_checker: bool,
-    pub is_local_admin: bool,
-    pub is_registered: bool,
-    pub is_admin: bool,
-    pub is_contributor: bool,
-}
-
-struct Ipv4AddrRange {
-    min: [u8; 4],
-    max: [u8; 4],
-}
-
-impl Ipv4AddrRange {
-    fn contains(&self, addr: [u8; 4]) -> bool {
-        (0..4).all(|i| self.min[i] <= addr[i] && addr[i] <= self.max[i])
-    }
-}
-
-struct BanCollection {
-    ban_ips: Vec<Ipv4AddrRange>,
-    ban_timeouts: Vec<DateTime<offset::Utc>>,
-    ban_reasons: Vec<String>,
-}
-
-impl BanCollection {
-    fn new() -> Self {
-        Self {
-            ban_ips: vec![],
-            ban_timeouts: vec![],
-            ban_reasons: vec![],
-        }
-    }
-
-    fn find(&self, addr: [u8; 4]) -> Option<String> {
-        let time = offset::Utc::now();
-        self.ban_ips
-            .iter()
-            .enumerate()
-            .find(|(i, r)| r.contains(addr) && time < self.ban_timeouts[*i])
-            .map(|(i, _)| self.ban_reasons[i].clone())
-    }
-}
-
-pub struct HwAnteroom {
-    pub clients: IndexSlab<HwAnteClient>,
-    bans: BanCollection,
-}
-
-impl HwAnteroom {
-    pub fn new(clients_limit: usize) -> Self {
-        let clients = IndexSlab::with_capacity(clients_limit);
-        HwAnteroom {
-            clients,
-            bans: BanCollection::new(),
-        }
-    }
-
-    pub fn find_ip_ban(&self, addr: [u8; 4]) -> Option<String> {
-        self.bans.find(addr)
-    }
-
-    pub fn add_client(&mut self, client_id: ClientId, salt: String, is_local_admin: bool) {
-        let client = HwAnteClient {
-            nick: None,
-            protocol_number: None,
-            server_salt: salt,
-            is_checker: false,
-            is_local_admin,
-            is_registered: false,
-            is_admin: false,
-            is_contributor: false,
-        };
-        self.clients.insert(client_id, client);
-    }
-
-    pub fn remove_client(&mut self, client_id: ClientId) -> Option<HwAnteClient> {
-        let client = self.clients.remove(client_id);
-        client
-    }
-}
-
 pub struct ServerGreetings {
     pub for_latest_protocol: String,
     pub for_old_protocols: String,
@@ -212,9 +127,8 @@
 }
 
 pub struct HwServer {
-    pub clients: IndexSlab<HwClient>,
+    clients: IndexSlab<HwClient>,
     pub rooms: Slab<HwRoom>,
-    pub anteroom: HwAnteroom,
     pub latest_protocol: u16,
     pub flags: ServerFlags,
     pub greetings: ServerGreetings,
@@ -227,7 +141,6 @@
         Self {
             clients,
             rooms,
-            anteroom: HwAnteroom::new(clients_limit),
             greetings: ServerGreetings::new(),
             latest_protocol: 58,
             flags: ServerFlags::empty(),
@@ -286,7 +199,7 @@
             .unwrap_or(false)
     }
 
-    pub fn add_client(&mut self, client_id: ClientId, data: HwAnteClient) {
+    pub fn add_client(&mut self, client_id: ClientId, data: HwAnteroomClient) {
         if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
             let mut client = HwClient::new(client_id, protocol.get(), nick);
             client.set_is_checker(data.is_checker);