diff options
| author | salmonik <salmonik@tuta.io> | 2026-10-29 20:43:09 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-10-29 20:43:09 +0100 |
| commit | 6e2787d792de64c003eff03b62a2df798ef851c0 (patch) | |
| tree | 0e9bf97b2f71df5fb9eb00d5195d32b8326886ff /src/bin/client/tls.rs | |
| parent | v2.2.1: forfeit blackjack bets when leaving or disconnecting (diff) | |
| download | zagan-bet-6e2787d792de64c003eff03b62a2df798ef851c0.tar.gz zagan-bet-6e2787d792de64c003eff03b62a2df798ef851c0.zip | |
v2.3.0: add crash with local multiplier and pushed crash event
Diffstat (limited to '')
| -rw-r--r-- | src/bin/client/tls.rs | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/src/bin/client/tls.rs b/src/bin/client/tls.rs index 8c052f7..b46f52b 100644 --- a/src/bin/client/tls.rs +++ b/src/bin/client/tls.rs | |||
| @@ -64,6 +64,7 @@ impl TlsConnector { | |||
| 64 | sock, | 64 | sock, |
| 65 | tls: conn, | 65 | tls: conn, |
| 66 | lines: LineBuffer::new(), | 66 | lines: LineBuffer::new(), |
| 67 | read_timeout: Some(std::time::Duration::from_secs(10)), | ||
| 67 | })), | 68 | })), |
| 68 | }) | 69 | }) |
| 69 | } | 70 | } |
| @@ -150,6 +151,7 @@ struct Session { | |||
| 150 | sock: TcpStream, | 151 | sock: TcpStream, |
| 151 | tls: rustls::ClientConnection, | 152 | tls: rustls::ClientConnection, |
| 152 | lines: LineBuffer, | 153 | lines: LineBuffer, |
| 154 | read_timeout: Option<std::time::Duration>, | ||
| 153 | } | 155 | } |
| 154 | 156 | ||
| 155 | impl Session { | 157 | impl Session { |
| @@ -202,7 +204,61 @@ impl TlsStream { | |||
| 202 | &self, | 204 | &self, |
| 203 | duration: Option<std::time::Duration>, | 205 | duration: Option<std::time::Duration>, |
| 204 | ) -> std::io::Result<()> { | 206 | ) -> std::io::Result<()> { |
| 205 | self.session.lock().sock.set_read_timeout(duration) | 207 | let mut s = self.session.lock(); |
| 208 | s.read_timeout = duration; | ||
| 209 | s.sock.set_read_timeout(duration) | ||
| 210 | } | ||
| 211 | |||
| 212 | /// Return one complete line if the socket already has one, without | ||
| 213 | /// blocking. Used to pick up server pushes between requests. | ||
| 214 | pub fn read_available_line(&mut self) -> std::io::Result<Option<String>> { | ||
| 215 | let mut s = self.session.lock(); | ||
| 216 | if let Some(line) = s.lines.next_line() { | ||
| 217 | return Ok(Some(line)); | ||
| 218 | } | ||
| 219 | |||
| 220 | // `set_read_timeout(Some(0))` is rejected by std, so poll with the | ||
| 221 | // shortest timeout the socket accepts instead of a nonblocking mode. | ||
| 222 | s.sock | ||
| 223 | .set_read_timeout(Some(std::time::Duration::from_millis(1)))?; | ||
| 224 | let drained = (|| -> std::io::Result<()> { | ||
| 225 | loop { | ||
| 226 | let mut buf = [0u8; 4096]; | ||
| 227 | match s.tls.reader().read(&mut buf) { | ||
| 228 | Ok(0) => return Ok(()), | ||
| 229 | Ok(n) => s.lines.push(&buf[..n]), | ||
| 230 | // The reader only drains plaintext rustls has already | ||
| 231 | // decrypted; `drive` moves bytes off the socket. | ||
| 232 | Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => { | ||
| 233 | match s.drive() { | ||
| 234 | Ok(()) => continue, | ||
| 235 | Err(error) | ||
| 236 | if matches!( | ||
| 237 | error.kind(), | ||
| 238 | std::io::ErrorKind::WouldBlock | ||
| 239 | | std::io::ErrorKind::TimedOut | ||
| 240 | ) => | ||
| 241 | { | ||
| 242 | return Ok(()); | ||
| 243 | } | ||
| 244 | Err(error) => return Err(error), | ||
| 245 | } | ||
| 246 | } | ||
| 247 | Err(error) | ||
| 248 | if matches!( | ||
| 249 | error.kind(), | ||
| 250 | std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut | ||
| 251 | ) => | ||
| 252 | { | ||
| 253 | return Ok(()); | ||
| 254 | } | ||
| 255 | Err(error) => return Err(error), | ||
| 256 | } | ||
| 257 | } | ||
| 258 | })(); | ||
| 259 | s.sock.set_read_timeout(s.read_timeout)?; | ||
| 260 | drained?; | ||
| 261 | Ok(s.lines.next_line()) | ||
| 206 | } | 262 | } |
| 207 | } | 263 | } |
| 208 | 264 | ||
