rust/hedgewars-server/src/server/network.rs
author unc0rr
Wed, 23 Jun 2021 23:41:51 +0200
changeset 15804 747278149393
parent 15800 6af892a0a4b8
child 15831 7d0f747afcb8
permissions -rw-r--r--
Extract network protocol into a separate crate
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,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     5
    io,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     6
    io::{Error, ErrorKind, Read, Write},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     7
    mem::{replace, swap},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     8
    net::{IpAddr, Ipv4Addr, SocketAddr},
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
     9
    num::NonZeroU32,
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    10
    time::Duration,
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    11
    time::Instant,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    12
};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    13
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    14
use log::*;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    15
use mio::{
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    16
    event::Source,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    17
    net::{TcpListener, TcpStream},
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    18
    Interest, Poll, Token, Waker,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    19
};
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    20
use netbuf;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    21
use slab::Slab;
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    22
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
    23
use crate::{
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    24
    core::{
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    25
        events::{TimedEvents, Timeout},
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    26
        types::ClientId,
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    27
    },
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14916
diff changeset
    28
    handlers,
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
    29
    handlers::{IoResult, IoTask, ServerState},
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15800
diff changeset
    30
    protocol::ProtocolDecoder,
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
    31
    utils,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    32
};
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15800
diff changeset
    33
use hedgewars_network_protocol::{messages::HwServerMessage::Redirect, messages::*};
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
    34
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
    35
#[cfg(feature = "official-server")]
15098
9397e07b3086 use blocking recv in IO thread
alfadur
parents: 15075
diff changeset
    36
use super::io::{IoThread, RequestId};
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
    37
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    38
#[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    39
use openssl::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    40
    error::ErrorStack,
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    41
    ssl::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    42
        HandshakeError, MidHandshakeSslStream, Ssl, SslContext, SslContextBuilder, SslFiletype,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    43
        SslMethod, SslOptions, SslStream, SslStreamBuilder, SslVerifyMode,
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    44
    },
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    45
};
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    46
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    47
const MAX_BYTES_PER_READ: usize = 2048;
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    48
const SEND_PING_TIMEOUT: Duration = Duration::from_secs(5);
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    49
const DROP_CLIENT_TIMEOUT: Duration = Duration::from_secs(5);
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    50
const MAX_TIMEOUT: usize = DROP_CLIENT_TIMEOUT.as_secs() as usize;
14807
b2beb784e4b5 add support for multiple ping probes
alfadur
parents: 14803
diff changeset
    51
