summaryrefslogtreecommitdiffstats
path: root/src/bin/server/tls.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/server/tls.rs')
-rw-r--r--src/bin/server/tls.rs27
1 files changed, 12 insertions, 15 deletions
diff --git a/src/bin/server/tls.rs b/src/bin/server/tls.rs
index 420b5b4..eb94637 100644
--- a/src/bin/server/tls.rs
+++ b/src/bin/server/tls.rs
@@ -1,7 +1,7 @@
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; 4use zagan_bet::protocol::{FrameBuffer, write_frame};
5 5
6#[derive(Clone)] 6#[derive(Clone)]
7pub struct TlsServer { 7pub struct TlsServer {
@@ -44,34 +44,31 @@ impl TlsServer {
44 44
45pub struct TlsStream { 45pub struct TlsStream {
46 stream: native_tls::TlsStream<TcpStream>, 46 stream: native_tls::TlsStream<TcpStream>,
47 lines: LineBuffer, 47 frames: FrameBuffer,
48} 48}
49 49
50impl TlsStream { 50impl TlsStream {
51 pub fn new(stream: native_tls::TlsStream<TcpStream>) -> Self { 51 pub fn new(stream: native_tls::TlsStream<TcpStream>) -> Self {
52 Self { 52 Self {
53 stream, 53 stream,
54 lines: LineBuffer::new(), 54 frames: FrameBuffer::new(),
55 } 55 }
56 } 56 }
57 57
58 pub fn read_line(&mut self, s: &mut String) -> std::io::Result<usize> { 58 /// Read one protocol frame. `Ok(0)` is EOF with no partial frame.
59 match self.lines.read_line(&mut self.stream)? { 59 pub fn read_frame(&mut self, out: &mut String) -> std::io::Result<usize> {
60 Some(line) => { 60 match self.frames.read_frame(&mut self.stream)? {
61 s.push_str(&line); 61 Some(frame) => {
62 s.push('\n'); 62 let length = frame.len();
63 Ok(line.len() + 1) 63 out.push_str(&frame);
64 Ok(length)
64 } 65 }
65 None => Ok(0), 66 None => Ok(0),
66 } 67 }
67 } 68 }
68 69
69 pub fn write_line(&mut self, msg: &str) -> std::io::Result<()> { 70 pub fn write_frame(&mut self, msg: &str) -> std::io::Result<()> {
70 use std::io::Write; 71 write_frame(&mut self.stream, msg)
71 self.stream.write_all(msg.as_bytes())?;
72 self.stream.write_all(b"\n")?;
73 self.stream.flush()?;
74 Ok(())
75 } 72 }
76 73
77 pub fn peer_addr(&self) -> std::io::Result<std::net::SocketAddr> { 74 pub fn peer_addr(&self) -> std::io::Result<std::net::SocketAddr> {