summaryrefslogtreecommitdiffstats
path: root/src/bin/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/server')
-rw-r--r--src/bin/server/tls.rs26
1 files changed, 13 insertions, 13 deletions
diff --git a/src/bin/server/tls.rs b/src/bin/server/tls.rs
index acf3315..62a25db 100644
--- a/src/bin/server/tls.rs
+++ b/src/bin/server/tls.rs
@@ -1,6 +1,8 @@
1use std::io::{Read, Write}; 1use std::io::{Read, Write};
2use std::net::TcpStream; 2use std::net::TcpStream;
3 3
4use zagan_bet::protocol::LineBuffer;
5
4#[derive(Clone)] 6#[derive(Clone)]
5pub struct TlsServer { 7pub struct TlsServer {
6 acceptor: native_tls::TlsAcceptor, 8 acceptor: native_tls::TlsAcceptor,
@@ -42,27 +44,25 @@ impl TlsServer {
42 44
43pub struct TlsStream { 45pub struct TlsStream {
44 stream: native_tls::TlsStream<TcpStream>, 46 stream: native_tls::TlsStream<TcpStream>,
47 lines: LineBuffer,
45} 48}
46 49
47impl TlsStream { 50impl TlsStream {
48 pub fn new(stream: native_tls::TlsStream<TcpStream>) -> Self { 51 pub fn new(stream: native_tls::TlsStream<TcpStream>) -> Self {
49 Self { stream } 52 Self {
53 stream,
54 lines: LineBuffer::new(),
55 }
50 } 56 }
51 57
52 pub fn read_line(&mut self, s: &mut String) -> std::io::Result<usize> { 58 pub fn read_line(&mut self, s: &mut String) -> std::io::Result<usize> {
53 let mut buf = [0u8; 1024]; 59 match self.lines.read_line(&mut self.stream)? {
54 loop { 60 Some(line) => {
55 match self.stream.read(&mut buf) { 61 s.push_str(&line);
56 Ok(0) => return Ok(0), 62 s.push('\n');
57 Ok(n) => { 63 Ok(line.len() + 1)
58 let chunk = std::str::from_utf8(&buf[..n]).unwrap_or("");
59 s.push_str(chunk);
60 if s.contains('\n') {
61 return Ok(n);
62 }
63 }
64 Err(e) => return Err(e),
65 } 64 }
65 None => Ok(0),
66 } 66 }
67 } 67 }
68 68