summaryrefslogtreecommitdiffstats
path: root/src/bin/server/handler.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-10-29 20:43:09 +0100
committersalmonik <salmonik@tuta.io>2026-10-29 20:43:09 +0100
commit6e2787d792de64c003eff03b62a2df798ef851c0 (patch)
tree0e9bf97b2f71df5fb9eb00d5195d32b8326886ff /src/bin/server/handler.rs
parentv2.2.1: forfeit blackjack bets when leaving or disconnecting (diff)
downloadzagan-bet-6e2787d792de64c003eff03b62a2df798ef851c0.tar.gz
zagan-bet-6e2787d792de64c003eff03b62a2df798ef851c0.zip
v2.3.0: add crash with local multiplier and pushed crash event
Diffstat (limited to 'src/bin/server/handler.rs')
-rw-r--r--src/bin/server/handler.rs55
1 files changed, 40 insertions, 15 deletions
diff --git a/src/bin/server/handler.rs b/src/bin/server/handler.rs
index 86b09cb..dffae69 100644
--- a/src/bin/server/handler.rs
+++ b/src/bin/server/handler.rs
@@ -1,6 +1,11 @@
1use std::time::Duration;
2
1use crate::tls::TlsStream; 3use crate::tls::TlsStream;
2use zagan_bet::server::core::{ConnectionState, ServerCore}; 4use zagan_bet::server::core::{ConnectionState, ServerCore};
3 5
6/// How long the connection waits for a request before advancing timed games.
7const POLL_INTERVAL: Duration = Duration::from_millis(50);
8
4pub fn handle_client_tls(mut stream: TlsStream) { 9pub fn handle_client_tls(mut stream: TlsStream) {
5 let client_addr = match stream.peer_addr() { 10 let client_addr = match stream.peer_addr() {
6 Ok(addr) => addr.to_string(), 11 Ok(addr) => addr.to_string(),
@@ -19,34 +24,54 @@ pub fn handle_client_tls(mut stream: TlsStream) {
19 } 24 }
20 }; 25 };
21 26
27 if let Err(error) = stream.set_read_timeout(Some(POLL_INTERVAL)) {
28 eprintln!("Could not set read timeout: {error}");
29 return;
30 }
31
22 let mut state = ConnectionState::default(); 32 let mut state = ConnectionState::default();
23 println!("[TLS Client connected] {client_addr}"); 33 println!("[TLS Client connected] {client_addr}");
24 34
25 loop { 35 'connection: loop {
26 let mut line = String::new(); 36 let mut line = String::new();
27 match stream.read_line(&mut line) { 37 match stream.read_line(&mut line) {
28 Ok(0) => break, 38 Ok(0) => break,
29 Ok(_) => {} 39 Ok(_) => {
40 let line = line.trim();
41 if !line.is_empty() {
42 let user = state.user().unwrap_or("guest").to_string();
43 println!("[{user} @ {client_addr}] Received: {line}");
44 let response = core.handle_message(&mut state, line);
45 if let Err(error) = stream.write_line(&response) {
46 eprintln!("Write error: {error}");
47 break;
48 }
49 let user = state.user().unwrap_or("guest");
50 println!("[{user} @ {client_addr}] Sent: {response}");
51 }
52 }
53 Err(error)
54 if matches!(
55 error.kind(),
56 std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
57 ) =>
58 {
59 // No request yet: fall through and let timed games advance.
60 }
30 Err(error) => { 61 Err(error) => {
31 eprintln!("Read error: {error}"); 62 eprintln!("Read error: {error}");
32 break; 63 break;
33 } 64 }
34 } 65 }
35 66
36 let line = line.trim(); 67 for push in core.poll(&state) {
37 if line.is_empty() { 68 if let Err(error) = stream.write_line(&push) {
38 continue; 69 eprintln!("Write error: {error}");
39 } 70 break 'connection;
40 71 }
41 let user = state.user().unwrap_or("guest").to_string(); 72 let user = state.user().unwrap_or("guest");
42 println!("[{user} @ {client_addr}] Received: {line}"); 73 println!("[{user} @ {client_addr}] Pushed: {push}");
43 let response = core.handle_message(&mut state, line);
44 if let Err(error) = stream.write_line(&response) {
45 eprintln!("Write error: {error}");
46 break;
47 } 74 }
48 let user = state.user().unwrap_or("guest");
49 println!("[{user} @ {client_addr}] Sent: {response}");
50 } 75 }
51 76
52 core.disconnect(&state); 77 core.disconnect(&state);