author | unC0Rr |
Sat, 28 Sep 2024 22:27:13 +0200 | |
changeset 16066 | 2b4f361e3891 |
parent 16029 | d9f1b239b6d7 |
permissions | -rw-r--r-- |
15853 | 1 |
use bytes::{Buf, Bytes}; |
2 |
use log::*; |
|
3 |
use slab::Slab; |
|
15966 | 4 |
use std::io::Error; |
5 |
use std::pin::Pin; |
|
6 |
use std::task::{Context, Poll}; |
|
13414 | 7 |
use std::{ |
15853 | 8 |
iter::Iterator, |
15854 | 9 |
net::{IpAddr, SocketAddr}, |
15822 | 10 |
time::Duration, |
13414 | 11 |
}; |
15853 | 12 |
use tokio::{ |
15966 | 13 |
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf}, |
14478 | 14 |
net::{TcpListener, TcpStream}, |
15853 | 15 |
sync::mpsc::{channel, Receiver, Sender}, |
13414 | 16 |
}; |
15966 | 17 |
#[cfg(feature = "tls-connections")] |
18 |
use tokio_native_tls::{TlsAcceptor, TlsStream}; |
|
13119 | 19 |
|
13666 | 20 |
use crate::{ |
15969 | 21 |
core::types::ClientId, |
15095 | 22 |
handlers, |
15542 | 23 |
handlers::{IoResult, IoTask, ServerState}, |
15854 | 24 |
protocol::{self, ProtocolDecoder, ProtocolError}, |
13666 | 25 |
utils, |
13414 | 26 |
}; |
15853 | 27 |
use hedgewars_network_protocol::{ |
28 |
messages::HwServerMessage::Redirect, messages::*, parser::server_message, |
|
29 |
}; |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14718
diff
changeset
|
30 |
|
15854 | 31 |
const PING_TIMEOUT: Duration = Duration::from_secs(15); |
32 |
||
16019
c40f5e27aaf0
fix connection errors carrying over to new clients
alfadur
parents:
16018
diff
changeset
|
33 |
#[derive(Debug)] |
15853 | 34 |
enum ClientUpdateData { |
35 |
Message(HwProtocolMessage), |
|
36 |
Error(String), |
|
37 |
} |
|
38 |
||
16019
c40f5e27aaf0
fix connection errors carrying over to new clients
alfadur
parents:
16018
diff
changeset
|
39 |
#[derive(Debug)] |
15853 | 40 |
struct ClientUpdate { |
41 |
client_id: ClientId, |
|
42 |
data: ClientUpdateData, |
|
43 |
} |
|
13414 | 44 |
|
15853 | 45 |
struct ClientUpdateSender { |
46 |
client_id: ClientId, |
|
47 |
sender: Sender<ClientUpdate>, |
|
48 |
} |
|
13119 | 49 |
|
15853 | 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 |
||
15966 | 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 |
||
15853 | 118 |
struct NetworkClient { |
119 |
id: ClientId, |
|
15966 | 120 |
stream: ClientStream, |
15853 | 121 |
receiver: Receiver<Bytes>, |
122 |
peer_addr: SocketAddr, |
|
123 |
decoder: ProtocolDecoder, |
|
13414 | 124 |
} |
125 |
||
15853 | 126 |
impl NetworkClient { |
127 |
fn new( |
|
128 |
id: ClientId, |
|
15966 | 129 |
stream: ClientStream, |
15853 | 130 |
peer_addr: SocketAddr, |
131 |
receiver: Receiver<Bytes>, |
|
132 |
) -> Self { |
|
133 |
Self { |
|
134 |
id, |
|
15966 | 135 |
stream, |
15853 | 136 |
peer_addr, |
137 |
receiver, |
|
15854 | 138 |
decoder: ProtocolDecoder::new(PING_TIMEOUT), |
15853 | 139 |
} |
140 |
} |
|
13119 | 141 |
|
15966 | 142 |
async fn read<T: AsyncRead + AsyncWrite + Unpin>( |
143 |
stream: &mut T, |
|
15854 | 144 |
decoder: &mut ProtocolDecoder, |
145 |
) -> protocol::Result<HwProtocolMessage> { |
|
15966 | 146 |
let result = decoder.read_from(stream).await; |
15854 | 147 |
if matches!(result, Err(ProtocolError::Timeout)) { |
15966 | 148 |
if Self::write(stream, Bytes::from(HwServerMessage::Ping.to_raw_protocol())).await { |
149 |
decoder.read_from(stream).await |
|
15854 | 150 |
} else { |
151 |
Err(ProtocolError::Eof) |
|
152 |
} |
|
153 |
} else { |
|
154 |
result |
|
155 |
} |
|
15853 | 156 |
} |
157 |
||
15966 | 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) |
|
15853 | 160 |
} |
13773 | 161 |
|
15853 | 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 { |
|
15966 | 173 |
Some(message) => if !Self::write(&mut self.stream, message).await { |
15853 | 174 |
sender.send(Error("Connection reset by peer".to_string())).await; |
175 |
break; |
|
176 |
} |
|
177 |
None => { |
|
178 |
break; |
|
179 |
} |
|
180 |
} |
|
181 |
} |
|
15966 | 182 |
client_message = Self::read(&mut self.stream, &mut self.decoder) => { |
15853 | 183 |
match client_message { |
15854 | 184 |
Ok(message) => { |
16018 | 185 |
//todo!("add flood stats"); |
15853 | 186 |
if !sender.send(Message(message)).await { |
187 |
break; |
|
188 |
} |
|
189 |
} |
|
15854 | 190 |
Err(e) => { |
16018 | 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 | 193 |
sender.send(Error(format!("{}", e))).await; |
194 |
if matches!(e, ProtocolError::Timeout) { |
|
15966 | 195 |
Self::write(&mut self.stream, Bytes::from(HwServerMessage::Bye("Ping timeout".to_string()).to_raw_protocol())).await; |
15854 | 196 |
} |
15853 | 197 |
break; |
198 |
} |
|
199 |
} |
|
200 |
} |
|
201 |
} |
|
13773 | 202 |
} |
203 |
} |
|
204 |
} |
|
205 |
||
15966 | 206 |
#[cfg(feature = "tls-connections")] |
207 |
struct TlsListener { |
|
208 |
listener: TcpListener, |
|
209 |
acceptor: TlsAcceptor, |
|
210 |
} |
|
211 |
||
15853 | 212 |
pub struct NetworkLayer { |
213 |
listener: TcpListener, |
|
15966 | 214 |
#[cfg(feature = "tls-connections")] |
215 |
tls: TlsListener, |
|
15853 | 216 |
server_state: ServerState, |
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 | 219 |
update_rx: Receiver<ClientUpdate>, |
13119 | 220 |
} |
221 |
||
15853 | 222 |
impl NetworkLayer { |
223 |
pub async fn run(&mut self) { |
|
15966 | 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 |
{ |
|
15967 | 246 |
tokio::spawn(client.run(update_tx)); |
15853 | 247 |
} |
248 |
} |
|
15966 | 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; |
|
15853 | 266 |
} |
15966 | 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 |
||
16018 | 281 |
//todo!("add the DB task"); |
282 |
//todo!("add certfile watcher task"); |
|
15966 | 283 |
loop { |
284 |
#[cfg(not(feature = "tls-connections"))] |
|
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 | 288 |
} |
289 |
||
290 |
#[cfg(feature = "tls-connections")] |
|
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 | 295 |
} |
296 |
} |
|
297 |
} |
|
298 |
||
15853 | 299 |
async fn create_client( |
300 |
&mut self, |
|
15966 | 301 |
stream: ClientStream, |
15853 | 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 |
|
15853 | 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") |
|
15822 | 325 |
}; |
326 |
||
15853 | 327 |
self.handle_response(response).await; |
13414 | 328 |
|
15853 | 329 |
if added { |
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 | 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 |
||
15853 | 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 | 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 | 363 |
debug!("Message {:?} to {:?}", message, clients); |
15853 | 364 |
Self::send_message(&mut self.clients, message, clients.iter().cloned()).await; |
13414 | 365 |
} |
14856 | 366 |
} |
367 |
||
15853 | 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 |
} |
14478 | 382 |
} |
13119 | 383 |
} |
13414 | 384 |
} |
13119 | 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 | 389 |
#[cfg(feature = "tls-connections")] |
390 |
tls_listener: Option<TcpListener>, |
|
391 |
#[cfg(feature = "tls-connections")] |
|
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 | 403 |
#[cfg(feature = "tls-connections")] |
404 |
tls_listener: None, |
|
405 |
#[cfg(feature = "tls-connections")] |
|
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 | 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 |
||
15853 | 435 |
pub fn build(self) -> NetworkLayer { |
15542 | 436 |
let server_state = ServerState::new(self.clients_capacity, self.rooms_capacity); |
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 | 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 |
}, |
|
15542 | 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 | 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 |
} |