rust/hedgewars-server/src/server/network.rs
author unC0Rr
Mon, 02 Oct 2023 21:36:13 +0200
changeset 15979 1635ce22b214
parent 15939 4a8e6f9d5133
child 15989 fb389df02e3e
permissions -rw-r--r--
Adopt more recent versions of dependencies, apply clippy fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
     1
use bytes::{Buf, Bytes};
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
     2
use log::*;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
     3
use slab::Slab;
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
     4
use std::io::Error;
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
     5
use std::pin::Pin;
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
     6
use std::task::{Context, Poll};
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     7
use std::{
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
     8
    iter::Iterator,
15832
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
     9
    net::{IpAddr, SocketAddr},
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
    10
    time::Duration,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    11
};
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    12
use tokio::{
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    13
    io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf},
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    14
    net::{TcpListener, TcpStream},
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    15
    sync::mpsc::{channel, Receiver, Sender},
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    16
};
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    17
#[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    18
use tokio_native_tls::{TlsAcceptor, TlsStream};
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    19
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
    20
use crate::{
15939
4a8e6f9d5133 remove events
alfadur
parents: 15938
diff changeset
    21
    core::types::ClientId,
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14916
diff changeset
    22
    handlers,
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
    23
    handlers::{IoResult, IoTask, ServerState},
15832
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
    24
    protocol::{self, ProtocolDecoder, ProtocolError},
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
    25
    utils,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    26
};
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    27
use hedgewars_network_protocol::{
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    28
    messages::HwServerMessage::Redirect, messages::*, parser::server_message,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    29
};
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
    30
15832
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
    31
