gameServer2/src/server/client.rs
author unc0rr
Sat, 14 Jan 2017 00:46:52 +0300
changeset 12136 e25a82ce2374
parent 12129 07972a8c2433
child 12137 193dfdcb0620
permissions -rw-r--r--
- Render messages to string - Respond to PING messages
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;
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
     9
use protocol::ProtocolDecoder;
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    10
use protocol::messages;
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    11
use protocol::messages::HWProtocolMessage::*;
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    12
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    13
pub struct HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    14
    sock: TcpStream,
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    15
    decoder: ProtocolDecoder,
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    16
    buf_out: netbuf::Buf
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    17
}
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    18
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    19
impl HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    20
    pub fn new(sock: TcpStream) -> HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    21
        HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    22
            sock: sock,
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    23
            decoder: ProtocolDecoder::new(),
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    24
            buf_out: netbuf::Buf::new(),
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    25
        }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    26
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    27
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    28
    pub fn register(&mut self, poll: &Poll, token: Token) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    29
        poll.register(&self.sock, token, Ready::readable(),
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    30
                      PollOpt::edge())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    31
            .ok().expect("could not register socket with event loop");
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    32
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    33
        self.send_raw_msg(
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    34
            format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    35
                    , utils::PROTOCOL_VERSION).as_bytes());
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    36
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    37
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    38
    fn send_raw_msg(&mut self, msg: &[u8]) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    39
        self.buf_out.write(msg).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    40
        self.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    41
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    42
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    43
    fn send_msg(&mut self, msg: messages::HWProtocolMessage) {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    44
        self.send_raw_msg(&msg.to_raw_protocol().into_bytes());
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    45
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    46
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    47
    fn flush(&mut self) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    48
        self.buf_out.write_to(&mut self.sock).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    49
        self.sock.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    50
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    51
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    52
    pub fn readable(&mut self, poll: &Poll) -> io::Result<()> {
12129
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
    53
        let v = self.decoder.read_from(&mut self.sock)?;
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12128
diff changeset
    54
        println!("Read {} bytes", v);
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    55
        let mut response = Vec::new();
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    56
        {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    57
            let msgs = self.decoder.extract_messages();
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    58
            for msg in msgs {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    59
                match msg {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    60
                    Ping => response.push(Pong),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    61
                    _ => println!("Unknown message")
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    62
                }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    63
            }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    64
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    65
        for r in response {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    66
            self.send_msg(r);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    67
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    68
        self.decoder.sweep();
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    69
        Ok(())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    70
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    71
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    72
    pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    73
        self.buf_out.write_to(&mut self.sock)?;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    74
        Ok(())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    75
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    76
}