gameServer2/src/server.rs
changeset 12127 36ac9c075d0d
parent 12126 4348997e502b
equal deleted inserted replaced
12126:4348997e502b 12127:36ac9c075d0d
     1 use slab::*;
     1 use slab;
     2 use mio::tcp::*;
     2 use mio::tcp::*;
     3 use mio::*;
     3 use mio::*;
     4 use mio;
       
     5 use std::io::Write;
     4 use std::io::Write;
     6 use std::io;
     5 use std::io;
       
     6 use netbuf;
     7 
     7 
     8 use utils;
     8 use utils;
       
     9 
       
    10 type Slab<T> = slab::Slab<T, Token>;
     9 
    11 
    10 pub struct HWServer {
    12 pub struct HWServer {
    11     listener: TcpListener,
    13     listener: TcpListener,
    12     clients: Slab<HWClient>,
    14     clients: Slab<HWClient>,
    13     rooms: Slab<HWRoom>
    15     rooms: Slab<HWRoom>
    36             .ok().expect("could not add connection to slab");
    38             .ok().expect("could not add connection to slab");
    37 
    39 
    38         self.clients[token].send_raw_msg(
    40         self.clients[token].send_raw_msg(
    39             format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
    41             format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
    40             , utils::PROTOCOL_VERSION).as_bytes());
    42             , utils::PROTOCOL_VERSION).as_bytes());
    41 
    43         self.clients[token].register(poll, token);
    42         self.clients[token].uid = Some(token);
       
    43         poll.register(&self.clients[token].sock, mio::Token(token), Ready::readable(),
       
    44                       PollOpt::edge() | PollOpt::oneshot())
       
    45             .ok().expect("could not register socket with event loop");
       
    46 
    44 
    47         Ok(())
    45         Ok(())
    48     }
    46     }
       
    47 
       
    48     pub fn client_readable(&mut self, poll: &Poll,
       
    49                            token: Token) -> io::Result<()> {
       
    50         self.clients[token].readable(poll)
       
    51     }
       
    52 
       
    53     pub fn client_writable(&mut self, poll: &Poll,
       
    54                            token: Token) -> io::Result<()> {
       
    55         self.clients[token].writable(poll)
       
    56     }
    49 }
    57 }
       
    58 
    50 
    59 
    51 struct HWClient {
    60 struct HWClient {
    52     sock: TcpStream,
    61     sock: TcpStream,
    53     uid: Option<usize>
    62     buf_in: netbuf::Buf,
       
    63     buf_out: netbuf::Buf
    54 }
    64 }
    55 
    65 
    56 impl HWClient {
    66 impl HWClient {
    57     fn new(sock: TcpStream) -> HWClient {
    67     fn new(sock: TcpStream) -> HWClient {
    58         HWClient {
    68         HWClient {
    59             sock: sock,
    69             sock: sock,
    60             uid: None
    70             buf_in: netbuf::Buf::new(),
       
    71             buf_out: netbuf::Buf::new(),
    61         }
    72         }
    62     }
    73     }
    63 
    74 
       
    75     fn register(&self, poll: &Poll, token: Token) {
       
    76         poll.register(&self.sock, token, Ready::readable(),
       
    77                       PollOpt::edge())
       
    78             .ok().expect("could not register socket with event loop");
       
    79     }
       
    80 
    64     fn send_raw_msg(&mut self, msg: &[u8]) {
    81     fn send_raw_msg(&mut self, msg: &[u8]) {
    65         self.sock.write_all(msg).unwrap();
    82         self.buf_out.write(msg).unwrap();
       
    83         self.flush();
       
    84     }
       
    85 
       
    86     fn flush(&mut self) {
       
    87         self.buf_out.write_to(&mut self.sock).unwrap();
       
    88         self.sock.flush();
       
    89     }
       
    90 
       
    91     fn readable(&mut self, poll: &Poll) -> io::Result<()> {
       
    92         self.buf_in.read_from(&mut self.sock)?;
       
    93         println!("Incoming buffer size: {}", self.buf_in.len());
       
    94         Ok(())
       
    95     }
       
    96 
       
    97     fn writable(&mut self, poll: &Poll) -> io::Result<()> {
       
    98         self.buf_out.write_to(&mut self.sock)?;
       
    99         Ok(())
    66     }
   100     }
    67 }
   101 }
    68 
   102 
    69 struct HWRoom {
   103 struct HWRoom {
    70     name: String
   104     name: String