mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-23 16:36:49 +00:00
wip reusing of ids
This commit is contained in:
@@ -23,12 +23,21 @@ use super::backend::*;
|
|||||||
use super::car::*;
|
use super::car::*;
|
||||||
use super::packet::*;
|
use super::packet::*;
|
||||||
|
|
||||||
static ATOMIC_ID_COUNTER: AtomicU8 = AtomicU8::new(0);
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
pub static ref FREE_IDS: Mutex<Vec<u8>> = Mutex::new((0..=255).collect());
|
||||||
pub static ref CLIENT_MOD_PROGRESS: Mutex<HashMap<u8, isize>> = Mutex::new(HashMap::new());
|
pub static ref CLIENT_MOD_PROGRESS: Mutex<HashMap<u8, isize>> = Mutex::new(HashMap::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn claim_id() -> u8 {
|
||||||
|
let mut lock = FREE_IDS.lock().await;
|
||||||
|
lock.remove(0) // This can panic when we run out of IDs!
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn free_id(id: u8) {
|
||||||
|
let mut lock = FREE_IDS.lock().await;
|
||||||
|
lock.push(id);
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub enum ClientState {
|
pub enum ClientState {
|
||||||
None,
|
None,
|
||||||
@@ -63,13 +72,14 @@ pub struct Client {
|
|||||||
|
|
||||||
impl Drop for Client {
|
impl Drop for Client {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
|
tokio::spawn(free_id(self.id));
|
||||||
self.write_runtime.abort();
|
self.write_runtime.abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
pub fn new(socket: TcpStream) -> Self {
|
pub async fn new(socket: TcpStream) -> Self {
|
||||||
let id = ATOMIC_ID_COUNTER.fetch_add(1, Ordering::SeqCst);
|
let id = claim_id().await;
|
||||||
trace!("Client with ID #{} created!", id);
|
trace!("Client with ID #{} created!", id);
|
||||||
|
|
||||||
let (read_half, write_half) = socket.into_split();
|
let (read_half, write_half) = socket.into_split();
|
||||||
|
|||||||
@@ -188,7 +188,7 @@ impl Server {
|
|||||||
|
|
||||||
match code as char {
|
match code as char {
|
||||||
'C' => {
|
'C' => {
|
||||||
let mut client = Client::new(socket);
|
let mut client = Client::new(socket).await;
|
||||||
match client.authenticate(&cfg_ref).await {
|
match client.authenticate(&cfg_ref).await {
|
||||||
Ok(is_client) if is_client => {
|
Ok(is_client) if is_client => {
|
||||||
let mut lock = ci_ref
|
let mut lock = ci_ref
|
||||||
|
|||||||
Reference in New Issue
Block a user