gameServer2/src/server/client.rs
author unc0rr
Mon, 02 Jan 2017 00:16:22 +0300
changeset 12128 f50876f3eff8
child 12129 07972a8c2433
permissions -rw-r--r--
Refactor modules layout
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;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     9
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    10
pub struct HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    11
    sock: TcpStream,
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    12
    buf_in: netbuf::Buf,
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    13
    buf_out: netbuf::Buf
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    14
}
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    15
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    16
impl HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    17
    pub fn new(sock: TcpStream) -> HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    18
        HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    19
            sock: sock,
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    20
            buf_in: netbuf::Buf::new(),
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    21
            buf_out: netbuf::Buf::new(),
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    22
        }
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
    pub fn register(&mut self, poll: &Poll, token: Token) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    26
        poll.register(&self.sock, token, Ready::readable(),
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    27
                      PollOpt::edge())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    28
            .ok().expect("could not register socket with event loop");
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    29
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    30
        self.send_raw_msg(
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    31
            format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    32
                    , utils::PROTOCOL_VERSION).as_bytes());
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    33
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    34
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    35
    fn send_raw_msg(&mut self, msg: &[u8]) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    36
        self.buf_out.write(msg).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    37
        self.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    38
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    39
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    40
    fn flush(&mut self) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    41
        self.buf_out.write_to(&mut self.sock).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    42
        self.sock.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    43
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    44
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    45
    pub fn readable(&mut self, poll: &Poll) -> io::Result<()> {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    46
        self.buf_in.read_from(&mut self.sock)?;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    47
        println!("Incoming buffer size: {}", self.buf_in.len());
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    48
        Ok(())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    49
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    50
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    51
    pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    52
        self.buf_out.write_to(&mut self.sock)?;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    53
        Ok(())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    54
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    55
}