gameServer2/src/server/network.rs
author alfadur
Fri, 06 Jul 2018 21:03:03 +0300
changeset 13477 f748a72432f2
parent 13419 81e0ed105f5d
child 13478 d79795acaa73
permissions -rw-r--r--
Implement greetings & fixed rooms
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},
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
     6
    collections::HashSet,
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
     7
    mem::swap
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     8
};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     9
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    10
use mio::{
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    11
    net::{TcpStream, TcpListener},
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    12
    Poll, PollOpt, Ready, Token
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    13
};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    14
use netbuf;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    15
use slab::Slab;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    16
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    17
use utils;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    18
use protocol::{ProtocolDecoder, messages::*};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    19
use super::{
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    20
    server::{HWServer},
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    21
    client::ClientId
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    22
};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    23
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    24
const MAX_BYTES_PER_READ: usize = 2048;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    25
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
    26
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    27
pub enum NetworkClientState {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    28
    Idle,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    29
    NeedsWrite,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    30
    NeedsRead,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    31
    Closed,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    32
}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    33
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    34
type NetworkResult<T> = io::Result<(T, NetworkClientState)>;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    35
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    36
pub struct NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    37
    id: ClientId,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    38
    socket: TcpStream,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    39
    peer_addr: SocketAddr,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    40
    decoder: ProtocolDecoder,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    41
    buf_out: netbuf::Buf
13119
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
impl NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    45
    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
    46
        NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    47
            id, socket, peer_addr,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    48
            decoder: ProtocolDecoder::new(),
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    49
            buf_out: netbuf::Buf::new()
