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