rust/hedgewars-server/src/server/handlers.rs
author alfadur <mail@none>
Sat, 02 Feb 2019 15:06:39 +0300
changeset 14671 455865ccd36c
parent 14457 98ef2913ec73
child 14672 6e6632068a33
permissions -rw-r--r--
Server action refactoring part 2 of N

use mio;
use std::{io, io::Write};

use super::{
    actions::{Action, Action::*},
    core::HWServer,
    coretypes::ClientId,
};
use crate::{
    protocol::messages::{HWProtocolMessage, HWServerMessage::*},
    server::actions::PendingMessage,
};
use log::*;

mod checker;
mod common;
mod inroom;
mod lobby;
mod loggingin;

pub struct Response {
    client_id: ClientId,
    messages: Vec<PendingMessage>,
}

impl Response {
    pub fn new(client_id: ClientId) -> Self {
        Self {
            client_id,
            messages: vec![],
        }
    }

    pub fn client_id(&self) -> ClientId {
        self.client_id
    }

    pub fn add(&mut self, message: PendingMessage) {
        self.messages.push(message)
    }
}

pub fn handle(
    server: &mut HWServer,
    client_id: ClientId,
    response: &mut Response,
    message: HWProtocolMessage,
) {
    match message {
        HWProtocolMessage::Ping => {
            response.add(Pong.send_self());
            server.react(client_id, vec![Pong.send_self().action()])
        }
        HWProtocolMessage::Quit(Some(msg)) => {
            server.react(client_id, vec![ByeClient("User quit: ".to_string() + &msg)])
        }
        HWProtocolMessage::Quit(None) => {
            server.react(client_id, vec![ByeClient("User quit".to_string())])
        }
        HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
        HWProtocolMessage::Empty => warn!("Empty message"),
        _ => match server.clients[client_id].room_id {
            None => loggingin::handle(server, client_id, response, message),
            Some(id) if id == server.lobby_id => {
                lobby::handle(server, client_id, response, message)
            }
            Some(id) => inroom::handle(server, client_id, response, id, message),
        },
    }
}