13119
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
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    53
    pub fn read_messages(&mut self) -> NetworkResult<Vec<HWProtocolMessage>> {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    54
        let mut bytes_read = 0;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    55
        let result = loop {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    56
            match self.decoder.read_from(&mut self.socket) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    57
                Ok(bytes) => {
13416
cdf69667593b partial room implementation
alfadur
parents: 13415
diff changeset
    58
                    debug!("Client {}: read {} bytes", self.id, bytes);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    59
                    bytes_read += bytes;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    60
                    if bytes == 0 {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    61
                        let result = if bytes_read == 0 {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    62
                            info!("EOF for client {} ({})", self.id, self.peer_addr);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    63
                            (Vec::new(), NetworkClientState::Closed)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    64
                        } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    65
                            (self.decoder.extract_messages(), NetworkClientState::NeedsRead)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    66
                        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    67
                        break Ok(result);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    68
                    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    69
                    else if bytes_read >= MAX_BYTES_PER_READ {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    70
                        break Ok((self.decoder.extract_messages(), NetworkClientState::NeedsRead))
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    71
                    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    72
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    73
                Err(ref error) if error.kind() == ErrorKind::WouldBlock => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    74
                    let messages =  if bytes_read == 0 {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    75
                        Vec::new()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    76
                    } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    77
                        self.decoder.extract_messages()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    78
                    };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    79
                    break Ok((messages, NetworkClientState::Idle));
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    80
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    81
                Err(error) =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    82
                    break Err(error)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    83
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    84
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    85
        self.decoder.sweep();
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    86
        result
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    87
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    88
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    89
    pub fn flush(&mut self) -> NetworkResult<()> {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    90
        let result = loop {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    91
            match self.buf_out.write_to(&mut self.socket) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    92
                Ok(bytes) if self.buf_out.is_empty() || bytes == 0 =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    93
                    break Ok(((), NetworkClientState::Idle)),
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
    94
                Ok(_) => (),
13414
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,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   123
    clients: Slab<NetworkClient>,
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   124
    pending: HashSet<(ClientId, NetworkClientState)>,
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   125
    pending_cache: Vec<(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);
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   132
        let pending = HashSet::with_capacity(2 * clients_limit);
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   133
        let pending_cache = Vec::with_capacity(2 * clients_limit);
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   134
        NetworkLayer {listener, server, clients, pending, pending_cache}
13119
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
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   137
    pub fn register_server(&self, poll: &Poll) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   138
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   139
                      PollOpt::edge())
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
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   142
    fn deregister_client(&mut self, poll: &Poll, id: ClientId) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   143
        let mut client_exists = false;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   144
        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
   145
            poll.deregister(&client.socket)
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   146
                .ok().expect("could not deregister socket");
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   147
            info!("client {} ({}) removed", client.id, client.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   148
            client_exists = true;
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
        if client_exists {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   151
            self.clients.remove(id);
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
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   155
    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
   156
        poll.register(&client_socket, Token(id),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   157
                      Ready::readable() | Ready::writable(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   158
                      PollOpt::edge())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   159
            .ok().expect("could not register socket with event loop");
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   160
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   161
        let entry = self.clients.vacant_entry();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   162
        let client = NetworkClient::new(id, client_socket, addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   163
        info!("client {} ({}) added", client.id, client.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   164
        entry.insert(client);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   165
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   166
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   167
    fn flush_server_messages(&mut self) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   168
        debug!("{} pending server messages", self.server.output.len());
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   169
        for (clients, message) in self.server.output.drain(..) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   170
            debug!("Message {:?} to {:?}", message, clients);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   171
            let msg_string = message.to_raw_protocol();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   172
            for client_id in clients {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   173
                if let Some(client) = self.clients.get_mut(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   174
                    client.send_string(&msg_string);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   175
                    self.pending.insert((client_id, NetworkClientState::NeedsWrite));
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   176
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   177
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   178
        }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   179
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   180
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   181
    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
   182
        let (client_socket, addr) = self.listener.accept()?;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   183
        info!("Connected: {}", addr);
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
        let client_id = self.server.add_client();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   186
        self.register_client(poll, client_id, client_socket, addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   187
        self.flush_server_messages();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   188
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   189
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   190
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   191
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   192
    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
   193
        let addr = if let Some(ref mut client) = self.clients.get_mut(client_id) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   194
            client.peer_addr
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   195
        } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   196
            SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   197
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   198
        debug!("{}({}): {}", msg, addr, error);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   199
        self.client_error(poll, client_id)
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   200
    }
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
    pub fn client_readable(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   203
                           client_id: ClientId) -> io::Result<()> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   204
        let messages =
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   205
            if let Some(ref mut client) = self.clients.get_mut(client_id) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   206
                client.read_messages()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   207
            } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   208
                warn!("invalid readable client: {}", client_id);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   209
                Ok((Vec::new(), NetworkClientState::Idle))
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   210
            };
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   211
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   212
        match messages {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   213
            Ok((messages, state)) => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   214
                for message in messages {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   215
                    self.server.handle_msg(client_id, message);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   216
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   217
                match state {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   218
                    NetworkClientState::NeedsRead => {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   219
                        self.pending.insert((client_id, state));
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   220
                    },
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   221
                    NetworkClientState::Closed =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   222
                        self.client_error(&poll, client_id)?,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   223
                    _ => {}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   224
                };
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   225
            }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   226
            Err(e) => self.operation_failed(
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   227
                poll, client_id, e,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   228
                "Error while reading from client socket")?
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   229
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   230
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   231
        self.flush_server_messages();
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   232
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   233
        if !self.server.removed_clients.is_empty() {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   234
            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
   235
            for client_id in ids {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   236
                self.deregister_client(poll, client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   237
            }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   238
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   239
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   240
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   241
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   242
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   243
    pub fn client_writable(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   244
                           client_id: ClientId) -> io::Result<()> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   245
        let result =
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   246
            if let Some(ref mut client) = self.clients.get_mut(client_id) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   247
                client.flush()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   248
            } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   249
                warn!("invalid writable client: {}", client_id);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   250
                Ok(((), NetworkClientState::Idle))
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   251
            };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   252
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   253
        match result {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   254
            Ok(((), state)) if state == NetworkClientState::NeedsWrite => {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   255
                self.pending.insert((client_id, state));
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   256
            },
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   257
            Ok(_) => {}
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   258
            Err(e) => self.operation_failed(
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   259
                poll, client_id, e,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   260
                "Error while writing to client socket")?
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   261
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   262
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   263
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   264
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   265
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   266
    pub fn client_error(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   267
                        client_id: ClientId) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   268
        self.deregister_client(poll, client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   269
        self.server.client_lost(client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   270
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   271
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   272
    }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   273
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   274
    pub fn has_pending_operations(&self) -> bool {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   275
        !self.pending.is_empty()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   276
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   277
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   278
    pub fn on_idle(&mut self, poll: &Poll) -> io::Result<()> {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   279
        if self.has_pending_operations() {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   280
            let mut cache = Vec::new();
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   281
            swap(&mut cache, &mut self.pending_cache);
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   282
            cache.extend(self.pending.drain());
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   283
            for (id, state) in cache.drain(..) {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   284
                match state {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   285
                    NetworkClientState::NeedsRead =>
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   286
                        self.client_readable(poll, id)?,
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   287
                    NetworkClientState::NeedsWrite =>
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   288
                        self.client_writable(poll, id)?,
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   289
                    _ => {}
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   290
                }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   291
            }
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   292
            swap(&mut cache, &mut self.pending_cache);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   293
        }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   294
        Ok(())
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   295
    }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   296
}