Skip to content

Commit

Permalink
fix safe_io bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah committed Sep 21, 2022
1 parent 90f1499 commit e3d9b3d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ rustls = {version = "0.20", features = ["tls12"]}
rustls-pemfile = "1"
webpki-roots = "0.22"

monoio-rustls = {version = "0.0.5", path = "../monoio-rustls", features = ["tls12"]}
monoio-rustls = {version = "0.0.6", path = "../monoio-rustls", features = ["tls12"]}

[[bin]]
name = "server"
Expand Down
6 changes: 3 additions & 3 deletions monoio-rustls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ license = "MIT/Apache-2.0"
name = "monoio-rustls"
readme = "README.md"
repository = "https://github.com/monoio-rs/monoio-tls"
version = "0.0.5"
version = "0.0.6"

[dependencies]
bytes = {version = "1"}
monoio = {version = "0.0.8", default-features = false, features = ["bytes"]}
monoio = {version = "0.0.8", default-features = false}
rustls = {version = "0.20", default-features = false}
thiserror = {version = "1"}

Expand All @@ -26,5 +26,5 @@ tls12 = ["rustls/tls12"]
unsafe_io = []

[dev-dependencies]
monoio = {version = "0.0.8", features = ["bytes"]}
monoio = {version = "0.0.8"}
webpki-roots = "0.22"
28 changes: 23 additions & 5 deletions monoio-rustls/src/safe_io.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{hint::unreachable_unchecked, io};
use std::{fmt::Debug, hint::unreachable_unchecked, io};

use monoio::{
buf::{IoBuf, IoBufMut},
Expand Down Expand Up @@ -39,7 +39,7 @@ impl Buffer {
}

fn advance(&mut self, n: usize) {
assert!(self.write - self.read <= n);
assert!(self.write - self.read >= n);
self.read += n;
if self.read == self.write {
self.read = 0;
Expand Down Expand Up @@ -78,6 +78,15 @@ pub(crate) struct SafeRead {
status: ReadStatus,
}

impl Debug for SafeRead {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SafeRead")
.field("status", &self.status)
.finish()
}
}

#[derive(Debug)]
enum ReadStatus {
Eof,
Err(io::Error),
Expand Down Expand Up @@ -108,16 +117,16 @@ impl SafeRead {
match result {
Ok(0) => {
self.status = ReadStatus::Eof;
return result;
result
}
Ok(_) => {
self.status = ReadStatus::Ok;
return result;
result
}
Err(e) => {
let rerr = e.kind().into();
self.status = ReadStatus::Err(e);
return Err(rerr);
Err(rerr)
}
}
}
Expand Down Expand Up @@ -153,6 +162,15 @@ pub(crate) struct SafeWrite {
status: WriteStatus,
}

impl Debug for SafeWrite {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SafeWrite")
.field("status", &self.status)
.finish()
}
}

#[derive(Debug)]
enum WriteStatus {
Err(io::Error),
Ok,
Expand Down
35 changes: 19 additions & 16 deletions monoio-rustls/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,24 @@ use crate::split::{ReadHalf, WriteHalf};
pub struct Stream<IO, C> {
pub(crate) io: IO,
pub(crate) session: C,
#[cfg(not(feature = "unsafe_io"))]
r_buffer: crate::safe_io::SafeRead,
#[cfg(not(feature = "unsafe_io"))]
w_buffer: crate::safe_io::SafeWrite,
#[cfg(feature = "unsafe_io")]
r_buffer: crate::unsafe_io::UnsafeRead,
#[cfg(feature = "unsafe_io")]
w_buffer: crate::unsafe_io::UnsafeWrite,
}

impl<IO, C> Stream<IO, C> {
pub fn new(io: IO, session: C) -> Self {
Self { io, session }
Self {
io,
session,
r_buffer: Default::default(),
w_buffer: Default::default(),
}
}

pub fn split(self) -> (ReadHalf<IO, C>, WriteHalf<IO, C>) {
Expand All @@ -46,20 +59,15 @@ where
C: DerefMut + Deref<Target = ConnectionCommon<SD>>,
{
pub(crate) async fn read_io(&mut self, splitted: bool) -> io::Result<usize> {
#[cfg(feature = "unsafe_io")]
let mut reader = crate::unsafe_io::UnsafeRead::default();
#[cfg(not(feature = "unsafe_io"))]
let mut reader = crate::safe_io::SafeRead::default();

let n = loop {
match self.session.read_tls(&mut reader) {
match self.session.read_tls(&mut self.r_buffer) {
Ok(n) => {
break n;
}
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => {
#[allow(unused_unsafe)]
unsafe {
reader.do_io(&mut self.io).await?
self.r_buffer.do_io(&mut self.io).await?
};
continue;
}
Expand Down Expand Up @@ -92,20 +100,15 @@ where
}

pub(crate) async fn write_io(&mut self) -> io::Result<usize> {
#[cfg(feature = "unsafe_io")]
let mut writer = crate::unsafe_io::UnsafeWrite::default();
#[cfg(not(feature = "unsafe_io"))]
let mut writer = crate::safe_io::SafeWrite::default();

let n = loop {
match self.session.write_tls(&mut writer) {
match self.session.write_tls(&mut self.w_buffer) {
Ok(n) => {
break n;
}
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => {
#[allow(unused_unsafe)]
unsafe {
writer.do_io(&mut self.io).await?
self.w_buffer.do_io(&mut self.io).await?
};
continue;
}
Expand All @@ -114,7 +117,7 @@ where
};
// Flush buffered data, only needed for safe_io.
#[cfg(not(feature = "unsafe_io"))]
writer.do_io(&mut self.io).await?;
self.w_buffer.do_io(&mut self.io).await?;

Ok(n)
}
Expand Down
5 changes: 3 additions & 2 deletions monoio-rustls/src/unsafe_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use monoio::{
};

/// Used by both UnsafeRead and UnsafeWrite.
#[derive(Debug)]
enum Status {
/// We haven't do real io, and maybe the dest is recorded.
WaitFill(Option<(*const u8, usize)>),
Expand All @@ -27,7 +28,7 @@ impl Default for Status {
/// Note that this action is an unsafe hack to avoid data copy.
/// You can only use this wrapper when you make sure the read dest is always
/// a valid buffer.
#[derive(Default)]
#[derive(Default, Debug)]
pub(crate) struct UnsafeRead {
status: Status,
}
Expand Down Expand Up @@ -72,7 +73,7 @@ impl io::Read for UnsafeRead {
}

/// UnsafeWrite behaves like `UnsafeRead`.
#[derive(Default)]
#[derive(Default, Debug)]
pub(crate) struct UnsafeWrite {
status: Status,
}
Expand Down

0 comments on commit e3d9b3d

Please sign in to comment.