gameServer2/src/server/network.rs
author alfadur
Thu, 14 Jun 2018 12:31:15 -0400
changeset 13414 28b314ad566d
parent 13119 1e39b8749072
child 13415 0eedc17055a0
permissions -rw-r--r--
handle edge polling properly
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
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     3
use std::{
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     4
    io, io::{Error, ErrorKind, Write},
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     5
    net::{SocketAddr, IpAddr, Ipv4Addr},
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     6
    collections::VecDeque
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     7
};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     8
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     9
use mio::{
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    10
    net::{TcpStream, TcpListener},
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    11
    Poll, PollOpt, Ready, Token
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    12
};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    13
use netbuf;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    14
use slab::Slab;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    15
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    16
use utils;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    17
use protocol::{ProtocolDecoder, messages::*};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    18
use super::{
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    19
    server::{HWServer, PendingMessage, Destination},
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    20
    client::ClientId
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    21
};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    22
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    23
const MAX_BYTES_PER_READ: usize = 2048;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    24
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    25
#[derive(PartialEq, Copy, Clone)]
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    26
pub enum NetworkClientState {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    27
    Idle,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    28
    NeedsWrite,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    29
    NeedsRead,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    30
    Closed,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    31
}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    32
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    33
type NetworkResult<T> = io::Result<(T, NetworkClientState)>;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    34
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    35
pub struct NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    36
    id: ClientId,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    37
    socket: TcpStream,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    38
    peer_addr: SocketAddr,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    39
    decoder: ProtocolDecoder,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    40
    buf_out: netbuf::Buf
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    41
}
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
impl NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    44
    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
    45
        NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    46
            id, socket, peer_addr,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    47
            decoder: ProtocolDecoder::new(),
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    48
            buf_out: netbuf::Buf::new()
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    49
        }
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
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    52
    pub fn read_messages(&mut self) -> NetworkResult<Vec<HWProtocolMessage>> {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    53
        let mut bytes_read = 0;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    54
        let result = loop {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    55
            match self.decoder.read_from(&mut self.socket) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    56
                Ok(bytes) => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    57
                    debug!("Read {} bytes", bytes);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    58
                    bytes_read += bytes;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    59
                    if bytes == 0 {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    60
                        let result = if bytes_read == 0 {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    61
                            info!("EOF for client {} ({})", self.id, self.peer_addr);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    62
                            (Vec::new(), NetworkClientState::Closed)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    63
                        } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    64
                            (self.decoder.extract_messages(), NetworkClientState::NeedsRead)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    65
                        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    66
                        break Ok(result);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    67
                    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    68
                    else if bytes_read >= MAX_BYTES_PER_READ {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    69
                        break Ok((self.decoder.extract_messages(), NetworkClientState::NeedsRead))
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    70
                    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    71
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    72
                Err(ref error) if error.kind() == ErrorKind::WouldBlock => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    73
                    let messages =  if bytes_read == 0 {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    74
                        Vec::new()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    75
                    } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    76
                        self.decoder.extract_messages()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    77
                    };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    78
                    break Ok((messages, NetworkClientState::Idle));
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    79
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    80
                Err(error) =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    81
                    break Err(error)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    82
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    83
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    84
        self.decoder.sweep();
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    85
        result
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    86
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    87
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    88
    pub fn flush(&mut self) -> NetworkResult<()> {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    89
        let result = loop {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    90
            match self.buf_out.write_to(&mut self.socket) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    91
                Ok(bytes) if self.buf_out.is_empty() || bytes == 0 =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    92
                    break Ok(((), NetworkClientState::Idle)),
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    93
                Ok(bytes) =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    94
                    (),
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    95
                Err(ref error) if error.kind() == ErrorKind::Interrupted
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    96
                    || error.kind() == ErrorKind::WouldBlock => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    97
                    break Ok(((), NetworkClientState::NeedsWrite));
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    98
                },
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    99
                Err(error) =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   100
                    break Err(error)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   101
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   102
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   103
        self.socket.flush()?;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   104
        result
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   105
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   106
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   107
    pub fn send_raw_msg(&mut self, msg: &[u8]) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   108
        self.buf_out.write(msg).unwrap();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   109
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   110
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   111
    pub fn send_string(&mut self, msg: &String) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   112
        self.send_raw_msg(&msg.as_bytes());
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   113
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   114
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   115
    pub fn send_msg(&mut self, msg: HWServerMessage) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   116
        self.send_string(&msg.to_raw_protocol());
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
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   120
pub struct NetworkLayer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   121
    listener: TcpListener,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   122
    server: HWServer,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   123
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   124
    clients: Slab<NetworkClient>,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   125
    pending: VecDeque<(ClientId, NetworkClientState)>
13119
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
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   128
impl NetworkLayer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   129
    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
   130
        let server = HWServer::new(clients_limit, rooms_limit);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   131
        let clients = Slab::with_capacity(clients_limit);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   132
        let pending = VecDeque::with_capacity(clients_limit);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   133
        NetworkLayer {listener, server, clients, pending}
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   134
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   135
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   136
    pub fn register_server(&self, poll: &Poll) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   137
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   138
                      PollOpt::edge())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   139
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   140
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   141
    fn deregister_client(&mut self, poll: &Poll, id: ClientId) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   142
        let mut client_exists = false;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   143
        if let Some(ref client) = self.clients.get(id) {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   144
            poll.deregister(&client.socket)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   145
                .ok().expect("could not deregister socket");
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   146
            info!("client {} ({}) removed", client.id, client.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   147
            client_exists = true;
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
        if client_exists {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   150
            self.clients.remove(id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   151
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   152
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   153
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   154
    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
   155
        poll.register(&client_socket, Token(id),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   156
                      Ready::readable() | Ready::writable(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   157
                      PollOpt::edge())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   158
            .ok().expect("could not register socket with event loop");
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
        let entry = self.clients.vacant_entry();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   161
        let client = NetworkClient::new(id, client_socket, addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   162
        info!("client {} ({}) added", client.id, client.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   163
        entry.insert(client);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   164
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   165
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   166
    fn flush_server_messages(&mut self) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   167
        debug!("{} pending server messages", self.server.output.len());
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   168
        for PendingMessage(destination, msg) in self.server.output.drain(..) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   169
            match destination {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   170
                Destination::ToSelf(id)  => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   171
                    if let Some(ref mut client) = self.clients.get_mut(id) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   172
                        client.send_msg(msg);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   173
                        self.pending.push_back((id, NetworkClientState::NeedsWrite));
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   174
                    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   175
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   176
                Destination::ToOthers(id) => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   177
                    let msg_string = msg.to_raw_protocol();
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   178
                    for (client_id, client) in self.clients.iter_mut() {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   179
                        if client_id != id {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   180
                            client.send_string(&msg_string);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   181
                            self.pending.push_back((client_id, NetworkClientState::NeedsWrite));
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   182
                        }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   183
                    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   184
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   185
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   186
        }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   187
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   188
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   189
    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
   190
        let (client_socket, addr) = self.listener.accept()?;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   191
        info!("Connected: {}", addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   192
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   193
        let client_id = self.server.add_client();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   194
        self.register_client(poll, client_id, client_socket, addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   195
        self.flush_server_messages();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   196
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   197
        Ok(())
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
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   200
    fn operation_failed(&mut self, poll: &Poll, client_id: ClientId, error: Error, msg: &str) -> io::Result<()> {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   201
        let addr = if let Some(ref mut client) = self.clients.get_mut(client_id) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   202
            client.peer_addr
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   203
        } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   204
            SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   205
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   206
        debug!("{}({}): {}", msg, addr, error);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   207
        self.client_error(poll, client_id)
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   208
    }
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
    pub fn client_readable(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   211
                           client_id: ClientId) -> io::Result<()> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   212
        let messages =
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   213
            if let Some(ref mut client) = self.clients.get_mut(client_id) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   214
                client.read_messages()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   215
            } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   216
                warn!("invalid readable client: {}", client_id);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   217
                Ok((Vec::new(), NetworkClientState::Idle))
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   218
            };
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   219
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   220
        match messages {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   221
            Ok((messages, state)) => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   222
                for message in messages {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   223
                    self.server.handle_msg(client_id, message);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   224
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   225
                match state {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   226
                    NetworkClientState::NeedsRead =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   227
                        self.pending.push_back((client_id, state)),
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   228
                    NetworkClientState::Closed =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   229
                        self.client_error(&poll, client_id)?,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   230
                    _ => {}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   231
                };
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   232
            }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   233
            Err(e) => self.operation_failed(
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   234
                poll, client_id, e,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   235
                "Error while reading from client socket")?
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   236
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   237
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   238
        self.flush_server_messages();
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   239
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   240
        if !self.server.removed_clients.is_empty() {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   241
            let ids: Vec<_> = self.server.removed_clients.drain(..).collect();
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   242
            for client_id in ids {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   243
                self.deregister_client(poll, client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   244
            }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   245
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   246
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   247
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   248
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   249
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   250
    pub fn client_writable(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   251
                           client_id: ClientId) -> io::Result<()> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   252
        let result =
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   253
            if let Some(ref mut client) = self.clients.get_mut(client_id) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   254
                client.flush()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   255
            } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   256
                warn!("invalid writable client: {}", client_id);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   257
                Ok(((), NetworkClientState::Idle))
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   258
            };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   259
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   260
        match result {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   261
            Ok(((), state)) if state == NetworkClientState::NeedsWrite =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   262
                self.pending.push_back((client_id, state)),
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   263
            Ok(_) =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   264
                {}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   265
            Err(e) => self.operation_failed(
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   266
                poll, client_id, e,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   267
                "Error while writing to client socket")?
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   268
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   269
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   270
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   271
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   272
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   273
    pub fn client_error(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   274
                        client_id: ClientId) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   275
        self.deregister_client(poll, client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   276
        self.server.client_lost(client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   277
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   278
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   279
    }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   280
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   281
    pub fn has_pending_operations(&self) -> bool {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   282
        !self.pending.is_empty()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   283
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   284
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   285
    pub fn on_idle(&mut self, poll: &Poll) -> io::Result<()> {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   286
        while let Some((id, state)) = self.pending.pop_front() {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   287
            match state {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   288
                NetworkClientState::NeedsRead =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   289
                    self.client_readable(poll, id)?,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   290
                NetworkClientState::NeedsWrite =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   291
                    self.client_writable(poll, id)?,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   292
                _ => {}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   293
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   294
        }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   295
        Ok(())
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   296
    }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   297
}