const PING_PROBES_COUNT: u8 = 2;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    52
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
    53
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    54
pub enum NetworkClientState {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    55
    Idle,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    56
    NeedsWrite,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    57
    NeedsRead,
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    58
    Closed,
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
    59
    #[cfg(feature = "tls-connections")]
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
    60
    Connected,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    61
}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    62
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    63
type NetworkResult<T> = io::Result<(T, NetworkClientState)>;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    64
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    65
pub enum ClientSocket {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    66
    Plain(TcpStream),
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
    67
    #[cfg(feature = "tls-connections")]
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    68
    SslHandshake(Option<MidHandshakeSslStream<TcpStream>>),
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
    69
    #[cfg(feature = "tls-connections")]
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    70
    SslStream(SslStream<TcpStream>),
13799
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
impl ClientSocket {
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    74
    fn inner_mut(&mut self) -> &mut TcpStream {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    75
        match self {
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    76
            ClientSocket::Plain(stream) => stream,
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
    77
            #[cfg(feature = "tls-connections")]
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    78
            ClientSocket::SslHandshake(Some(builder)) => builder.get_mut(),
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
    79
            #[cfg(feature = "tls-connections")]
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    80
            ClientSocket::SslHandshake(None) => unreachable!(),
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
    81
            #[cfg(feature = "tls-connections")]
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    82
            ClientSocket::SslStream(ssl_stream) => ssl_stream.get_mut(),
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    83
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    84
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    85
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    86
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    87
pub struct NetworkClient {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    88
    id: ClientId,
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    89
    socket: ClientSocket,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    90
    peer_addr: SocketAddr,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    91
    decoder: ProtocolDecoder,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    92
    buf_out: netbuf::Buf,
15174
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
    93
    pending_close: bool,
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    94
    timeout: Timeout,
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    95
    last_rx_time: Instant,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    96
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    97
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    98
impl NetworkClient {
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
    99
    pub fn new(
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   100
        id: ClientId,
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   101
        socket: ClientSocket,
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   102
        peer_addr: SocketAddr,
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   103
        timeout: Timeout,
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   104
    ) -> NetworkClient {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   105
        NetworkClient {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   106
            id,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   107
            socket,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   108
            peer_addr,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   109
            decoder: ProtocolDecoder::new(),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   110
            buf_out: netbuf::Buf::new(),
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   111
            pending_close: false,
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   112
            timeout,
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   113
            last_rx_time: Instant::now(),
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   114
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   115
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   116
13802
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   117
    #[cfg(feature = "tls-connections")]
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   118
    fn handshake_impl(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   119
        &mut self,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   120
        handshake: MidHandshakeSslStream<TcpStream>,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   121
    ) -> io::Result<NetworkClientState> {
13802
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   122
        match handshake.handshake() {
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   123
            Ok(stream) => {
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   124
                self.socket = ClientSocket::SslStream(stream);
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   125
                debug!(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   126
                    "TLS handshake with {} ({}) completed",
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   127
                    self.id, self.peer_addr
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   128
                );
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   129
                Ok(NetworkClientState::Connected)
13802
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   130
            }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   131
            Err(HandshakeError::WouldBlock(new_handshake)) => {
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   132
                self.socket = ClientSocket::SslHandshake(Some(new_handshake));
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   133
                Ok(NetworkClientState::Idle)
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   134
            }
13803
fea7c23d9c2c rescue the stream on handshake failure
alfadur
parents: 13802
diff changeset
   135
            Err(HandshakeError::Failure(new_handshake)) => {
fea7c23d9c2c rescue the stream on handshake failure
alfadur
parents: 13802
diff changeset
   136
                self.socket = ClientSocket::SslHandshake(Some(new_handshake));
13802
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   137
                debug!("TLS handshake with {} ({}) failed", self.id, self.peer_addr);
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   138
                Err(Error::new(ErrorKind::Other, "Connection failure"))
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   139
            }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   140
            Err(HandshakeError::SetupFailure(_)) => unreachable!(),
13802
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   141
        }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   142
    }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   143
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   144
    fn read_impl<R: Read>(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   145
        decoder: &mut ProtocolDecoder,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   146
        source: &mut R,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   147
        id: ClientId,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   148
        addr: &SocketAddr,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   149
    ) -> NetworkResult<Vec<HwProtocolMessage>> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   150
        let mut bytes_read = 0;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   151
        let result = loop {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   152
            match decoder.read_from(source) {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   153
                Ok(bytes) => {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   154
                    debug!("Client {}: read {} bytes", id, bytes);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   155
                    bytes_read += bytes;
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   156
                    if bytes == 0 {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   157
                        let result = if bytes_read == 0 {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   158
                            info!("EOF for client {} ({})", id, addr);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   159
                            (Vec::new(), NetworkClientState::Closed)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   160
                        } else {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   161
                            (decoder.extract_messages(), NetworkClientState::NeedsRead)
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   162
                        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   163
                        break Ok(result);
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   164
                    } else if bytes_read >= MAX_BYTES_PER_READ {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   165
                        break Ok((decoder.extract_messages(), NetworkClientState::NeedsRead));
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   166
                    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   167
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   168
                Err(ref error) if error.kind() == ErrorKind::WouldBlock => {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   169
                    let messages = if bytes_read == 0 {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   170
                        Vec::new()
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   171
                    } else {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   172
                        decoder.extract_messages()
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   173
                    };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   174
                    break Ok((messages, NetworkClientState::Idle));
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   175
                }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   176
                Err(error) => break Err(error),
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   177
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   178
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   179
        result
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   180
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   181
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   182
    pub fn read(&mut self) -> NetworkResult<Vec<HwProtocolMessage>> {
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   183
        let result = match self.socket {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   184
            ClientSocket::Plain(ref mut stream) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   185
                NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   186
            }
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   187
            #[cfg(feature = "tls-connections")]
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   188
            ClientSocket::SslHandshake(ref mut handshake_opt) => {
13802
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   189
                let handshake = std::mem::replace(handshake_opt, None).unwrap();
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   190
                Ok((Vec::new(), self.handshake_impl(handshake)?))
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   191
            }
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   192
            #[cfg(feature = "tls-connections")]
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   193
            ClientSocket::SslStream(ref mut stream) => {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   194
                NetworkClient::read_impl(&mut self.decoder, stream, self.id, &self.peer_addr)
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   195
            }
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   196
        };
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   197
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   198
        if let Ok(_) = result {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   199
            self.last_rx_time = Instant::now();
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   200
        }
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   201
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   202
        result
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   203
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   204
15174
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   205
    fn write_impl<W: Write>(
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   206
        buf_out: &mut netbuf::Buf,
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   207
        destination: &mut W,
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   208
        close_on_empty: bool,
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   209
    ) -> NetworkResult<()> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   210
        let result = loop {
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   211
            match buf_out.write_to(destination) {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   212
                Ok(bytes) if buf_out.is_empty() || bytes == 0 => {
15174
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   213
                    let status = if buf_out.is_empty() && close_on_empty {
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   214
                        NetworkClientState::Closed
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   215
                    } else {
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   216
                        NetworkClientState::Idle
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   217
                    };
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   218
                    break Ok(((), status));
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   219
                }
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   220
                Ok(_) => (),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   221
                Err(ref error)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   222
                    if error.kind() == ErrorKind::Interrupted
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   223
                        || error.kind() == ErrorKind::WouldBlock =>
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   224
                {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   225
                    break Ok(((), NetworkClientState::NeedsWrite));
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   226
                }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   227
                Err(error) => break Err(error),
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   228
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   229
        };
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   230
        result
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   231
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   232
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   233
    pub fn write(&mut self) -> NetworkResult<()> {
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   234
        let result = match self.socket {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   235
            ClientSocket::Plain(ref mut stream) => {
15174
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   236
                NetworkClient::write_impl(&mut self.buf_out, stream, self.pending_close)
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   237
            }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   238
            #[cfg(feature = "tls-connections")]
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   239
            ClientSocket::SslHandshake(ref mut handshake_opt) => {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   240
                let handshake = std::mem::replace(handshake_opt, None).unwrap();
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   241
                Ok(((), self.handshake_impl(handshake)?))
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   242
            }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   243
            #[cfg(feature = "tls-connections")]
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   244
            ClientSocket::SslStream(ref mut stream) => {
15174
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   245
                NetworkClient::write_impl(&mut self.buf_out, stream, self.pending_close)
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   246
            }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   247
        };
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   248
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   249
        self.socket.inner_mut().flush()?;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   250
        result
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   251
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   252
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   253
    pub fn send_raw_msg(&mut self, msg: &[u8]) {
13524
5359ff75da3a indulge clippy
alfadur
parents: 13478
diff changeset
   254
        self.buf_out.write_all(msg).unwrap();
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   255
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   256
13524
5359ff75da3a indulge clippy
alfadur
parents: 13478
diff changeset
   257
    pub fn send_string(&mut self, msg: &str) {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   258
        self.send_raw_msg(&msg.as_bytes());
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   259
    }
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   260
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   261
    pub fn replace_timeout(&mut self, timeout: Timeout) -> Timeout {
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   262
        replace(&mut self.timeout, timeout)
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   263
    }
15174
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   264
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   265
    pub fn has_pending_sends(&self) -> bool {
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   266
        !self.buf_out.is_empty()
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   267
    }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   268
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   269
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   270
#[cfg(feature = "tls-connections")]
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   271
struct ServerSsl {
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   272
    listener: TcpListener,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   273
    context: SslContext,
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   274
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   275
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   276
#[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   277
pub struct IoLayer {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   278
    next_request_id: RequestId,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   279
    request_queue: Vec<(RequestId, ClientId)>,
15098
9397e07b3086 use blocking recv in IO thread
alfadur
parents: 15075
diff changeset
   280
    io_thread: IoThread,
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   281
}
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   282
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   283
#[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   284
impl IoLayer {
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   285
    fn new(waker: Waker) -> Self {
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   286
        Self {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   287
            next_request_id: 0,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   288
            request_queue: vec![],
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   289
            io_thread: IoThread::new(waker),
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   290
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   291
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   292
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   293
    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: 14697
diff changeset
   294
        let request_id = self.next_request_id;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   295
        self.next_request_id += 1;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   296
        self.request_queue.push((request_id, client_id));
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   297
        self.io_thread.send(request_id, task);
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   298
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   299
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   300
    fn try_recv(&mut self) -> Option<(ClientId, IoResult)> {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   301
        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: 14697
diff changeset
   302
        if let Some(index) = self
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   303
            .request_queue
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   304
            .iter()
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   305
            .position(|(id, _)| *id == request_id)
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   306
        {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   307
            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: 14697
diff changeset
   308
            Some((client_id, result))
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   309
        } else {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   310
            None
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   311
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   312
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   313
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   314
    fn cancel(&mut self, client_id: ClientId) {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   315
        let mut index = 0;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   316
        while index < self.request_queue.len() {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   317
            if self.request_queue[index].1 == client_id {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   318
                self.request_queue.swap_remove(index);
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   319
            } else {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   320
                index += 1;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   321
            }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   322
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   323
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   324
}
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   325
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   326
enum TimeoutEvent {
14807
b2beb784e4b5 add support for multiple ping probes
alfadur
parents: 14803
diff changeset
   327
    SendPing { probes_count: u8 },
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   328
    DropClient,
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   329
}
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   330
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   331
struct TimerData(TimeoutEvent, ClientId);
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   332
type NetworkTimeoutEvents = TimedEvents<TimerData, MAX_TIMEOUT>;
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   333
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   334
pub struct NetworkLayer {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   335
    listener: TcpListener,
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
   336
    server_state: ServerState,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   337
    clients: Slab<NetworkClient>,
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   338
    pending: HashSet<(ClientId, NetworkClientState)>,
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   339
    pending_cache: Vec<(ClientId, NetworkClientState)>,
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   340
    #[cfg(feature = "tls-connections")]
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   341
    ssl: ServerSsl,
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   342
    #[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   343
    io: IoLayer,
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   344
    timeout_events: NetworkTimeoutEvents,
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   345
}
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   346
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   347
fn register_read<S: Source>(poll: &Poll, source: &mut S, token: mio::Token) -> io::Result<()> {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   348
    poll.registry().register(source, token, Interest::READABLE)
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   349
}
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   350
14807
b2beb784e4b5 add support for multiple ping probes
alfadur
parents: 14803
diff changeset
   351
fn create_ping_timeout(
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   352
    timeout_events: &mut NetworkTimeoutEvents,
14807
b2beb784e4b5 add support for multiple ping probes
alfadur
parents: 14803
diff changeset
   353
    probes_count: u8,
b2beb784e4b5 add support for multiple ping probes
alfadur
parents: 14803
diff changeset
   354
    client_id: ClientId,
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   355
) -> Timeout {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   356
    timeout_events.set_timeout(
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   357
        NonZeroU32::new(SEND_PING_TIMEOUT.as_secs() as u32).unwrap(),
14807
b2beb784e4b5 add support for multiple ping probes
alfadur
parents: 14803
diff changeset
   358
        TimerData(TimeoutEvent::SendPing { probes_count }, client_id),
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   359
    )
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   360
}
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   361
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   362
fn create_drop_timeout(timeout_events: &mut NetworkTimeoutEvents, client_id: ClientId) -> Timeout {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   363
    timeout_events.set_timeout(
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   364
        NonZeroU32::new(DROP_CLIENT_TIMEOUT.as_secs() as u32).unwrap(),
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   365
        TimerData(TimeoutEvent::DropClient, client_id),
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   366
    )
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
impl NetworkLayer {
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   370
    pub fn register(&mut self, poll: &Poll) -> io::Result<()> {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   371
        register_read(poll, &mut self.listener, utils::SERVER_TOKEN)?;
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   372
        #[cfg(feature = "tls-connections")]
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   373
        register_read(poll, &mut self.ssl.listener, utils::SECURE_SERVER_TOKEN)?;
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   374
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   375
        Ok(())
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   376
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   377
15175
f1c2289d40bd break error loop in client removal
alfadur
parents: 15174
diff changeset
   378
    fn deregister_client(&mut self, poll: &Poll, id: ClientId, is_error: bool) {
15174
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   379
        if let Some(ref mut client) = self.clients.get_mut(id) {
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   380
            poll.registry()
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   381
                .deregister(client.socket.inner_mut())
13524
5359ff75da3a indulge clippy
alfadur
parents: 13478
diff changeset
   382
                .expect("could not deregister socket");
15175
f1c2289d40bd break error loop in client removal
alfadur
parents: 15174
diff changeset
   383
            if client.has_pending_sends() && !is_error {
15174
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   384
                info!(
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   385
                    "client {} ({}) pending removal",
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   386
                    client.id, client.peer_addr
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   387
                );
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   388
                client.pending_close = true;
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   389
                poll.registry()
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   390
                    .register(client.socket.inner_mut(), Token(id), Interest::WRITABLE)
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   391
                    .unwrap_or_else(|_| {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   392
                        self.clients.remove(id);
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   393
                    });
15174
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   394
            } else {
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   395
                info!("client {} ({}) removed", client.id, client.peer_addr);
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   396
                self.clients.remove(id);
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   397
            }
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   398
            #[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   399
            self.io.cancel(id);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   400
        }
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
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   403
    fn register_client(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   404
        &mut self,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   405
        poll: &Poll,
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   406
        mut client_socket: ClientSocket,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   407
        addr: SocketAddr,
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14907
diff changeset
   408
    ) -> io::Result<ClientId> {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14673
diff changeset
   409
        let entry = self.clients.vacant_entry();
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14673
diff changeset
   410
        let client_id = entry.key();
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14673
diff changeset
   411
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   412
        poll.registry().register(
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   413
            client_socket.inner_mut(),
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14673
diff changeset
   414
            Token(client_id),
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   415
            Interest::READABLE | Interest::WRITABLE,
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14907
diff changeset
   416
        )?;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   417
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   418
        let client = NetworkClient::new(
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   419
            client_id,
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   420
            client_socket,
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   421
            addr,
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   422
            create_ping_timeout(&mut self.timeout_events, PING_PROBES_COUNT - 1, client_id),
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   423
        );
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   424
        info!("client {} ({}) added", client.id, client.peer_addr);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   425
        entry.insert(client);
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14673
diff changeset
   426
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14907
diff changeset
   427
        Ok(client_id)
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   428
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   429
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   430
    fn handle_response(&mut self, mut response: handlers::Response, poll: &Poll) {
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   431
        if response.is_empty() {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   432
            return;
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   433
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   434
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   435
        debug!("{} pending server messages", response.len());
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
   436
        let output = response.extract_messages(&mut self.server_state.server);
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   437
        for (clients, message) in output {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   438
            debug!("Message {:?} to {:?}", message, clients);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   439
            let msg_string = message.to_raw_protocol();
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   440
            for client_id in clients {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   441
                if let Some(client) = self.clients.get_mut(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   442
                    client.send_string(&msg_string);
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   443
                    self.pending
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   444
                        .insert((client_id, NetworkClientState::NeedsWrite));
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   445
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   446
            }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   447
        }
14696
8a45c90f4580 fix client removal
alfadur
parents: 14693
diff changeset
   448
8a45c90f4580 fix client removal
alfadur
parents: 14693
diff changeset
   449
        for client_id in response.extract_removed_clients() {
15175
f1c2289d40bd break error loop in client removal
alfadur
parents: 15174
diff changeset
   450
            self.deregister_client(poll, client_id, false);
14696
8a45c90f4580 fix client removal
alfadur
parents: 14693
diff changeset
   451
        }
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   452
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   453
        #[cfg(feature = "official-server")]
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   454
        {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   455
            let client_id = response.client_id();
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   456
            for task in response.extract_io_tasks() {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   457
                self.io.send(client_id, task);
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   458
            }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   459
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   460
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   461
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   462
    pub fn handle_timeout(&mut self, poll: &mut Poll) -> io::Result<()> {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   463
        for TimerData(event, client_id) in self.timeout_events.poll(Instant::now()) {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   464
            if let Some(client) = self.clients.get_mut(client_id) {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   465
                if client.last_rx_time.elapsed() > SEND_PING_TIMEOUT {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   466
                    match event {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   467
                        TimeoutEvent::SendPing { probes_count } => {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   468
                            client.send_string(&HwServerMessage::Ping.to_raw_protocol());
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   469
                            client.write()?;
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   470
                            let timeout = if probes_count != 0 {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   471
                                create_ping_timeout(
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   472
                                    &mut self.timeout_events,
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   473
                                    probes_count - 1,
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   474
                                    client_id,
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   475
                                )
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   476
                            } else {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   477
                                create_drop_timeout(&mut self.timeout_events, client_id)
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   478
                            };
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   479
                            client.replace_timeout(timeout);
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   480
                        }
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   481
                        TimeoutEvent::DropClient => {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   482
                            client.send_string(
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   483
                                &HwServerMessage::Bye("Ping timeout".to_string()).to_raw_protocol(),
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   484
                            );
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   485
                            let _res = client.write();
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   486
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   487
                            self.operation_failed(
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   488
                                poll,
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   489
                                client_id,
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   490
                                &ErrorKind::TimedOut.into(),
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   491
                                "No ping response",
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   492
                            )?;
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   493
                        }
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   494
                    }
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   495
                } else {
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   496
                    client.replace_timeout(create_ping_timeout(
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   497
                        &mut self.timeout_events,
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   498
                        PING_PROBES_COUNT - 1,
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   499
                        client_id,
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   500
                    ));
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   501
                }
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   502
            }
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   503
        }
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   504
        Ok(())
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   505
    }
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   506
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   507
    #[cfg(feature = "official-server")]
15102
80ff12edf5e6 handle response from IO result handler
alfadur
parents: 15098
diff changeset
   508
    pub fn handle_io_result(&mut self, poll: &Poll) -> io::Result<()> {
80ff12edf5e6 handle response from IO result handler
alfadur
parents: 15098
diff changeset
   509
        while let Some((client_id, result)) = self.io.try_recv() {
80ff12edf5e6 handle response from IO result handler
alfadur
parents: 15098
diff changeset
   510
            debug!("Handling io result {:?} for client {}", result, client_id);
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   511
            let mut response = handlers::Response::new(client_id);
15531
ede5f4ec48f3 fix official server
alfadur <mail@none>
parents: 15530
diff changeset
   512
            handlers::handle_io_result(&mut self.server_state, client_id, &mut response, result);
15102
80ff12edf5e6 handle response from IO result handler
alfadur
parents: 15098
diff changeset
   513
            self.handle_response(response, poll);
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   514
        }
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14907
diff changeset
   515
        Ok(())
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   516
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   517
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   518
    fn create_client_socket(&self, socket: TcpStream) -> io::Result<ClientSocket> {
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   519
        Ok(ClientSocket::Plain(socket))
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   520
    }
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   521
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   522
    #[cfg(feature = "tls-connections")]
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   523
    fn create_client_secure_socket(&self, socket: TcpStream) -> io::Result<ClientSocket> {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   524
        let ssl = Ssl::new(&self.ssl.context).unwrap();
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   525
        let mut builder = SslStreamBuilder::new(ssl, socket);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   526
        builder.set_accept_state();
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   527
        match builder.handshake() {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   528
            Ok(stream) => Ok(ClientSocket::SslStream(stream)),
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   529
            Err(HandshakeError::WouldBlock(stream)) => Ok(ClientSocket::SslHandshake(Some(stream))),
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   530
            Err(e) => {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   531
                debug!("OpenSSL handshake failed: {}", e);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   532
                Err(Error::new(ErrorKind::Other, "Connection failure"))
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   533
            }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   534
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   535
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   536
14835
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   537
    fn init_client(&mut self, poll: &Poll, client_id: ClientId) {
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   538
        let mut response = handlers::Response::new(client_id);
14847
eef1f76150fe don't redirect to self
alfadur
parents: 14835
diff changeset
   539
eef1f76150fe don't redirect to self
alfadur
parents: 14835
diff changeset
   540
        if let ClientSocket::Plain(_) = self.clients[client_id].socket {
eef1f76150fe don't redirect to self
alfadur
parents: 14835
diff changeset
   541
            #[cfg(feature = "tls-connections")]
eef1f76150fe don't redirect to self
alfadur
parents: 14835
diff changeset
   542
            response.add(Redirect(self.ssl.listener.local_addr().unwrap().port()).send_self())
eef1f76150fe don't redirect to self
alfadur
parents: 14835
diff changeset
   543
        }
14835
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   544
15517
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   545
        if let IpAddr::V4(addr) = self.clients[client_id].peer_addr.ip() {
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   546
            handlers::handle_client_accept(
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
   547
                &mut self.server_state,
15517
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   548
                client_id,
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   549
                &mut response,
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   550
                addr.octets(),
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   551
                addr.is_loopback(),
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   552
            );
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   553
            self.handle_response(response, poll);
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   554
        } else {
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   555
            todo!("implement something")
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   556
        }
14835
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   557
    }
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   558
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   559
    pub fn accept_client(&mut self, poll: &Poll, server_token: mio::Token) -> io::Result<()> {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   560
        match server_token {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   561
            utils::SERVER_TOKEN => {
14870
ce98c37826a7 fix accepting on wrong server socket
alfadur
parents: 14847
diff changeset
   562
                let (client_socket, addr) = self.listener.accept()?;
ce98c37826a7 fix accepting on wrong server socket
alfadur
parents: 14847
diff changeset
   563
                info!("Connected(plaintext): {}", addr);
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   564
                let client_id =
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14907
diff changeset
   565
                    self.register_client(poll, self.create_client_socket(client_socket)?, addr)?;
14835
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   566
                self.init_client(poll, client_id);
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   567
            }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   568
            #[cfg(feature = "tls-connections")]
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   569
            utils::SECURE_SERVER_TOKEN => {
14870
ce98c37826a7 fix accepting on wrong server socket
alfadur
parents: 14847
diff changeset
   570
                let (client_socket, addr) = self.ssl.listener.accept()?;
ce98c37826a7 fix accepting on wrong server socket
alfadur
parents: 14847
diff changeset
   571
                info!("Connected(TLS): {}", addr);
14916
8750530bf7e7 avoid crashing server in the event loop
alfadur
parents: 14907
diff changeset
   572
                self.register_client(poll, self.create_client_secure_socket(client_socket)?, addr)?;
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   573
            }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   574
            _ => unreachable!(),
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14673
diff changeset
   575
        }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   576
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   577
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   578
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   579
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   580
    fn operation_failed(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   581
        &mut self,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   582
        poll: &Poll,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   583
        client_id: ClientId,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   584
        error: &Error,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   585
        msg: &str,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   586
    ) -> io::Result<()> {
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   587
        let addr = if let Some(ref mut client) = self.clients.get_mut(client_id) {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   588
            client.peer_addr
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   589
        } else {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   590
            SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   591
        };
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   592
        debug!("{}({}): {}", msg, addr, error);
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   593
        self.client_error(poll, client_id)
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   594
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   595
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   596
    pub fn client_readable(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   597
        let messages = if let Some(ref mut client) = self.clients.get_mut(client_id) {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   598
            client.read()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   599
        } else {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   600
            warn!("invalid readable client: {}", client_id);
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   601
            Ok((Vec::new(), NetworkClientState::Idle))
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   602
        };
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   603
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   604
        let mut response = handlers::Response::new(client_id);
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   605
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   606
        match messages {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   607
            Ok((messages, state)) => {
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   608
                for message in messages {
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
   609
                    debug!("Handling message {:?} for client {}", message, client_id);
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
   610
                    handlers::handle(&mut self.server_state, client_id, &mut response, message);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   611
                }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   612
                match state {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   613
                    NetworkClientState::NeedsRead => {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   614
                        self.pending.insert((client_id, state));
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   615
                    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   616
                    NetworkClientState::Closed => self.client_error(&poll, client_id)?,
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   617
                    #[cfg(feature = "tls-connections")]
14835
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   618
                    NetworkClientState::Connected => self.init_client(poll, client_id),
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   619
                    _ => {}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   620
                };
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   621
            }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   622
            Err(e) => self.operation_failed(
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   623
                poll,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   624
                client_id,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   625
                &e,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   626
                "Error while reading from client socket",
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   627
            )?,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   628
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   629
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   630
        self.handle_response(response, poll);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   631
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   632
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   633
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   634
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   635
    pub fn client_writable(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   636
        let result = if let Some(ref mut client) = self.clients.get_mut(client_id) {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   637
            client.write()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   638
        } else {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   639
            warn!("invalid writable client: {}", client_id);
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   640
            Ok(((), NetworkClientState::Idle))
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   641
        };
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   642
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   643
        match result {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   644
            Ok(((), state)) if state == NetworkClientState::NeedsWrite => {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   645
                self.pending.insert((client_id, state));
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   646
            }
15174
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   647
            Ok(((), state)) if state == NetworkClientState::Closed => {
15175
f1c2289d40bd break error loop in client removal
alfadur
parents: 15174
diff changeset
   648
                self.deregister_client(poll, client_id, false);
15174
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   649
            }
e705ac360785 pin removed clients until the output buffer is flushed
alfadur
parents: 15173
diff changeset
   650
            Ok(_) => (),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   651
            Err(e) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   652
                self.operation_failed(poll, client_id, &e, "Error while writing to client socket")?
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   653
            }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   654
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   655
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   656
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   657
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   658
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   659
    pub fn client_error(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> {
15175
f1c2289d40bd break error loop in client removal
alfadur
parents: 15174
diff changeset
   660
        let pending_close = self.clients[client_id].pending_close;
f1c2289d40bd break error loop in client removal
alfadur
parents: 15174
diff changeset
   661
        self.deregister_client(poll, client_id, true);
f1c2289d40bd break error loop in client removal
alfadur
parents: 15174
diff changeset
   662
f1c2289d40bd break error loop in client removal
alfadur
parents: 15174
diff changeset
   663
        if !pending_close {
f1c2289d40bd break error loop in client removal
alfadur
parents: 15174
diff changeset
   664
            let mut response = handlers::Response::new(client_id);
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
   665
            handlers::handle_client_loss(&mut self.server_state, client_id, &mut response);
15175
f1c2289d40bd break error loop in client removal
alfadur
parents: 15174
diff changeset
   666
            self.handle_response(response, poll);
f1c2289d40bd break error loop in client removal
alfadur
parents: 15174
diff changeset
   667
        }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   668
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   669
        Ok(())
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   670
    }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   671
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   672
    pub fn has_pending_operations(&self) -> bool {
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   673
        !self.pending.is_empty() || !self.timeout_events.is_empty()
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   674
    }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   675
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   676
    pub fn on_idle(&mut self, poll: &Poll) -> io::Result<()> {
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   677
        if self.has_pending_operations() {
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13419
diff changeset
   678
            let mut cache = replace(&mut self.pending_cache, Vec::new());
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   679
            cache.extend(self.pending.drain());
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   680
            for (id, state) in cache.drain(..) {
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   681
                match state {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   682
                    NetworkClientState::NeedsRead => self.client_readable(poll, id)?,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   683
                    NetworkClientState::NeedsWrite => self.client_writable(poll, id)?,
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   684
                    _ => {}
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   685
                }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   686
            }
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
   687
            swap(&mut cache, &mut self.pending_cache);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   688
        }
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   689
        Ok(())
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   690
    }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   691
}
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   692
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   693
pub struct NetworkLayerBuilder {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   694
    listener: Option<TcpListener>,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   695
    secure_listener: Option<TcpListener>,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   696
    clients_capacity: usize,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   697
    rooms_capacity: usize,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   698
}
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   699
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   700
impl Default for NetworkLayerBuilder {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   701
    fn default() -> Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   702
        Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   703
            clients_capacity: 1024,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   704
            rooms_capacity: 512,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   705
            listener: None,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   706
            secure_listener: None,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   707
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   708
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   709
}
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   710
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   711
impl NetworkLayerBuilder {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   712
    pub fn with_listener(self, listener: TcpListener) -> Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   713
        Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   714
            listener: Some(listener),
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   715
            ..self
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   716
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   717
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   718
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   719
    pub fn with_secure_listener(self, listener: TcpListener) -> Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   720
        Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   721
            secure_listener: Some(listener),
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   722
            ..self
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   723
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   724
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   725
14835
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   726
    #[cfg(feature = "tls-connections")]
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   727
    fn create_ssl_context(listener: TcpListener) -> ServerSsl {
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   728
        let mut builder = SslContextBuilder::new(SslMethod::tls()).unwrap();
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   729
        builder.set_verify(SslVerifyMode::NONE);
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   730
        builder.set_read_ahead(true);
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   731
        builder
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   732
            .set_certificate_file("ssl/cert.pem", SslFiletype::PEM)
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   733
            .expect("Cannot find certificate file");
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   734
        builder
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   735
            .set_private_key_file("ssl/key.pem", SslFiletype::PEM)
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   736
            .expect("Cannot find private key file");
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   737
        builder.set_options(SslOptions::NO_COMPRESSION);
15531
ede5f4ec48f3 fix official server
alfadur <mail@none>
parents: 15530
diff changeset
   738
        builder.set_options(SslOptions::NO_TLSV1);
15530
a859f08ebb4f update tls configuration
alfadur <mail@none>
parents: 15520
diff changeset
   739
        builder.set_options(SslOptions::NO_TLSV1_1);
a859f08ebb4f update tls configuration
alfadur <mail@none>
parents: 15520
diff changeset
   740
        builder.set_cipher_list("ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384").unwrap();
14835
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   741
        ServerSsl {
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   742
            listener,
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   743
            context: builder.build(),
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   744
        }
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   745
    }
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   746
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   747
    pub fn build(self, poll: &Poll) -> NetworkLayer {
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
   748
        let server_state = ServerState::new(self.clients_capacity, self.rooms_capacity);
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
   749
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   750
        let clients = Slab::with_capacity(self.clients_capacity);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   751
        let pending = HashSet::with_capacity(2 * self.clients_capacity);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   752
        let pending_cache = Vec::with_capacity(2 * self.clients_capacity);
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   753
        let timeout_events = NetworkTimeoutEvents::new();
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   754
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   755
        #[cfg(feature = "official-server")]
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   756
        let waker = Waker::new(poll.registry(), utils::IO_TOKEN)
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   757
            .expect("Unable to create a waker for the IO thread");
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   758
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   759
        NetworkLayer {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   760
            listener: self.listener.expect("No listener provided"),
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
   761
            server_state,
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   762
            clients,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   763
            pending,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   764
            pending_cache,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   765
            #[cfg(feature = "tls-connections")]
14835
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   766
            ssl: Self::create_ssl_context(
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   767
                self.secure_listener.expect("No secure listener provided"),
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   768
            ),
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   769
            #[cfg(feature = "official-server")]
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   770
            io: IoLayer::new(waker),
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   771
            timeout_events,
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   772
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   773
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   774
}