gameServer2/src/server/client.rs
author unc0rr
Wed, 18 Jan 2017 22:15:55 +0300
changeset 12139 f3121d7dedec
parent 12138 e0bf51609062
child 12141 78925eff02c2
permissions -rw-r--r--
- Handle errors - Client removal routine - Handle QUIT message

use slab;
use mio::tcp::*;
use mio::*;
use std::io::Write;
use std::io;
use netbuf;

use utils;
use protocol::ProtocolDecoder;
use protocol::messages;
use protocol::messages::HWProtocolMessage::*;
use server::actions::Action::*;
use server::actions::Action;
use log;

pub struct HWClient {
    sock: TcpStream,
    decoder: ProtocolDecoder,
    buf_out: netbuf::Buf
}

impl HWClient {
    pub fn new(sock: TcpStream) -> HWClient {
        HWClient {
            sock: sock,
            decoder: ProtocolDecoder::new(),
            buf_out: netbuf::Buf::new(),
        }
    }

    pub fn register(&mut self, poll: &Poll, token: Token) {
        poll.register(&self.sock, token, Ready::all(),
                      PollOpt::edge())
            .ok().expect("could not register socket with event loop");

        self.send_msg(Connected(utils::PROTOCOL_VERSION));
    }

    pub fn deregister(&mut self, poll: &Poll) {
        poll.deregister(&self.sock);
    }

    pub fn send_raw_msg(&mut self, msg: &[u8]) {
        self.buf_out.write(msg).unwrap();
        self.flush();
    }

    pub fn send_string(&mut self, msg: &String) {
        self.send_raw_msg(&msg.as_bytes());
    }

    pub fn send_msg(&mut self, msg: messages::HWProtocolMessage) {
        self.send_string(&msg.to_raw_protocol());
    }

    fn flush(&mut self) {
        self.buf_out.write_to(&mut self.sock).unwrap();
        self.sock.flush();
    }

    pub fn readable(&mut self, poll: &Poll) -> Vec<Action> {
        let v = self.decoder.read_from(&mut self.sock).unwrap();
        debug!("Read {} bytes", v);
        let mut response = Vec::new();
        {
            let msgs = self.decoder.extract_messages();
            for msg in msgs {
                match msg {
                    Ping => response.push(SendMe(Pong.to_raw_protocol())),
                    Quit(Some(msg)) => response.push(ByeClient("User quit: ".to_string() + msg)),
                    Quit(None) => response.push(ByeClient("User quit".to_string())),
                    Malformed => warn!("Malformed/unknown message"),
                    Empty => warn!("Empty message"),
                    _ => unimplemented!(),
                }
            }
        }
        self.decoder.sweep();
        response
    }

    pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
        self.buf_out.write_to(&mut self.sock)?;

        Ok(())
    }

    pub fn error(&mut self, poll: &Poll) -> Vec<Action> {
        return vec![ByeClient("Connection reset".to_string())]
    }
}