Skip to content

Commit

Permalink
Upgrade to tokio 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mzohreva committed Nov 24, 2020
1 parent 7a0767e commit 9151be6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mbedtls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ byteorder = "1.0.0"
yasna = { version = "0.2", optional = true }
block-modes = { version = "0.3", optional = true }
rc2 = { version = "0.3", optional = true }
tokio = { version = "0.2", optional = true }
tokio = { version = "0.3", optional = true }

[target.x86_64-fortanix-unknown-sgx.dependencies]
rs-libc = "0.1.0"
Expand Down
22 changes: 12 additions & 10 deletions mbedtls/src/ssl/async_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use std::cell::Cell;
use std::future::Future;
use std::io::{self, Read, Write};
use std::marker::Unpin;
use std::mem::{self, MaybeUninit};
use std::mem;
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use std::ptr::null_mut;
use std::rc::Rc;
use std::task::{Context as TaskContext, Poll};

use tokio::io::{AsyncRead, AsyncWrite};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};

use crate::ssl::{Context, HandshakeError, MidHandshake, Session, Version};
use crate::x509::VerifyError;
Expand Down Expand Up @@ -81,7 +81,9 @@ impl<S: Unpin> IoAdapter<S> {

impl<S: AsyncRead + Unpin> Read for IoAdapter<S> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.with_context(|ctx, stream| stream.poll_read(ctx, buf))
let mut buf = ReadBuf::new(buf);
self.with_context(|ctx, stream| stream.poll_read(ctx, &mut buf))?;
Ok(buf.filled().len())
}
}

Expand Down Expand Up @@ -211,16 +213,16 @@ impl Drop for AsyncSession<'_> {
}

impl AsyncRead for AsyncSession<'_> {
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [MaybeUninit<u8>]) -> bool {
false
}

fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut TaskContext<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
self.with_context(cx, |s| s.read(buf))
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
self.with_context(cx, |s| {
let n = s.read(buf.initialize_unfilled())?;
buf.advance(n);
Ok(())
})
}
}

Expand Down
13 changes: 4 additions & 9 deletions mbedtls/tests/async_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ where
#[cfg(unix)]
mod test {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;

#[tokio::test]
async fn client_server_test() {
Expand Down Expand Up @@ -222,8 +221,7 @@ mod test {
continue;
}

let (c, s) = crate::support::net::create_tcp_pair().unwrap();
let (c, s) = (TcpStream::from_std(c).unwrap(), TcpStream::from_std(s).unwrap());
let (c, s) = crate::support::net::create_tcp_pair_async().unwrap();
let c = tokio::spawn(super::client(c, min_c, max_c, exp_ver.clone()));
let s = tokio::spawn(super::server(s, min_s, max_s, exp_ver));

Expand All @@ -234,8 +232,7 @@ mod test {

#[tokio::test]
async fn shutdown1() {
let (c, s) = crate::support::net::create_tcp_pair().unwrap();
let (c, s) = (TcpStream::from_std(c).unwrap(), TcpStream::from_std(s).unwrap());
let (c, s) = crate::support::net::create_tcp_pair_async().unwrap();

let c = tokio::spawn(super::with_client(c, |session| Box::pin(async move {
session.shutdown().await.unwrap();
Expand All @@ -255,8 +252,7 @@ mod test {

#[tokio::test]
async fn shutdown2() {
let (c, s) = crate::support::net::create_tcp_pair().unwrap();
let (c, s) = (TcpStream::from_std(c).unwrap(), TcpStream::from_std(s).unwrap());
let (c, s) = crate::support::net::create_tcp_pair_async().unwrap();

let c = tokio::spawn(super::with_client(c, |session| Box::pin(async move {
let mut buf = [0u8; 5];
Expand All @@ -279,8 +275,7 @@ mod test {

#[tokio::test]
async fn shutdown3() {
let (c, s) = crate::support::net::create_tcp_pair().unwrap();
let (c, s) = (TcpStream::from_std(c).unwrap(), TcpStream::from_std(s).unwrap());
let (c, s) = crate::support::net::create_tcp_pair_async().unwrap();

let c = tokio::spawn(super::with_client(c, |session| Box::pin(async move {
session.shutdown().await
Expand Down
14 changes: 14 additions & 0 deletions mbedtls/tests/support/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use std::io::{Error as IoError, Result as IoResult};
use std::net::TcpStream;
use std::os::unix::io::FromRawFd;

#[cfg(feature = "tokio")]
use tokio::net;

pub fn create_tcp_pair() -> IoResult<(TcpStream, TcpStream)> {
let mut fds: [libc::c_int; 2] = [0; 2];
unsafe {
Expand All @@ -28,3 +31,14 @@ pub fn create_tcp_pair() -> IoResult<(TcpStream, TcpStream)> {
}
}
}

#[cfg(feature = "tokio")]
pub fn create_tcp_pair_async() -> IoResult<(net::TcpStream, net::TcpStream)> {
let (c, s) = create_tcp_pair()?;
c.set_nonblocking(true)?;
s.set_nonblocking(true)?;
Ok((
net::TcpStream::from_std(c)?,
net::TcpStream::from_std(s)?,
))
}

0 comments on commit 9151be6

Please sign in to comment.