gameServer2/src/server.rs
author unc0rr
Mon, 02 Jan 2017 00:05:12 +0300
changeset 12127 36ac9c075d0d
parent 12126 4348997e502b
permissions -rw-r--r--
- Use netbuf buffers for client connection stream - Read data from client
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
     1
use slab;
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     2
use mio::tcp::*;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     3
use mio::*;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     4
use std::io::Write;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     5
use std::io;
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
     6
use netbuf;
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     7
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     8
use utils;
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
     9
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    10
type Slab<T> = slab::Slab<T, Token>;
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    11
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    12
pub struct HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    13
    listener: TcpListener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    14
    clients: Slab<HWClient>,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    15
    rooms: Slab<HWRoom>
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    16
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    17
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    18
impl HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    19
    pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    20
        HWServer {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    21
            listener: listener,
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    22
            clients: Slab::with_capacity(clients_limit),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    23
            rooms: Slab::with_capacity(rooms_limit),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    24
        }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    25
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    26
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    27
    pub fn register(&self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    28
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    29
                      PollOpt::edge())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    30
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    31
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    32
    pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    33
        let (sock, addr) = self.listener.accept().unwrap();
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    34
        println!("Connected: {}", addr);
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    35
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    36
        let client = HWClient::new(sock);
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    37
        let token = self.clients.insert(client)
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    38
            .ok().expect("could not add connection to slab");
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    39
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    40
        self.clients[token].send_raw_msg(
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    41
            format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    42
            , utils::PROTOCOL_VERSION).as_bytes());
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    43
        self.clients[token].register(poll, token);
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    44
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    45
        Ok(())
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    46
    }
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    47
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    48
    pub fn client_readable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    49
                           token: Token) -> io::Result<()> {
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    50
        self.clients[token].readable(poll)
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    51
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    52
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    53
    pub fn client_writable(&mut self, poll: &Poll,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    54
                           token: Token) -> io::Result<()> {
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    55
        self.clients[token].writable(poll)
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    56
    }
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    57
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    58
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    59
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    60
struct HWClient {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    61
    sock: TcpStream,
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    62
    buf_in: netbuf::Buf,
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    63
    buf_out: netbuf::Buf
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    64
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    65
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    66
impl HWClient {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    67
    fn new(sock: TcpStream) -> HWClient {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    68
        HWClient {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    69
            sock: sock,
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    70
            buf_in: netbuf::Buf::new(),
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    71
            buf_out: netbuf::Buf::new(),
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    72
        }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    73
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    74
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    75
    fn register(&self, poll: &Poll, token: Token) {
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    76
        poll.register(&self.sock, token, Ready::readable(),
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    77
                      PollOpt::edge())
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    78
            .ok().expect("could not register socket with event loop");
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    79
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    80
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    81
    fn send_raw_msg(&mut self, msg: &[u8]) {
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    82
        self.buf_out.write(msg).unwrap();
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    83
        self.flush();
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    84
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    85
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    86
    fn flush(&mut self) {
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    87
        self.buf_out.write_to(&mut self.sock).unwrap();
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    88
        self.sock.flush();
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    89
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    90
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    91
    fn readable(&mut self, poll: &Poll) -> io::Result<()> {
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    92
        self.buf_in.read_from(&mut self.sock)?;
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    93
        println!("Incoming buffer size: {}", self.buf_in.len());
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    94
        Ok(())
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    95
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    96
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    97
    fn writable(&mut self, poll: &Poll) -> io::Result<()> {
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    98
        self.buf_out.write_to(&mut self.sock)?;
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
    99
        Ok(())
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   100
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   101
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   102
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   103
struct HWRoom {
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   104
    name: String
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   105
}