rust/hedgewars-server/src/server/network.rs
author alfadur
Fri, 12 Apr 2019 22:36:54 +0300
changeset 14816 add191d825f4
parent 14801 65861ba8b4e8
child 14817 f5d43f007970
permissions -rw-r--r--
add parser error handling
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::{
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
     4
    collections::HashSet,
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     5
    io,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     6
    io::{Error, ErrorKind, Read, Write},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     7
    mem::{replace, swap},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     8
    net::{IpAddr, Ipv4Addr, SocketAddr},
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     9
};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    10
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    11
use log::*;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    12
use mio::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    13
    net::{TcpListener, TcpStream},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    14
    Poll, PollOpt, Ready, Token,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    15
};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    16
use netbuf;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    17
use slab::Slab;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    18
14801
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14800
diff changeset
    19
use super::{core::HWServer, coretypes::ClientId, handlers};
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13500
diff changeset
    20
use crate::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    21
    protocol::{messages::*, ProtocolDecoder},
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13500
diff changeset
    22
    utils,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    23
};
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
    24
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
    25
#[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
    26
use super::io::{IOThread, RequestId};
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
    27
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
    28
use crate::server::handlers::{IoResult, IoTask};
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    29
#[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    30
use openssl::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    31
    error::ErrorStack,
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    32
    ssl::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    33
        HandshakeError, MidHandshakeSslStream, Ssl, SslContext, SslContextBuilder, SslFiletype,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    34
        SslMethod, SslOptions, SslStream, SslStreamBuilder, SslVerifyMode,
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    35
    },
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    36
};
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    37
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    38
const MAX_BYTES_PER_READ: usize = 2048;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    39
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
    40
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    41
pub enum NetworkClientState {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    42
    Idle,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    43
    NeedsWrite,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    44
    NeedsRead,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    45
    Closed,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    46
}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    47
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    48
type NetworkResult<T> = io::Result<(T, NetworkClientState)>;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    49
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    50
#[cfg(not(feature = "tls-connections"))]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    51
pub enum ClientSocket {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    52
    Plain(TcpStream),
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    53
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    54
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    55
#[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    56
pub enum ClientSocket {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    57
    SslHandshake(Option<MidHandshakeSslStream<TcpStream>>),
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    58
    SslStream(SslStream<TcpStream>),
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    59
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    60
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    61
impl ClientSocket {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    62
    fn inner(&self) -> &TcpStream {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    63
        #[cfg(not(feature = "tls-connections"))]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    64
        match self {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    65
            ClientSocket::Plain(stream) => stream,
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    66
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    67
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    68
        #[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    69
        match self {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    70
            ClientSocket::SslHandshake(Some(builder)) => builder.get_ref(),
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    71
            ClientSocket::SslHandshake(None) => unreachable!(),
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    72
            ClientSocket::SslStream(ssl_stream) => ssl_stream.get_ref(),
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    73
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    74
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    75
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    76
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    77
pub struct NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    78
    id: ClientId,
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    79
    socket: ClientSocket,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    80
    peer_addr: SocketAddr,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    81
    decoder: ProtocolDecoder,
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    82
    buf_out: netbuf::Buf,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    83
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    84
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    85
impl NetworkClient {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    86
    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
    87
        NetworkClient {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    88
            id,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    89
            socket,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    90
            peer_addr,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    91
            decoder: ProtocolDecoder::new(),
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    92
            buf_out: netbuf::Buf::new(),
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    93
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    94
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    95
13776
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
    96
    #[cfg(feature = "tls-connections")]
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    97
    fn handshake_impl(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    98
        &mut self,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    99
        handshake: MidHandshakeSslStream<TcpStream>,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   100
    ) -> io::Result<NetworkClientState> {
13776
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   101
        match handshake.handshake() {
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   102
            Ok(stream) => {
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   103
                self.socket = ClientSocket::SslStream(stream);
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   104
                debug!(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   105
                    "TLS handshake with {} ({}) completed",
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   106
                    self.id, self.peer_addr
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   107
                );
13776
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   108
                Ok(NetworkClientState::Idle)
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   109
            }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   110
            Err(HandshakeError::WouldBlock(new_handshake)) => {
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   111
                self.socket = ClientSocket::SslHandshake(Some(new_handshake));
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   112
                Ok(NetworkClientState::Idle)
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   113
            }
13777
fea7c23d9c2c rescue the stream on handshake failure
alfadur
parents: 13776
diff changeset
   114
            Err(HandshakeError::Failure(new_handshake)) => {
fea7c23d9c2c rescue the stream on handshake failure
alfadur
parents: 13776
diff changeset
   115
                self.socket = ClientSocket::SslHandshake(Some(new_handshake));
13776
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   116
                debug!("TLS handshake with {} ({}) failed", self.id, self.peer_addr);
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   117
                Err(Error::new(ErrorKind::Other, "Connection failure"))
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   118
            }
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   119
            Err(HandshakeError::SetupFailure(_)) => unreachable!(),
13776
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   120
        }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   121
    }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   122
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   123
    fn read_impl<R: Read>(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   124
        decoder: &mut ProtocolDecoder,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   125
        source: &mut R,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   126
        id: ClientId,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   127
        addr: &SocketAddr,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   128
    ) -> NetworkResult<Vec<HWProtocolMessage>> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   129
        let mut bytes_read = 0;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   130
        let result = loop {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   131
            match decoder.read_from(source) {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   132
                Ok(bytes) => {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   133
                    debug!("Client {}: read {} bytes", id, bytes);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   134
                    bytes_read += bytes;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   135
                    if bytes == 0 {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   136
                        let result = if bytes_read == 0 {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   137
                            info!("EOF for client {} ({})", id, addr);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   138
                            (Vec::new(), NetworkClientState::Closed)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   139
                        } else {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   140
                            (decoder.extract_messages(), NetworkClientState::NeedsRead)
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   141
                        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   142
                        break Ok(result);
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   143
                    } else if bytes_read >= MAX_BYTES_PER_READ {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   144
                        break Ok((decoder.extract_messages(), NetworkClientState::NeedsRead));
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   145
                    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   146
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   147
                Err(ref error) if error.kind() == ErrorKind::WouldBlock => {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   148
                    let messages = if bytes_read == 0 {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   149
                        Vec::new()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   150
                    } else {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   151
                        decoder.extract_messages()
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   152
                    };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   153
                    break Ok((messages, NetworkClientState::Idle));
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   154
                }
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   155
                Err(error) => break Err(error),
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   156
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   157
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   158
        result
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   159
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   160
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   161
    pub fn read(&mut self) -> NetworkResult<Vec<HWProtocolMessage>> {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   162
        #[cfg(not(feature = "tls-connections"))]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   163
        match self.socket {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   164
            ClientSocket::Plain(ref mut stream) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   165
                NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   166
            }
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   167
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   168
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   169
        #[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   170
        match self.socket {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   171
            ClientSocket::SslHandshake(ref mut handshake_opt) => {
13776
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   172
                let handshake = std::mem::replace(handshake_opt, None).unwrap();
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   173
                Ok((Vec::new(), self.handshake_impl(handshake)?))
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   174
            }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   175
            ClientSocket::SslStream(ref mut stream) => {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   176
                NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr)
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   177
            }
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   178
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   179
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   180
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   181
    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
   182
        let result = loop {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   183
            match buf_out.write_to(destination) {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   184
                Ok(bytes) if buf_out.is_empty() || bytes == 0 => {
14692
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
   185
                    break Ok(((), NetworkClientState::Idle));
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   186
                }
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   187
                Ok(_) => (),
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   188
                Err(ref error)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   189
                    if error.kind() == ErrorKind::Interrupted
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   190
                        || error.kind() == ErrorKind::WouldBlock =>
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   191
                {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   192
                    break Ok(((), NetworkClientState::NeedsWrite));
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   193
                }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   194
                Err(error) => break Err(error),
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   195
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   196
        };
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   197
        result
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   198
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   199
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   200
    pub fn write(&mut self) -> NetworkResult<()> {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   201
        let result = {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   202
            #[cfg(not(feature = "tls-connections"))]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   203
            match self.socket {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   204
                ClientSocket::Plain(ref mut stream) => {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   205
                    NetworkClient::write_impl(&mut self.buf_out, stream)
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   206
                }
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   207
            }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   208
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   209
            #[cfg(feature = "tls-connections")]
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   210
            {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   211
                match self.socket {
13776
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   212
                    ClientSocket::SslHandshake(ref mut handshake_opt) => {
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   213
                        let handshake = std::mem::replace(handshake_opt, None).unwrap();
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   214
                        Ok(((), self.handshake_impl(handshake)?))
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   215
                    }
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   216
                    ClientSocket::SslStream(ref mut stream) => {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   217
                        NetworkClient::write_impl(&mut self.buf_out, stream)
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   218
                    }
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   219
                }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   220
            }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   221
        };
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   222
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   223
        self.socket.inner().flush()?;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   224
        result
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   225
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   226
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   227
    pub fn send_raw_msg(&mut self, msg: &[u8]) {
13500
5359ff75da3a indulge clippy
alfadur
parents: 13450
diff changeset
   228
        self.buf_out.write_all(msg).unwrap();
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
13500
5359ff75da3a indulge clippy
alfadur
parents: 13450
diff changeset
   231
    pub fn send_string(&mut self, msg: &str) {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   232
        self.send_raw_msg(&msg.as_bytes());
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   233
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   234
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   235
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   236
#[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   237
struct ServerSsl {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   238
    context: SslContext,
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   239
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   240
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   241
#[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   242
pub struct IoLayer {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   243
    next_request_id: RequestId,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   244
    request_queue: Vec<(RequestId, ClientId)>,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   245
    io_thread: IOThread,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   246
}
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   247
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   248
#[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   249
impl IoLayer {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   250
    fn new() -> Self {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   251
        Self {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   252
            next_request_id: 0,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   253
            request_queue: vec![],
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   254
            io_thread: IOThread::new(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   255
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   256
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   257
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   258
    fn send(&mut self, client_id: ClientId, task: IoTask) {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   259
        let request_id = self.next_request_id;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   260
        self.next_request_id += 1;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   261
        self.request_queue.push((request_id, client_id));
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   262
        self.io_thread.send(request_id, task);
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   263
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   264
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   265
    fn try_recv(&mut self) -> Option<(ClientId, IoResult)> {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   266
        let (request_id, result) = self.io_thread.try_recv()?;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   267
        if let Some(index) = self
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   268
            .request_queue
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   269
            .iter()
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   270
            .position(|(id, _)| *id == request_id)
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   271
        {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   272
            let (_, client_id) = self.request_queue.swap_remove(index);
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   273
            Some((client_id, result))
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   274
        } else {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   275
            None
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   276
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   277
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   278
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   279
    fn cancel(&mut self, client_id: ClientId) {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   280
        let mut index = 0;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   281
        while index < self.request_queue.len() {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   282
            if self.request_queue[index].1 == client_id {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   283
                self.request_queue.swap_remove(index);
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   284
            } else {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   285
                index += 1;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   286
            }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   287
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   288
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   289
}
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   290
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   291
pub struct NetworkLayer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   292
    listener: TcpListener,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   293
    server: HWServer,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   294
    clients: Slab<NetworkClient>,
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   295
    pending: HashSet<(ClientId, NetworkClientState)>,
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   296
    pending_cache: Vec<(ClientId, NetworkClientState)>,
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   297
    #[cfg(feature = "tls-connections")]
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   298
    ssl: ServerSsl,
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   299
    #[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   300
    io: IoLayer,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   301
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   302
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   303
impl NetworkLayer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   304
    pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> NetworkLayer {
14801
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14800
diff changeset
   305
        let server = HWServer::new(clients_limit, rooms_limit);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   306
        let clients = Slab::with_capacity(clients_limit);
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   307
        let pending = HashSet::with_capacity(2 * clients_limit);
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   308
        let pending_cache = Vec::with_capacity(2 * clients_limit);
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   309
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   310
        NetworkLayer {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   311
            listener,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   312
            server,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   313
            clients,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   314
            pending,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   315
            pending_cache,
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   316
            #[cfg(feature = "tls-connections")]
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   317
            ssl: NetworkLayer::create_ssl_context(),
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   318
            #[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   319
            io: IoLayer::new(),
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   320
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   321
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   322
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   323
    #[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   324
    fn create_ssl_context() -> ServerSsl {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   325
        let mut builder = SslContextBuilder::new(SslMethod::tls()).unwrap();
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   326
        builder.set_verify(SslVerifyMode::NONE);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   327
        builder.set_read_ahead(true);
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   328
        builder
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   329
            .set_certificate_file("ssl/cert.pem", SslFiletype::PEM)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   330
            .unwrap();
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   331
        builder
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   332
            .set_private_key_file("ssl/key.pem", SslFiletype::PEM)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   333
            .unwrap();
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   334
        builder.set_options(SslOptions::NO_COMPRESSION);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   335
        builder.set_cipher_list("DEFAULT:!LOW:!RC4:!EXP").unwrap();
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   336
        ServerSsl {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   337
            context: builder.build(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   338
        }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   339
    }
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
    pub fn register_server(&self, poll: &Poll) -> io::Result<()> {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   342
        poll.register(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   343
            &self.listener,
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   344
            utils::SERVER_TOKEN,
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   345
            Ready::readable(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   346
            PollOpt::edge(),
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   347
        )?;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   348
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   349
        #[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   350
        self.io.io_thread.register_rx(poll, utils::IO_TOKEN)?;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   351
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   352
        Ok(())
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   353
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   354
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   355
    fn deregister_client(&mut self, poll: &Poll, id: ClientId) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   356
        let mut client_exists = false;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   357
        if let Some(ref client) = self.clients.get(id) {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   358
            poll.deregister(client.socket.inner())
13500
5359ff75da3a indulge clippy
alfadur
parents: 13450
diff changeset
   359
                .expect("could not deregister socket");
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   360
            info!("client {} ({}) removed", client.id, client.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   361
            client_exists = true;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   362
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   363
        if client_exists {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   364
            self.clients.remove(id);
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   365
            #[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   366
            self.io.cancel(id);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   367
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   368
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   369
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   370
    fn register_client(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   371
        &mut self,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   372
        poll: &Poll,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   373
        client_socket: ClientSocket,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   374
        addr: SocketAddr,
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   375
    ) -> ClientId {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   376
        let entry = self.clients.vacant_entry();
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   377
        let client_id = entry.key();
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   378
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   379
        poll.register(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   380
            client_socket.inner(),
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   381
            Token(client_id),
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   382
            Ready::readable() | Ready::writable(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   383
            PollOpt::edge(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   384
        )
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   385
        .expect("could not register socket with event loop");
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   386
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   387
        let client = NetworkClient::new(client_id, client_socket, addr);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   388
        info!("client {} ({}) added", client.id, client.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   389
        entry.insert(client);
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   390
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   391
        client_id
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   392
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   393
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   394
    fn handle_response(&mut self, mut response: handlers::Response, poll: &Poll) {
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
   395
        debug!("{} pending server messages", response.len());
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
   396
        let output = response.extract_messages(&mut self.server);
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
   397
        for (clients, message) in output {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   398
            debug!("Message {:?} to {:?}", message, clients);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   399
            let msg_string = message.to_raw_protocol();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   400
            for client_id in clients {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   401
                if let Some(client) = self.clients.get_mut(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   402
                    client.send_string(&msg_string);
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   403
                    self.pending
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   404
                        .insert((client_id, NetworkClientState::NeedsWrite));
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   405
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   406
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   407
        }
14717
8a45c90f4580 fix client removal
alfadur
parents: 14714
diff changeset
   408
8a45c90f4580 fix client removal
alfadur
parents: 14714
diff changeset
   409
        for client_id in response.extract_removed_clients() {
8a45c90f4580 fix client removal
alfadur
parents: 14714
diff changeset
   410
            self.deregister_client(poll, client_id);
8a45c90f4580 fix client removal
alfadur
parents: 14714
diff changeset
   411
        }
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   412
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   413
        #[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   414
        {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   415
            let client_id = response.client_id();
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   416
            for task in response.extract_io_tasks() {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   417
                self.io.send(client_id, task);
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   418
            }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   419
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   420
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   421
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   422
    #[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   423
    pub fn handle_io_result(&mut self) {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   424
        if let Some((client_id, result)) = self.io.try_recv() {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   425
            let mut response = handlers::Response::new(client_id);
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   426
            handlers::handle_io_result(&mut self.server, client_id, &mut response, result);
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   427
        }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   428
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   429
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   430
    fn create_client_socket(&self, socket: TcpStream) -> io::Result<ClientSocket> {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   431
        #[cfg(not(feature = "tls-connections"))]
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   432
        {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   433
            Ok(ClientSocket::Plain(socket))
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   434
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   435
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   436
        #[cfg(feature = "tls-connections")]
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   437
        {
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   438
            let ssl = Ssl::new(&self.ssl.context).unwrap();
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   439
            let mut builder = SslStreamBuilder::new(ssl, socket);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   440
            builder.set_accept_state();
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   441
            match builder.handshake() {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   442
                Ok(stream) => Ok(ClientSocket::SslStream(stream)),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   443
                Err(HandshakeError::WouldBlock(stream)) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   444
                    Ok(ClientSocket::SslHandshake(Some(stream)))
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   445
                }
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   446
                Err(e) => {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   447
                    debug!("OpenSSL handshake failed: {}", e);
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   448
                    Err(Error::new(ErrorKind::Other, "Connection failure"))
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   449
                }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   450
            }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   451
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   452
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   453
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   454
    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
   455
        let (client_socket, addr) = self.listener.accept()?;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   456
        info!("Connected: {}", addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   457
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   458
        let client_id = self.register_client(poll, self.create_client_socket(client_socket)?, addr);
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   459
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   460
        let mut response = handlers::Response::new(client_id);
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   461
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   462
        handlers::handle_client_accept(&mut self.server, client_id, &mut response);
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   463
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   464
        if !response.is_empty() {
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   465
            self.handle_response(response, poll);
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14694
diff changeset
   466
        }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   467
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   468
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   469
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   470
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   471
    fn operation_failed(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   472
        &mut self,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   473
        poll: &Poll,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   474
        client_id: ClientId,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   475
        error: &Error,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   476
        msg: &str,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   477
    ) -> io::Result<()> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   478
        let addr = if let Some(ref mut client) = self.clients.get_mut(client_id) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   479
            client.peer_addr
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   480
        } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   481
            SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   482
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   483
        debug!("{}({}): {}", msg, addr, error);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   484
        self.client_error(poll, client_id)
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   485
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   486
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   487
    pub fn client_readable(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   488
        let messages = if let Some(ref mut client) = self.clients.get_mut(client_id) {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   489
            client.read()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   490
        } else {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   491
            warn!("invalid readable client: {}", client_id);
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   492
            Ok((Vec::new(), NetworkClientState::Idle))
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   493
        };
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   494
14692
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
   495
        let mut response = handlers::Response::new(client_id);
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
   496
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   497
        match messages {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   498
            Ok((messages, state)) => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   499
                for message in messages {
14692
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
   500
                    debug!("Handling message {:?} for client {}", message, client_id);
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
   501
                    if self.server.clients.contains(client_id) {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
   502
                        handlers::handle(&mut self.server, client_id, &mut response, message);
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
   503
                    }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   504
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   505
                match state {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   506
                    NetworkClientState::NeedsRead => {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   507
                        self.pending.insert((client_id, state));
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   508
                    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   509
                    NetworkClientState::Closed => self.client_error(&poll, client_id)?,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   510
                    _ => {}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   511
                };
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   512
            }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   513
            Err(e) => self.operation_failed(
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   514
                poll,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   515
                client_id,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   516
                &e,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   517
                "Error while reading from client socket",
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   518
            )?,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   519
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   520
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
   521
        if !response.is_empty() {
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   522
            self.handle_response(response, poll);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   523
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   524
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   525
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   526
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   527
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   528
    pub fn client_writable(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   529
        let result = if let Some(ref mut client) = self.clients.get_mut(client_id) {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   530
            client.write()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   531
        } else {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   532
            warn!("invalid writable client: {}", client_id);
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   533
            Ok(((), NetworkClientState::Idle))
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   534
        };
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   535
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   536
        match result {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   537
            Ok(((), state)) if state == NetworkClientState::NeedsWrite => {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   538
                self.pending.insert((client_id, state));
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   539
            }
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   540
            Ok(_) => {}
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   541
            Err(e) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   542
                self.operation_failed(poll, client_id, &e, "Error while writing to client socket")?
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   543
            }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   544
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   545
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   546
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   547
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   548
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   549
    pub fn client_error(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   550
        self.deregister_client(poll, client_id);
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
   551
        let mut response = handlers::Response::new(client_id);
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
   552
        handlers::handle_client_loss(&mut self.server, client_id, &mut response);
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   553
        self.handle_response(response, poll);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   554
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   555
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   556
    }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   557
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   558
    pub fn has_pending_operations(&self) -> bool {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   559
        !self.pending.is_empty()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   560
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   561
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   562
    pub fn on_idle(&mut self, poll: &Poll) -> io::Result<()> {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   563
        if self.has_pending_operations() {
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13419
diff changeset
   564
            let mut cache = replace(&mut self.pending_cache, Vec::new());
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   565
            cache.extend(self.pending.drain());
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   566
            for (id, state) in cache.drain(..) {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   567
                match state {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   568
                    NetworkClientState::NeedsRead => self.client_readable(poll, id)?,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   569
                    NetworkClientState::NeedsWrite => self.client_writable(poll, id)?,
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   570
                    _ => {}
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   571
                }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   572
            }
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   573
            swap(&mut cache, &mut self.pending_cache);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   574
        }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   575
        Ok(())
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   576
    }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   577
}