rust/hedgewars-server/src/server/network.rs
author alfadur
Tue, 01 Feb 2022 02:23:35 +0300
changeset 15853 7d0f747afcb8
parent 15826 747278149393
child 15854 a4d505a32879
permissions -rw-r--r--
move server network to tokio
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;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
     4
use std::{
13415
0eedc17055a0 rework of prior
alfadur
parents: 13414
diff changeset
     5
    collections::HashSet,
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     6
    io,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     7
    io::{Error, ErrorKind, Read, Write},
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
     8
    iter::Iterator,
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     9
    mem::{replace, swap},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    10
    net::{IpAddr, Ipv4Addr, SocketAddr},
15822
6af892a0a4b8 update mio
alfadur
parents: 15553
diff changeset
    11
    num::NonZeroU32,
6af892a0a4b8 update mio
alfadur
parents: 15553
diff changeset
    12
    time::Duration,
6af892a0a4b8 update mio
alfadur
parents: 15553
diff changeset
    13
    time::Instant,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    14
};
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    15
use tokio::{
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    16
    io::AsyncReadExt,
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    17
    net::{TcpListener, TcpStream},
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    18
    sync::mpsc::{channel, Receiver, Sender},
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    19
};
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    20
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13500
diff changeset
    21
use crate::{
15822
6af892a0a4b8 update mio
alfadur
parents: 15553
diff changeset
    22
    core::{
6af892a0a4b8 update mio
alfadur
parents: 15553
diff changeset
    23
        events::{TimedEvents, Timeout},
6af892a0a4b8 update mio
alfadur
parents: 15553
diff changeset
    24
        types::ClientId,
6af892a0a4b8 update mio
alfadur
parents: 15553
diff changeset
    25
    },
15095
c5a6e8566425 shuffle server files
alfadur
parents: 14937
diff changeset
    26
    handlers,
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15539
diff changeset
    27
    handlers::{IoResult, IoTask, ServerState},
15826
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15822
diff changeset
    28
    protocol::ProtocolDecoder,
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13500
diff changeset
    29
    utils,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    30
};
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    31
use hedgewars_network_protocol::{
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    32
    messages::HwServerMessage::Redirect, messages::*, parser::server_message,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    33
};
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    34
use tokio::io::AsyncWriteExt;
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
    35
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    36
enum ClientUpdateData {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    37
    Message(HwProtocolMessage),
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    38
    Error(String),
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    39
}
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    40
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    41
struct ClientUpdate {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    42
    client_id: ClientId,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    43
    data: ClientUpdateData,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    44
}
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    45
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    46
struct ClientUpdateSender {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    47
    client_id: ClientId,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    48
    sender: Sender<ClientUpdate>,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    49
}
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    50
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    51
impl ClientUpdateSender {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    52
    async fn send(&mut self, data: ClientUpdateData) -> bool {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    53
        self.sender
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    54
            .send(ClientUpdate {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    55
                client_id: self.client_id,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    56
                data,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    57
            })
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    58
            .await
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    59
            .is_ok()
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    60
    }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    61
}
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    62
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    63
struct NetworkClient {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    64
    id: ClientId,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    65
    socket: TcpStream,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    66
    receiver: Receiver<Bytes>,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    67
    peer_addr: SocketAddr,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    68
    decoder: ProtocolDecoder,
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    69
}
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
    70
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    71
impl NetworkClient {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    72
    fn new(
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    73
        id: ClientId,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    74
        socket: TcpStream,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    75
        peer_addr: SocketAddr,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    76
        receiver: Receiver<Bytes>,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    77
    ) -> Self {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    78
        Self {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    79
            id,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    80
            socket,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    81
            peer_addr,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    82
            receiver,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    83
            decoder: ProtocolDecoder::new(),
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    84
        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    85
    }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    86
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    87
    async fn read(&mut self) -> Option<HwProtocolMessage> {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    88
        self.decoder.read_from(&mut self.socket).await
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    89
    }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    90
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    91
    async fn write(&mut self, mut data: Bytes) -> bool {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    92
        !data.has_remaining() || matches!(self.socket.write_buf(&mut data).await, Ok(n) if n > 0)
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    93
    }
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
    94
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    95
    async fn run(mut self, sender: Sender<ClientUpdate>) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    96
        use ClientUpdateData::*;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    97
        let mut sender = ClientUpdateSender {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    98
            client_id: self.id,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
    99
            sender,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   100
        };
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   101
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   102
        loop {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   103
            tokio::select! {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   104
                server_message = self.receiver.recv() => {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   105
                    match server_message {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   106
                        Some(message) => if !self.write(message).await {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   107
                            sender.send(Error("Connection reset by peer".to_string())).await;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   108
                            break;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   109
                        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   110
                        None => {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   111
                            break;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   112
                        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   113
                    }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   114
                }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   115
                client_message = self.decoder.read_from(&mut self.socket) => {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   116
                     match client_message {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   117
                        Some(message) => {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   118
                            if !sender.send(Message(message)).await {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   119
                                break;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   120
                            }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   121
                        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   122
                        None => {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   123
                            sender.send(Error("Connection reset by peer".to_string())).await;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   124
                            break;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   125
                        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   126
                    }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   127
                }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   128
            }
13773
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   129
        }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   130
    }
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   131
}
c8fd12db6215 Add TLS support
alfadur
parents: 13666
diff changeset
   132
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   133
pub struct NetworkLayer {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   134
    listener: TcpListener,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   135
    server_state: ServerState,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   136
    clients: Slab<Sender<Bytes>>,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   137
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   138
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   139
impl NetworkLayer {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   140
    pub async fn run(&mut self) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   141
        let (update_tx, mut update_rx) = channel(128);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   142
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   143
        loop {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   144
            tokio::select! {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   145
                Ok((stream, addr)) = self.listener.accept() => {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   146
                    if let Some(client) = self.create_client(stream, addr).await {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   147
                        tokio::spawn(client.run(update_tx.clone()));
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   148
                    }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   149
                }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   150
                client_message = update_rx.recv(), if !self.clients.is_empty() => {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   151
                    use ClientUpdateData::*;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   152
                    match client_message {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   153
                        Some(ClientUpdate{ client_id, data: Message(message) } ) => {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   154
                            self.handle_message(client_id, message).await;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   155
                        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   156
                        Some(ClientUpdate{ client_id, .. } ) => {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   157
                            let mut response = handlers::Response::new(client_id);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   158
                            handlers::handle_client_loss(&mut self.server_state, client_id, &mut response);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   159
                            self.handle_response(response).await;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   160
                        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   161
                        None => unreachable!()
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   162
                    }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   163
                }
13776
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   164
            }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   165
        }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   166
    }
24fe56d3c6a2 improve error handling for TLS handshake
alfadur
parents: 13773
diff changeset
   167
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   168
    async fn create_client(
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   169
        &mut self,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   170
        stream: TcpStream,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   171
        addr: SocketAddr,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   172
    ) -> Option<NetworkClient> {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   173
        let entry = self.clients.vacant_entry();
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   174
        let client_id = entry.key();
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   175
        let (tx, rx) = channel(16);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   176
        entry.insert(tx);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   177
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   178
        let client = NetworkClient::new(client_id, stream, addr, rx);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   179
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   180
        info!("client {} ({}) added", client.id, client.peer_addr);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   181
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   182
        let mut response = handlers::Response::new(client_id);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   183
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   184
        let added = if let IpAddr::V4(addr) = client.peer_addr.ip() {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   185
            handlers::handle_client_accept(
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   186
                &mut self.server_state,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   187
                client_id,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   188
                &mut response,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   189
                addr.octets(),
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   190
                addr.is_loopback(),
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   191
            )
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   192
        } else {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   193
            todo!("implement something")
15822
6af892a0a4b8 update mio
alfadur
parents: 15553
diff changeset
   194
        };
6af892a0a4b8 update mio
alfadur
parents: 15553
diff changeset
   195
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   196
        self.handle_response(response).await;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   197
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   198
        if added {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   199
            Some(client)
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   200
        } else {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   201
            None
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   202
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   203
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14718
diff changeset
   204
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   205
    async fn handle_message(&mut self, client_id: ClientId, message: HwProtocolMessage) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   206
        debug!("Handling message {:?} for client {}", message, client_id);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   207
        let mut response = handlers::Response::new(client_id);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   208
        handlers::handle(&mut self.server_state, client_id, &mut response, message);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   209
        self.handle_response(response).await;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   210
    }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   211
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   212
    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
   213
        if response.is_empty() {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   214
            return;
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   215
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   216
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
   217
        debug!("{} pending server messages", response.len());
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15539
diff changeset
   218
        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
   219
        for (clients, message) in output {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   220
            debug!("Message {:?} to {:?}", message, clients);
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   221
            Self::send_message(&mut self.clients, message, clients.iter().cloned()).await;
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   222
        }
14717
8a45c90f4580 fix client removal
alfadur
parents: 14714
diff changeset
   223
8a45c90f4580 fix client removal
alfadur
parents: 14714
diff changeset
   224
        for client_id in response.extract_removed_clients() {
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   225
            if self.clients.contains(client_id) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   226
                self.clients.remove(client_id);
14824
92225a708bda add client timeouts
alfadur
parents: 14817
diff changeset
   227
            }
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   228
            info!("Client {} removed", client_id);
15539
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15197
diff changeset
   229
        }
14856
57ed3981db20 add redirect message
alfadur
parents: 14851
diff changeset
   230
    }
57ed3981db20 add redirect message
alfadur
parents: 14851
diff changeset
   231
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   232
    async fn send_message<I>(
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   233
        clients: &mut Slab<Sender<Bytes>>,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   234
        message: HwServerMessage,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   235
        to_clients: I,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   236
    ) where
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   237
        I: Iterator<Item = ClientId>,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   238
    {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   239
        let msg_string = message.to_raw_protocol();
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   240
        let bytes = Bytes::copy_from_slice(msg_string.as_bytes());
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   241
        for client_id in to_clients {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   242
            if let Some(client) = clients.get_mut(client_id) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   243
                if !client.send(bytes.clone()).await.is_ok() {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   244
                    clients.remove(client_id);
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   245
                }
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   246
            }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   247
        }
13414
28b314ad566d handle edge polling properly
alfadur
parents: 13119
diff changeset
   248
    }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   249
}
14851
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   250
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   251
pub struct NetworkLayerBuilder {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   252
    listener: Option<TcpListener>,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   253
    clients_capacity: usize,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   254
    rooms_capacity: usize,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   255
}
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   256
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   257
impl Default for NetworkLayerBuilder {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   258
    fn default() -> Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   259
        Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   260
            clients_capacity: 1024,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   261
            rooms_capacity: 512,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   262
            listener: None,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   263
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   264
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   265
}
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   266
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   267
impl NetworkLayerBuilder {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   268
    pub fn with_listener(self, listener: TcpListener) -> Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   269
        Self {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   270
            listener: Some(listener),
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   271
            ..self
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   272
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   273
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   274
15853
7d0f747afcb8 move server network to tokio
alfadur
parents: 15826
diff changeset
   275
    pub fn build(self) -> NetworkLayer {
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15539
diff changeset
   276
        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
   277
14851
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   278
        let clients = Slab::with_capacity(self.clients_capacity);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   279
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   280
        NetworkLayer {
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   281
            listener: self.listener.expect("No listener provided"),
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15539
diff changeset
   282
            server_state,
14851
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   283
            clients,
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   284
        }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   285
    }
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14828
diff changeset
   286
}