gameServer2/src/server/network.rs
author Wuzzy <Wuzzy2@mail.ru>
Mon, 19 Mar 2018 16:07:08 +0100
changeset 13256 0cfa8f6a4e80
parent 13119 1e39b8749072
child 13414 28b314ad566d
permissions -rw-r--r--
Force proper map and theme reset when host delegates to you if you don't have the map or theme
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
     1
extern crate slab;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
     2
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
     3
use std::io::ErrorKind;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
     4
use mio::net::*;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
     5
use super::server::{HWServer, PendingMessage, Destination};
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
     6
use super::client::ClientId;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
     7
use slab::Slab;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
     8
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
     9
use mio::net::TcpStream;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    10
use mio::*;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    11
use std::io::Write;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    12
use std::io;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    13
use netbuf;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    14
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    15
use utils;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    16
use protocol::ProtocolDecoder;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    17
use protocol::messages::*;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    18
use std::net::SocketAddr;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    19
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    20
pub struct NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    21
    id: ClientId,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    22
    socket: TcpStream,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    23
    peer_addr: SocketAddr,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    24
    decoder: ProtocolDecoder,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    25
    buf_out: netbuf::Buf,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    26
    closed: bool
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    27
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    28
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    29
impl NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    30
    pub fn new(id: ClientId, socket: TcpStream, peer_addr: SocketAddr) -> NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    31
        NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    32
            id, socket, peer_addr,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    33
            decoder: ProtocolDecoder::new(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    34
            buf_out: netbuf::Buf::new(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    35
            closed: false
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    36
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    37
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    38
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    39
    pub fn send_raw_msg(&mut self, msg: &[u8]) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    40
        self.buf_out.write(msg).unwrap();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    41
        self.flush();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    42
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    43
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    44
    pub fn send_string(&mut self, msg: &String) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    45
        self.send_raw_msg(&msg.as_bytes());
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    46
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    47
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    48
    pub fn send_msg(&mut self, msg: HWServerMessage) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    49
        self.send_string(&msg.to_raw_protocol());
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    50
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    51
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    52
    fn flush(&mut self) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    53
        self.buf_out.write_to(&mut self.socket).unwrap();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    54
        self.socket.flush().unwrap();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    55
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    56
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    57
    pub fn read_messages(&mut self) -> io::Result<Vec<HWProtocolMessage>> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    58
        let bytes_read = self.decoder.read_from(&mut self.socket)?;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    59
        debug!("Read {} bytes", bytes_read);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    60
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    61
        if bytes_read == 0 {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    62
            self.closed = true;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    63
            info!("EOF for client {} ({})", self.id, self.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    64
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    65
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    66
        Ok(self.decoder.extract_messages())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    67
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    68
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    69
    pub fn write_messages(&mut self) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    70
        self.buf_out.write_to(&mut self.socket)?;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    71
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    72
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    73
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    74
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    75
pub struct NetworkLayer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    76
    listener: TcpListener,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    77
    server: HWServer,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    78
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    79
    clients: Slab<NetworkClient>
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    80
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    81
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    82
impl NetworkLayer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    83
    pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> NetworkLayer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    84
        let server = HWServer::new(clients_limit, rooms_limit);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    85
        let clients = Slab::with_capacity(clients_limit);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    86
        NetworkLayer {listener, server, clients}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    87
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    88
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    89
    pub fn register_server(&self, poll: &Poll) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    90
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    91
                      PollOpt::edge())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    92
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    93
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    94
    fn deregister_client(&mut self, poll: &Poll, id: ClientId) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    95
        let mut client_exists = false;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    96
        if let Some(ref client) = self.clients.get_mut(id) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    97
            poll.deregister(&client.socket)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    98
                .ok().expect("could not deregister socket");
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    99
            info!("client {} ({}) removed", client.id, client.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   100
            client_exists = true;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   101
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   102
        if client_exists {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   103
            self.clients.remove(id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   104
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   105
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   106
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   107
    fn register_client(&mut self, poll: &Poll, id: ClientId, client_socket: TcpStream, addr: SocketAddr) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   108
        poll.register(&client_socket, Token(id),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   109
                      Ready::readable() | Ready::writable(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   110
                      PollOpt::edge())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   111
            .ok().expect("could not register socket with event loop");
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   112
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   113
        let entry = self.clients.vacant_entry();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   114
        let client = NetworkClient::new(id, client_socket, addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   115
        info!("client {} ({}) added", client.id, client.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   116
        entry.insert(client);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   117
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   118
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   119
    pub fn accept_client(&mut self, poll: &Poll) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   120
        let (client_socket, addr) = self.listener.accept()?;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   121
        info!("Connected: {}", addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   122
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   123
        let client_id = self.server.add_client();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   124
        self.register_client(poll, client_id, client_socket, addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   125
        self.flush_server_messages();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   126
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   127
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   128
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   129
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   130
    fn flush_server_messages(&mut self) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   131
        for PendingMessage(destination, msg) in self.server.output.drain(..) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   132
            match destination {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   133
                Destination::ToSelf(id)  => {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   134
                    if let Some(ref mut client) = self.clients.get_mut(id) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   135
                        client.send_msg(msg)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   136
                    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   137
                }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   138
                Destination::ToOthers(id) => {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   139
                    let msg_string = msg.to_raw_protocol();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   140
                    for item in self.clients.iter_mut() {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   141
                        if item.0 != id {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   142
                            item.1.send_string(&msg_string)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   143
                        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   144
                    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   145
                }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   146
            }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   147
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   148
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   149
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   150
    pub fn client_readable(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   151
                           client_id: ClientId) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   152
        let mut client_lost = false;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   153
        let messages;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   154
        if let Some(ref mut client) = self.clients.get_mut(client_id) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   155
            messages = match client.read_messages() {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   156
                Ok(messages) => Some(messages),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   157
                Err(ref error) if error.kind() == ErrorKind::WouldBlock => None,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   158
                Err(error) => return Err(error)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   159
            };
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   160
            if client.closed {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   161
                client_lost = true;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   162
            }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   163
        } else {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   164
            warn!("invalid readable client: {}", client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   165
            messages = None;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   166
        };
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   167
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   168
        if client_lost {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   169
            self.client_error(&poll, client_id)?;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   170
        } else if let Some(msg) = messages {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   171
            for message in msg {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   172
                self.server.handle_msg(client_id, message);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   173
            }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   174
            self.flush_server_messages();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   175
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   176
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   177
        if !self.server.removed_clients.is_empty() {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   178
            let ids = self.server.removed_clients.to_vec();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   179
            self.server.removed_clients.clear();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   180
            for client_id in ids {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   181
                self.deregister_client(poll, client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   182
            }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   183
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   184
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   185
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   186
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   187
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   188
    pub fn client_writable(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   189
                           client_id: ClientId) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   190
        if let Some(ref mut client) = self.clients.get_mut(client_id) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   191
            match client.write_messages() {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   192
                Ok(_) => (),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   193
                Err(ref error) if error.kind() == ErrorKind::WouldBlock => (),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   194
                Err(error) => return Err(error)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   195
            }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   196
        } else {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   197
            warn!("invalid writable client: {}", client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   198
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   199
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   200
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   201
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   202
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   203
    pub fn client_error(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   204
                        client_id: ClientId) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   205
        self.deregister_client(poll, client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   206
        self.server.client_lost(client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   207
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   208
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   209
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   210
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   211