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