bornera_rustls/
application.rs1use std::io::{self, Read, Write};
4
5use crate::{RustlsTransport, transport::EofState};
6
7impl Read for RustlsTransport {
8 fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
9 let read = self.tls.reader().read(buffer)?;
10 self.plaintext_bytes = self.plaintext_bytes.saturating_sub(read);
11 Ok(read)
12 }
13}
14
15impl Write for RustlsTransport {
16 fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
17 if buffer.is_empty() {
18 return Ok(0);
19 }
20 if !self.can_accept_plaintext() {
21 return Err(io::ErrorKind::WouldBlock.into());
22 }
23 match self.tls.writer().write(buffer)? {
24 0 => Err(io::ErrorKind::WouldBlock.into()),
25 written => {
26 if let Err(error) = self.refresh_state(bornera::TransportFailurePhase::Write) {
27 self.record_pending_error(error.diagnostic());
28 }
29 Ok(written)
30 }
31 }
32 }
33
34 fn flush(&mut self) -> io::Result<()> {
35 if self.tls.wants_write() {
36 Err(io::ErrorKind::WouldBlock.into())
37 } else {
38 Ok(())
39 }
40 }
41}
42
43impl RustlsTransport {
44 pub(crate) fn can_read_plaintext(&self) -> bool {
45 self.plaintext_bytes != 0 || self.eof == EofState::Clean
46 }
47
48 pub(crate) fn can_accept_plaintext(&self) -> bool {
49 !self.tls.wants_write() && self.can_raw_write()
50 }
51
52 pub(crate) fn can_raw_write(&self) -> bool {
53 self.readiness.intersects(
54 calandria::Readiness::WRITABLE
55 .union(calandria::Readiness::WRITE_CLOSED)
56 .union(calandria::Readiness::ERROR),
57 )
58 }
59}