gameServer2/src/server/network.rs
author alfadur
Thu, 06 Sep 2018 23:12:32 +0300
changeset 13799 c8fd12db6215
parent 13666 09f4a30e50cc
child 13802 24fe56d3c6a2
permissions -rw-r--r--
Add TLS support
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::{
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
     4
    io, io::{Error, ErrorKind, Read, Write},
13414
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,
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13419
diff changeset
     7
    mem::{swap, replace}
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
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
    17
use crate::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
    18
    utils,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
    19
    protocol::{ProtocolDecoder, messages::*}
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
    20
};
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    21
use super::{
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    22
    server::{HWServer},
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13419
diff changeset
    23
    coretypes::ClientId
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    24
};
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    25
#[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    26
use openssl::{
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    27
    ssl::{
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    28
        SslMethod, SslContext, Ssl, SslContextBuilder,
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    29
        SslVerifyMode, SslFiletype, SslOptions,
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    30
        SslStreamBuilder, HandshakeError, MidHandshakeSslStream, SslStream
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    31
    },
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    32
    error::ErrorStack
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    33
};
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    34
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    35
const MAX_BYTES_PER_READ: usize = 2048;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    36
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
    37
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    38
pub enum NetworkClientState {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    39
    Idle,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    40
    NeedsWrite,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    41
    NeedsRead,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    42
    Closed,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    43
}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    44
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    45
type NetworkResult<T> = io::Result<(T, NetworkClientState)>;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    46
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    47
#[cfg(not(feature = "tls-connections"))]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    48
pub enum ClientSocket {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    49
    Plain(TcpStream)
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    50
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    51
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    52
#[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    53
pub enum ClientSocket {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    54
    SslHandshake(Option<MidHandshakeSslStream<TcpStream>>),
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    55
    SslStream(SslStream<TcpStream>)
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    56
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    57
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    58
impl ClientSocket {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    59
    fn inner(&self) -> &TcpStream {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    60
        #[cfg(not(feature = "tls-connections"))]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    61
        match self {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    62
            ClientSocket::Plain(stream) => stream,
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    63
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    64
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    65
        #[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    66
        match self {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    67
            ClientSocket::SslHandshake(Some(builder)) => builder.get_ref(),
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    68
            ClientSocket::SslHandshake(None) => unreachable!(),
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    69
            ClientSocket::SslStream(ssl_stream) => ssl_stream.get_ref()
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    70
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    71
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    72
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    73
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    74
pub struct NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    75
    id: ClientId,
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    76
    socket: ClientSocket,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    77
    peer_addr: SocketAddr,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    78
    decoder: ProtocolDecoder,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    79
    buf_out: netbuf::Buf
13119
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 NetworkClient {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    83
    pub fn new(id: ClientId, socket: ClientSocket, peer_addr: SocketAddr) -> NetworkClient {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    84
        NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    85
            id, socket, peer_addr,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    86
            decoder: ProtocolDecoder::new(),
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    87
            buf_out: netbuf::Buf::new()
13119
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
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    90
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    91
    fn read_impl<R: Read>(decoder: &mut ProtocolDecoder, source: &mut R,
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    92
                          id: ClientId, addr: &SocketAddr) -> NetworkResult<Vec<HWProtocolMessage>> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    93
        let mut bytes_read = 0;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    94
        let result = loop {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    95
            match decoder.read_from(source) {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    96
                Ok(bytes) => {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    97
                    debug!("Client {}: read {} bytes", id, bytes);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    98
                    bytes_read += bytes;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    99
                    if bytes == 0 {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   100
                        let result = if bytes_read == 0 {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   101
                            info!("EOF for client {} ({})", id, addr);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   102
                            (Vec::new(), NetworkClientState::Closed)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   103
                        } else {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   104
                            (decoder.extract_messages(), NetworkClientState::NeedsRead)
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   105
                        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   106
                        break Ok(result);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   107
                    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   108
                    else if bytes_read >= MAX_BYTES_PER_READ {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   109
                        break Ok((decoder.extract_messages(), NetworkClientState::NeedsRead))
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   110
                    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   111
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   112
                Err(ref error) if error.kind() == ErrorKind::WouldBlock => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   113
                    let messages =  if bytes_read == 0 {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   114
                        Vec::new()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   115
                    } else {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   116
                        decoder.extract_messages()
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   117
                    };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   118
                    break Ok((messages, NetworkClientState::Idle));
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   119
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   120
                Err(error) =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   121
                    break Err(error)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   122
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   123
        };
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   124
        decoder.sweep();
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   125
        result
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   126
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   127
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   128
    pub fn read(&mut self) -> NetworkResult<Vec<HWProtocolMessage>> {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   129
        #[cfg(not(feature = "tls-connections"))]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   130
        match self.socket {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   131
            ClientSocket::Plain(ref mut stream) =>
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   132
                NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr),
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   133
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   134
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   135
        #[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   136
        match self.socket {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   137
            ClientSocket::SslHandshake(ref mut handshake_opt) => {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   138
                let mut handshake = std::mem::replace(handshake_opt, None).unwrap();
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   139
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   140
                match handshake.handshake() {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   141
                    Ok(stream) => {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   142
                        debug!("TLS handshake with {} ({}) completed", self.id, self.peer_addr);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   143
                        self.socket = ClientSocket::SslStream(stream);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   144
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   145
                        Ok((Vec::new(), NetworkClientState::Idle))
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   146
                    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   147
                    Err(HandshakeError::WouldBlock(new_handshake)) => {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   148
                        *handshake_opt = Some(new_handshake);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   149
                        Ok((Vec::new(), NetworkClientState::Idle))
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   150
                    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   151
                    Err(e) => {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   152
                        debug!("TLS handshake with {} ({}) failed", self.id, self.peer_addr);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   153
                        Err(Error::new(ErrorKind::Other, "Connection failure"))
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   154
                    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   155
                }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   156
            },
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   157
            ClientSocket::SslStream(ref mut stream) =>
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   158
                NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr)
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   159
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   160
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   161
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   162
    fn write_impl<W: Write>(buf_out: &mut netbuf::Buf, destination: &mut W) -> NetworkResult<()> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   163
        let result = loop {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   164
            match buf_out.write_to(destination) {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   165
                Ok(bytes) if buf_out.is_empty() || bytes == 0 =>
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   166
                    break Ok(((), NetworkClientState::Idle)),
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   167
                Ok(_) => (),
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   168
                Err(ref error) if error.kind() == ErrorKind::Interrupted
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   169
                    || error.kind() == ErrorKind::WouldBlock => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   170
                    break Ok(((), NetworkClientState::NeedsWrite));
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   171
                },
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   172
                Err(error) =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   173
                    break Err(error)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   174
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   175
        };
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   176
        result
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   177
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   178
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   179
    pub fn write(&mut self) -> NetworkResult<()> {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   180
        let result = {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   181
            #[cfg(not(feature = "tls-connections"))]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   182
            match self.socket {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   183
                ClientSocket::Plain(ref mut stream) =>
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   184
                    NetworkClient::write_impl(&mut self.buf_out, stream)
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   185
            }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   186
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   187
            #[cfg(feature = "tls-connections")] {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   188
                match self.socket {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   189
                    ClientSocket::SslHandshake(_) =>
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   190
                        Ok(((), NetworkClientState::Idle)),
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   191
                    ClientSocket::SslStream(ref mut stream) =>
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   192
                        NetworkClient::write_impl(&mut self.buf_out, stream)
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   193
                }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   194
            }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   195
        };
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   196
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   197
        self.socket.inner().flush()?;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   198
        result
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   199
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   200
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   201
    pub fn send_raw_msg(&mut self, msg: &[u8]) {
13524
5359ff75da3a indulge clippy
alfadur
parents: 13478
diff changeset
   202
        self.buf_out.write_all(msg).unwrap();
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   203
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   204
13524
5359ff75da3a indulge clippy
alfadur
parents: 13478
diff changeset
   205
    pub fn send_string(&mut self, msg: &str) {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   206
        self.send_raw_msg(&msg.as_bytes());
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
13524
5359ff75da3a indulge clippy
alfadur
parents: 13478
diff changeset
   209
    pub fn send_msg(&mut self, msg: &HWServerMessage) {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   210
        self.send_string(&msg.to_raw_protocol());
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   211
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   212
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   213
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   214
#[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   215
struct ServerSsl {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   216
    context: SslContext
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   217
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   218
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   219
pub struct NetworkLayer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   220
    listener: TcpListener,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   221
    server: HWServer,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   222
    clients: Slab<NetworkClient>,
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   223
    pending: HashSet<(ClientId, NetworkClientState)>,
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   224
    pending_cache: Vec<(ClientId, NetworkClientState)>,
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   225
    #[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   226
    ssl: ServerSsl
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   227
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   228
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   229
impl NetworkLayer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   230
    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
   231
        let server = HWServer::new(clients_limit, rooms_limit);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   232
        let clients = Slab::with_capacity(clients_limit);
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   233
        let pending = HashSet::with_capacity(2 * clients_limit);
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   234
        let pending_cache = Vec::with_capacity(2 * clients_limit);
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   235
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   236
        NetworkLayer {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   237
            listener, server, clients, pending, pending_cache,
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   238
            #[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   239
            ssl: NetworkLayer::create_ssl_context()
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   240
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   241
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   242
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   243
    #[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   244
    fn create_ssl_context() -> ServerSsl {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   245
        let mut builder = SslContextBuilder::new(SslMethod::tls()).unwrap();
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   246
        builder.set_verify(SslVerifyMode::NONE);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   247
        builder.set_read_ahead(true);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   248
        builder.set_certificate_file("ssl/cert.pem", SslFiletype::PEM).unwrap();
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   249
        builder.set_private_key_file("ssl/key.pem", SslFiletype::PEM).unwrap();
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   250
        builder.set_options(SslOptions::NO_COMPRESSION);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   251
        builder.set_cipher_list("DEFAULT:!LOW:!RC4:!EXP").unwrap();
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   252
        ServerSsl { context: builder.build() }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   253
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   254
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   255
    pub fn register_server(&self, poll: &Poll) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   256
        poll.register(&self.listener, utils::SERVER, Ready::readable(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   257
                      PollOpt::edge())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   258
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   259
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   260
    fn deregister_client(&mut self, poll: &Poll, id: ClientId) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   261
        let mut client_exists = false;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   262
        if let Some(ref client) = self.clients.get(id) {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   263
            poll.deregister(client.socket.inner())
13524
5359ff75da3a indulge clippy
alfadur
parents: 13478
diff changeset
   264
                .expect("could not deregister socket");
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   265
            info!("client {} ({}) removed", client.id, client.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   266
            client_exists = true;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   267
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   268
        if client_exists {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   269
            self.clients.remove(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
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   272
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   273
    fn register_client(&mut self, poll: &Poll, id: ClientId, client_socket: ClientSocket, addr: SocketAddr) {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   274
        poll.register(client_socket.inner(), Token(id),
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   275
                      Ready::readable() | Ready::writable(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   276
                      PollOpt::edge())
13524
5359ff75da3a indulge clippy
alfadur
parents: 13478
diff changeset
   277
            .expect("could not register socket with event loop");
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   278
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   279
        let entry = self.clients.vacant_entry();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   280
        let client = NetworkClient::new(id, client_socket, addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   281
        info!("client {} ({}) added", client.id, client.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   282
        entry.insert(client);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   283
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   284
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   285
    fn flush_server_messages(&mut self) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   286
        debug!("{} pending server messages", self.server.output.len());
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   287
        for (clients, message) in self.server.output.drain(..) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   288
            debug!("Message {:?} to {:?}", message, clients);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   289
            let msg_string = message.to_raw_protocol();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   290
            for client_id in clients {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   291
                if let Some(client) = self.clients.get_mut(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   292
                    client.send_string(&msg_string);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   293
                    self.pending.insert((client_id, NetworkClientState::NeedsWrite));
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   294
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   295
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   296
        }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   297
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   298
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   299
    fn create_client_socket(&self, socket: TcpStream) -> io::Result<ClientSocket> {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   300
        #[cfg(not(feature = "tls-connections"))] {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   301
            Ok(ClientSocket::Plain(socket))
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   302
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   303
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   304
        #[cfg(feature = "tls-connections")] {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   305
            let ssl = Ssl::new(&self.ssl.context).unwrap();
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   306
            let mut builder = SslStreamBuilder::new(ssl, socket);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   307
            builder.set_accept_state();
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   308
            match builder.handshake() {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   309
                Ok(stream) =>
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   310
                    Ok(ClientSocket::SslStream(stream)),
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   311
                Err(HandshakeError::WouldBlock(stream)) =>
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   312
                    Ok(ClientSocket::SslHandshake(Some(stream))),
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   313
                Err(e) => {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   314
                    debug!("OpenSSL handshake failed: {}", e);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   315
                    Err(Error::new(ErrorKind::Other, "Connection failure"))
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   316
                }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   317
            }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   318
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   319
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   320
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   321
    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
   322
        let (client_socket, addr) = self.listener.accept()?;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   323
        info!("Connected: {}", addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   324
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   325
        let client_id = self.server.add_client();
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   326
        self.register_client(poll, client_id, self.create_client_socket(client_socket)?, addr);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   327
        self.flush_server_messages();
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   328
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   329
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   330
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   331
13524
5359ff75da3a indulge clippy
alfadur
parents: 13478
diff changeset
   332
    fn operation_failed(&mut self, poll: &Poll, client_id: ClientId, error: &Error, msg: &str) -> io::Result<()> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   333
        let addr = if let Some(ref mut client) = self.clients.get_mut(client_id) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   334
            client.peer_addr
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   335
        } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   336
            SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   337
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   338
        debug!("{}({}): {}", msg, addr, error);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   339
        self.client_error(poll, client_id)
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   340
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   341
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   342
    pub fn client_readable(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   343
                           client_id: ClientId) -> io::Result<()> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   344
        let messages =
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   345
            if let Some(ref mut client) = self.clients.get_mut(client_id) {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   346
                client.read()
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   347
            } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   348
                warn!("invalid readable client: {}", client_id);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   349
                Ok((Vec::new(), NetworkClientState::Idle))
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   350
            };
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   351
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   352
        match messages {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   353
            Ok((messages, state)) => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   354
                for message in messages {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   355
                    self.server.handle_msg(client_id, message);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   356
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   357
                match state {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   358
                    NetworkClientState::NeedsRead => {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   359
                        self.pending.insert((client_id, state));
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   360
                    },
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   361
                    NetworkClientState::Closed =>
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   362
                        self.client_error(&poll, client_id)?,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   363
                    _ => {}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   364
                };
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   365
            }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   366
            Err(e) => self.operation_failed(
13524
5359ff75da3a indulge clippy
alfadur
parents: 13478
diff changeset
   367
                poll, client_id, &e,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   368
                "Error while reading from client socket")?
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   369
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   370
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   371
        self.flush_server_messages();
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   372
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   373
        if !self.server.removed_clients.is_empty() {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   374
            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
   375
            for client_id in ids {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   376
                self.deregister_client(poll, client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   377
            }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   378
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   379
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   380
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   381
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   382
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   383
    pub fn client_writable(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   384
                           client_id: ClientId) -> io::Result<()> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   385
        let result =
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   386
            if let Some(ref mut client) = self.clients.get_mut(client_id) {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   387
                client.write()
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   388
            } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   389
                warn!("invalid writable client: {}", client_id);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   390
                Ok(((), NetworkClientState::Idle))
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   391
            };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   392
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   393
        match result {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   394
            Ok(((), state)) if state == NetworkClientState::NeedsWrite => {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   395
                self.pending.insert((client_id, state));
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   396
            },
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   397
            Ok(_) => {}
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   398
            Err(e) => self.operation_failed(
13524
5359ff75da3a indulge clippy
alfadur
parents: 13478
diff changeset
   399
                poll, client_id, &e,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   400
                "Error while writing to client socket")?
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   401
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   402
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   403
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   404
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   405
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   406
    pub fn client_error(&mut self, poll: &Poll,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   407
                        client_id: ClientId) -> io::Result<()> {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   408
        self.deregister_client(poll, client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   409
        self.server.client_lost(client_id);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   410
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   411
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   412
    }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   413
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   414
    pub fn has_pending_operations(&self) -> bool {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   415
        !self.pending.is_empty()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   416
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   417
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   418
    pub fn on_idle(&mut self, poll: &Poll) -> io::Result<()> {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   419
        if self.has_pending_operations() {
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13419
diff changeset
   420
            let mut cache = replace(&mut self.pending_cache, Vec::new());
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   421
            cache.extend(self.pending.drain());
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   422
            for (id, state) in cache.drain(..) {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   423
                match state {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   424
                    NetworkClientState::NeedsRead =>
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   425
                        self.client_readable(poll, id)?,
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   426
                    NetworkClientState::NeedsWrite =>
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   427
                        self.client_writable(poll, id)?,
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   428
                    _ => {}
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   429
                }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   430
            }
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   431
            swap(&mut cache, &mut self.pending_cache);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   432
        }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   433
        Ok(())
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   434
    }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   435
}