const PING_TIMEOUT: Duration = Duration::from_secs(15);
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
    32
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    33
enum ClientUpdateData {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    34
    Message(HwProtocolMessage),
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    35
    Error(String),
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    36
}
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    37
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    38
struct ClientUpdate {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    39
    client_id: ClientId,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    40
    data: ClientUpdateData,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    41
}
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    42
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    43
struct ClientUpdateSender {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    44
    client_id: ClientId,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    45
    sender: Sender<ClientUpdate>,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    46
}
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    47
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    48
impl ClientUpdateSender {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    49
    async fn send(&mut self, data: ClientUpdateData) -> bool {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    50
        self.sender
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    51
            .send(ClientUpdate {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    52
                client_id: self.client_id,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    53
                data,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    54
            })
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    55
            .await
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    56
            .is_ok()
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    57
    }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    58
}
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
    59
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    60
enum ClientStream {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    61
    Tcp(TcpStream),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    62
    #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    63
    Tls(TlsStream<TcpStream>),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    64
}
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    65
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    66
impl Unpin for ClientStream {}
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    67
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    68
impl AsyncRead for ClientStream {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    69
    fn poll_read(
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    70
        self: Pin<&mut Self>,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    71
        cx: &mut Context<'_>,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    72
        buf: &mut ReadBuf<'_>,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    73
    ) -> Poll<std::io::Result<()>> {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    74
        use ClientStream::*;
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    75
        match Pin::into_inner(self) {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    76
            Tcp(stream) => Pin::new(stream).poll_read(cx, buf),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    77
            #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    78
            Tls(stream) => Pin::new(stream).poll_read(cx, buf),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    79
        }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    80
    }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    81
}
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    82
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    83
impl AsyncWrite for ClientStream {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    84
    fn poll_write(
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    85
        self: Pin<&mut Self>,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    86
        cx: &mut Context<'_>,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    87
        buf: &[u8],
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    88
    ) -> Poll<Result<usize, Error>> {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    89
        use ClientStream::*;
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    90
        match Pin::into_inner(self) {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    91
            Tcp(stream) => Pin::new(stream).poll_write(cx, buf),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    92
            #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    93
            Tls(stream) => Pin::new(stream).poll_write(cx, buf),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    94
        }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    95
    }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    96
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    97
    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    98
        use ClientStream::*;
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
    99
        match Pin::into_inner(self) {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   100
            Tcp(stream) => Pin::new(stream).poll_flush(cx),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   101
            #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   102
            Tls(stream) => Pin::new(stream).poll_flush(cx),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   103
        }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   104
    }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   105
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   106
    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   107
        use ClientStream::*;
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   108
        match Pin::into_inner(self) {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   109
            Tcp(stream) => Pin::new(stream).poll_shutdown(cx),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   110
            #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   111
            Tls(stream) => Pin::new(stream).poll_shutdown(cx),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   112
        }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   113
    }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   114
}
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   115
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   116
struct NetworkClient {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   117
    id: ClientId,
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   118
    stream: ClientStream,
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   119
    receiver: Receiver<Bytes>,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   120
    peer_addr: SocketAddr,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   121
    decoder: ProtocolDecoder,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   122
}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   123
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   124
impl NetworkClient {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   125
    fn new(
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   126
        id: ClientId,
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   127
        stream: ClientStream,
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   128
        peer_addr: SocketAddr,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   129
        receiver: Receiver<Bytes>,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   130
    ) -> Self {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   131
        Self {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   132
            id,
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   133
            stream,
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   134
            peer_addr,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   135
            receiver,
15832
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   136
            decoder: ProtocolDecoder::new(PING_TIMEOUT),
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   137
        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   138
    }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   139
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   140
    async fn read<T: AsyncRead + AsyncWrite + Unpin>(
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   141
        stream: &mut T,
15832
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   142
        decoder: &mut ProtocolDecoder,
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   143
    ) -> protocol::Result<HwProtocolMessage> {
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   144
        let result = decoder.read_from(stream).await;
15832
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   145
        if matches!(result, Err(ProtocolError::Timeout)) {
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   146
            if Self::write(stream, Bytes::from(HwServerMessage::Ping.to_raw_protocol())).await {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   147
                decoder.read_from(stream).await
15832
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   148
            } else {
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   149
                Err(ProtocolError::Eof)
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   150
            }
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   151
        } else {
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   152
            result
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   153
        }
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   154
    }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   155
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   156
    async fn write<T: AsyncWrite + Unpin>(stream: &mut T, mut data: Bytes) -> bool {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   157
        !data.has_remaining() || matches!(stream.write_buf(&mut data).await, Ok(n) if n > 0)
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   158
    }
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   159
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   160
    async fn run(mut self, sender: Sender<ClientUpdate>) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   161
        use ClientUpdateData::*;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   162
        let mut sender = ClientUpdateSender {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   163
            client_id: self.id,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   164
            sender,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   165
        };
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   166
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   167
        loop {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   168
            tokio::select! {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   169
                server_message = self.receiver.recv() => {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   170
                    match server_message {
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   171
                        Some(message) => if !Self::write(&mut self.stream, message).await {
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   172
                            sender.send(Error("Connection reset by peer".to_string())).await;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   173
                            break;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   174
                        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   175
                        None => {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   176
                            break;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   177
                        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   178
                    }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   179
                }
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   180
                client_message = Self::read(&mut self.stream, &mut self.decoder) => {
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   181
                     match client_message {
15832
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   182
                        Ok(message) => {
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   183
                            if !sender.send(Message(message)).await {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   184
                                break;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   185
                            }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   186
                        }
15832
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   187
                        Err(e) => {
15938
ce47259d5c86 add some server todos
alfadur
parents: 15937
diff changeset
   188
                            todo!("send cmdline errors");
15832
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   189
                            sender.send(Error(format!("{}", e))).await;
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   190
                            if matches!(e, ProtocolError::Timeout) {
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   191
                                Self::write(&mut self.stream, Bytes::from(HwServerMessage::Bye("Ping timeout".to_string()).to_raw_protocol())).await;
15832
a4d505a32879 add back client timeouts
alfadur
parents: 15831
diff changeset
   192
                            }
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   193
                            break;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   194
                        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   195
                    }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   196
                }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   197
            }
13799
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   198
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   199
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   200
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   201
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   202
#[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   203
struct TlsListener {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   204
    listener: TcpListener,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   205
    acceptor: TlsAcceptor,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   206
}
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   207
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   208
pub struct NetworkLayer {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   209
    listener: TcpListener,
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   210
    #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   211
    tls: TlsListener,
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   212
    server_state: ServerState,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   213
    clients: Slab<Sender<Bytes>>,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   214
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   215
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   216
impl NetworkLayer {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   217
    pub async fn run(&mut self) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   218
        let (update_tx, mut update_rx) = channel(128);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   219
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   220
        async fn accept_plain_branch(
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   221
            layer: &mut NetworkLayer,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   222
            value: (TcpStream, SocketAddr),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   223
            update_tx: Sender<ClientUpdate>,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   224
        ) {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   225
            let (stream, addr) = value;
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   226
            if let Some(client) = layer.create_client(ClientStream::Tcp(stream), addr).await {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   227
                tokio::spawn(client.run(update_tx));
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   228
            }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   229
        }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   230
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   231
        #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   232
        async fn accept_tls_branch(
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   233
            layer: &mut NetworkLayer,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   234
            value: (TcpStream, SocketAddr),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   235
            update_tx: Sender<ClientUpdate>,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   236
        ) {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   237
            let (stream, addr) = value;
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   238
            match layer.tls.acceptor.accept(stream).await {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   239
                Ok(stream) => {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   240
                    if let Some(client) = layer.create_client(ClientStream::Tls(stream), addr).await
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   241
                    {
15937
e514ceb5e7d6 separate out DB task
alfadur
parents: 15936
diff changeset
   242
                        tokio::spawn(client.run(update_tx));
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   243
                    }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   244
                }
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   245
                Err(e) => {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   246
                    warn!("Unable to establish TLS connection: {}", e);
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   247
                }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   248
            }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   249
        }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   250
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   251
        async fn client_message_branch(
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   252
            layer: &mut NetworkLayer,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   253
            client_message: Option<ClientUpdate>,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   254
        ) {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   255
            use ClientUpdateData::*;
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   256
            match client_message {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   257
                Some(ClientUpdate {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   258
                    client_id,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   259
                    data: Message(message),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   260
                }) => {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   261
                    layer.handle_message(client_id, message).await;
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   262
                }
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   263
                Some(ClientUpdate {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   264
                    client_id,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   265
                    data: Error(e),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   266
                }) => {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   267
                    let mut response = handlers::Response::new(client_id);
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   268
                    info!("Client {} error: {:?}", client_id, e);
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   269
                    response.remove_client(client_id);
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   270
                    handlers::handle_client_loss(&mut layer.server_state, client_id, &mut response);
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   271
                    layer.handle_response(response).await;
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   272
                }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   273
                None => unreachable!(),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   274
            }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   275
        }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   276
15938
ce47259d5c86 add some server todos
alfadur
parents: 15937
diff changeset
   277
        todo!("add the DB task");
ce47259d5c86 add some server todos
alfadur
parents: 15937
diff changeset
   278
        todo!("add certfile watcher task");
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   279
        loop {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   280
            #[cfg(not(feature = "tls-connections"))]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   281
            tokio::select! {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   282
                Ok(value) = self.listener.accept() => accept_plain_branch(self, value, update_tx.clone()).await,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   283
                client_message = update_rx.recv(), if !self.clients.is_empty() => client_message_branch(self, client_message).await
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   284
            }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   285
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   286
            #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   287
            tokio::select! {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   288
                Ok(value) = self.listener.accept() => accept_plain_branch(self, value, update_tx.clone()).await,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   289
                Ok(value) = self.tls.listener.accept() => accept_tls_branch(self, value, update_tx.clone()).await,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   290
                client_message = update_rx.recv(), if !self.clients.is_empty() => client_message_branch(self, client_message).await
13802
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   291
            }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   292
        }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   293
    }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13799
diff changeset
   294
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   295
    async fn create_client(
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   296
        &mut self,
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   297
        stream: ClientStream,
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   298
        addr: SocketAddr,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   299
    ) -> Option<NetworkClient> {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   300
        let entry = self.clients.vacant_entry();
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   301
        let client_id = entry.key();
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   302
        let (tx, rx) = channel(16);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   303
        entry.insert(tx);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   304
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   305
        let client = NetworkClient::new(client_id, stream, addr, rx);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   306
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   307
        info!("client {} ({}) added", client.id, client.peer_addr);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   308
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   309
        let mut response = handlers::Response::new(client_id);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   310
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   311
        let added = if let IpAddr::V4(addr) = client.peer_addr.ip() {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   312
            handlers::handle_client_accept(
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   313
                &mut self.server_state,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   314
                client_id,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   315
                &mut response,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   316
                addr.octets(),
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   317
                addr.is_loopback(),
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   318
            )
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   319
        } else {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   320
            todo!("implement something")
15800
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   321
        };
6af892a0a4b8 update mio
alfadur
parents: 15531
diff changeset
   322
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   323
        self.handle_response(response).await;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   324
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   325
        if added {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   326
            Some(client)
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   327
        } else {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   328
            None
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   329
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   330
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   331
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   332
    async fn handle_message(&mut self, client_id: ClientId, message: HwProtocolMessage) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   333
        debug!("Handling message {:?} for client {}", message, client_id);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   334
        let mut response = handlers::Response::new(client_id);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   335
        handlers::handle(&mut self.server_state, client_id, &mut response, message);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   336
        self.handle_response(response).await;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   337
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   338
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   339
    async fn handle_response(&mut self, mut response: handlers::Response) {
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   340
        if response.is_empty() {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   341
            return;
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   342
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   343
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   344
        debug!("{} pending server messages", response.len());
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
   345
        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
   346
        for (clients, message) in output {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   347
            debug!("Message {:?} to {:?}", message, clients);
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   348
            Self::send_message(&mut self.clients, message, clients.iter().cloned()).await;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   349
        }
14696
8a45c90f4580 fix client removal
alfadur
parents: 14693
diff changeset
   350
8a45c90f4580 fix client removal
alfadur
parents: 14693
diff changeset
   351
        for client_id in response.extract_removed_clients() {
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   352
            if self.clients.contains(client_id) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   353
                self.clients.remove(client_id);
14803
92225a708bda add client timeouts
alfadur
parents: 14796
diff changeset
   354
            }
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   355
            info!("Client {} removed", client_id);
15517
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15176
diff changeset
   356
        }
14835
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   357
    }
57ed3981db20 add redirect message
alfadur
parents: 14830
diff changeset
   358
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   359
    async fn send_message<I>(
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   360
        clients: &mut Slab<Sender<Bytes>>,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   361
        message: HwServerMessage,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   362
        to_clients: I,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   363
    ) where
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   364
        I: Iterator<Item = ClientId>,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   365
    {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   366
        let msg_string = message.to_raw_protocol();
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   367
        let bytes = Bytes::copy_from_slice(msg_string.as_bytes());
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   368
        for client_id in to_clients {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   369
            if let Some(client) = clients.get_mut(client_id) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   370
                if !client.send(bytes.clone()).await.is_ok() {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   371
                    clients.remove(client_id);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   372
                }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   373
            }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   374
        }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   375
    }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   376
}
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   377
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   378
pub struct NetworkLayerBuilder {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   379
    listener: Option<TcpListener>,
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   380
    #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   381
    tls_listener: Option<TcpListener>,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   382
    #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   383
    tls_acceptor: Option<TlsAcceptor>,
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   384
    clients_capacity: usize,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   385
    rooms_capacity: usize,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   386
}
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   387
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   388
impl Default for NetworkLayerBuilder {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   389
    fn default() -> Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   390
        Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   391
            clients_capacity: 1024,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   392
            rooms_capacity: 512,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   393
            listener: None,
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   394
            #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   395
            tls_listener: None,
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   396
            #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   397
            tls_acceptor: None,
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   398
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   399
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   400
}
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   401
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   402
impl NetworkLayerBuilder {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   403
    pub fn with_listener(self, listener: TcpListener) -> Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   404
        Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   405
            listener: Some(listener),
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   406
            ..self
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   407
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   408
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   409
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   410
    #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   411
    pub fn with_tls_acceptor(self, listener: TlsAcceptor) -> Self {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   412
        Self {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   413
            tls_acceptor: Option::from(listener),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   414
            ..self
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   415
        }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   416
    }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   417
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   418
    #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   419
    pub fn with_tls_listener(self, listener: TlsAcceptor) -> Self {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   420
        Self {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   421
            tls_acceptor: Option::from(listener),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   422
            ..self
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   423
        }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   424
    }
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   425
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   426
    pub fn build(self) -> NetworkLayer {
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
   427
        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
   428
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   429
        let clients = Slab::with_capacity(self.clients_capacity);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   430
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   431
        NetworkLayer {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   432
            listener: self.listener.expect("No listener provided"),
15936
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   433
            #[cfg(feature = "tls-connections")]
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   434
            tls: TlsListener {
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   435
                listener: self.tls_listener.expect("No TLS listener provided"),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   436
                acceptor: self.tls_acceptor.expect("No TLS acceptor provided"),
c5c53ebb2d91 add back server TLS support
alfadur
parents: 15832
diff changeset
   437
            },
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15517
diff changeset
   438
            server_state,
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   439
            clients,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   440
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   441
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14807
diff changeset
   442
}