gameServer2/src/server/client.rs
author unc0rr
Tue, 03 Jan 2017 00:19:47 +0300
changeset 12129 07972a8c2433
parent 12128 f50876f3eff8
child 12136 e25a82ce2374
permissions -rw-r--r--
- Start protocol parser implementation - Small fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     1
use slab;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     2
use mio::tcp::*;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     3
use mio::*;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     4
use std::io::Write;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     5
use std::io;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     6
use netbuf;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     7
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     8
use utils;
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
     9
use protocol::FrameDecoder;
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    10
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    11
pub struct HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    12
    sock: TcpStream,
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
    13
    decoder: FrameDecoder,
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    14
    buf_out: netbuf::Buf
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    15
}
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    16
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    17
impl HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    18
    pub fn new(sock: TcpStream) -> HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    19
        HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    20
            sock: sock,
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
    21
            decoder: FrameDecoder::new(),
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    22
            buf_out: netbuf::Buf::new(),
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    23
        }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    24
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    25
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    26
    pub fn register(&mut self, poll: &Poll, token: Token) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    27
        poll.register(&self.sock, token, Ready::readable(),
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    28
                      PollOpt::edge())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    29
            .ok().expect("could not register socket with event loop");
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    30
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    31
        self.send_raw_msg(
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    32
            format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    33
                    , utils::PROTOCOL_VERSION).as_bytes());
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    34
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    35
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    36
    fn send_raw_msg(&mut self, msg: &[u8]) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    37
        self.buf_out.write(msg).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    38
        self.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    39
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    40
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    41
    fn flush(&mut self) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    42
        self.buf_out.write_to(&mut self.sock).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    43
        self.sock.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    44
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    45
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    46
    pub fn readable(&mut self, poll: &Poll) -> io::Result<()> {
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
    47
        let v = self.decoder.read_from(&mut self.sock)?;
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
    48
        self.decoder.extract_messages();
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
    49
        println!("Read {} bytes", v);
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    50
        Ok(())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    51
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    52
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    53
    pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    54
        self.buf_out.write_to(&mut self.sock)?;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    55
        Ok(())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    56
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    57
}