Switch to tokio-io 0.2

This commit is contained in:
quininer 2019-07-13 17:34:52 +08:00
parent b7925003e2
commit 2f4419b285
5 changed files with 35 additions and 30 deletions

View File

@ -17,7 +17,8 @@ appveyor = { repository = "quininer/tokio-rustls" }
[dependencies] [dependencies]
smallvec = "0.6" smallvec = "0.6"
futures = { package = "futures-preview", version = "0.3.0-alpha.16" } tokio-io = { git = "https://github.com/tokio-rs/tokio" }
tokio-futures = { git = "https://github.com/tokio-rs/tokio" }
rustls = "0.15" rustls = "0.15"
webpki = "0.19" webpki = "0.19"
@ -25,6 +26,6 @@ webpki = "0.19"
early-data = [] early-data = []
[dev-dependencies] [dev-dependencies]
romio = "0.3.0-alpha.8" tokio = { git = "https://github.com/tokio-rs/tokio" }
lazy_static = "1" lazy_static = "1"
webpki-roots = "0.16" webpki-roots = "0.16"

View File

@ -74,8 +74,8 @@ impl<IO> AsyncRead for TlsStream<IO>
where where
IO: AsyncRead + AsyncWrite + Unpin, IO: AsyncRead + AsyncWrite + Unpin,
{ {
unsafe fn initializer(&self) -> Initializer { unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
Initializer::nop() self.io.prepare_uninitialized_buffer(buf)
} }
fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> { fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
@ -192,7 +192,7 @@ where
stream.as_mut_pin().poll_flush(cx) stream.as_mut_pin().poll_flush(cx)
} }
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
if self.state.writeable() { if self.state.writeable() {
self.session.send_close_notify(); self.session.send_close_notify();
self.state.shutdown_write(); self.state.shutdown_write();
@ -201,6 +201,6 @@ where
let this = self.get_mut(); let this = self.get_mut();
let mut stream = Stream::new(&mut this.io, &mut this.session) let mut stream = Stream::new(&mut this.io, &mut this.session)
.set_eof(!this.state.readable()); .set_eof(!this.state.readable());
stream.as_mut_pin().poll_close(cx) stream.as_mut_pin().poll_shutdown(cx)
} }
} }

View File

@ -1,11 +1,10 @@
use std::pin::Pin; use std::pin::Pin;
use std::task::Poll; use std::task::{ Poll, Context };
use std::marker::Unpin; use std::marker::Unpin;
use std::io::{ self, Read }; use std::io::{ self, Read, Write };
use rustls::{ Session, WriteV }; use rustls::Session;
use futures::task::Context; use tokio_io::{ AsyncRead, AsyncWrite };
use futures::io::{ AsyncRead, AsyncWrite, IoSlice }; use tokio_futures as futures;
use smallvec::SmallVec;
pub struct Stream<'a, IO, S> { pub struct Stream<'a, IO, S> {
@ -154,27 +153,31 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin, S: Session> Stream<'a, IO, S> {
impl<'a, IO: AsyncRead + AsyncWrite + Unpin, S: Session> WriteTls<IO, S> for Stream<'a, IO, S> { impl<'a, IO: AsyncRead + AsyncWrite + Unpin, S: Session> WriteTls<IO, S> for Stream<'a, IO, S> {
fn write_tls(&mut self, cx: &mut Context) -> io::Result<usize> { fn write_tls(&mut self, cx: &mut Context) -> io::Result<usize> {
struct Writer<'a, 'b, IO> { // TODO writev
io: &'a mut IO,
struct Writer<'a, 'b, T> {
io: &'a mut T,
cx: &'a mut Context<'b> cx: &'a mut Context<'b>
} }
impl<'a, 'b, IO: AsyncWrite + Unpin> WriteV for Writer<'a, 'b, IO> { impl<'a, 'b, T: AsyncWrite + Unpin> Write for Writer<'a, 'b, T> {
fn writev(&mut self, vbytes: &[&[u8]]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let vbytes = vbytes match Pin::new(&mut self.io).poll_write(self.cx, buf) {
.into_iter() Poll::Ready(result) => result,
.map(|v| IoSlice::new(v)) Poll::Pending => Err(io::ErrorKind::WouldBlock.into())
.collect::<SmallVec<[IoSlice<'_>; 64]>>(); }
}
match Pin::new(&mut self.io).poll_write_vectored(self.cx, &vbytes) { fn flush(&mut self) -> io::Result<()> {
match Pin::new(&mut self.io).poll_flush(self.cx) {
Poll::Ready(result) => result, Poll::Ready(result) => result,
Poll::Pending => Err(io::ErrorKind::WouldBlock.into()) Poll::Pending => Err(io::ErrorKind::WouldBlock.into())
} }
} }
} }
let mut vecio = Writer { io: self.io, cx }; let mut writer = Writer { io: self.io, cx };
self.session.writev_tls(&mut vecio) self.session.write_tls(&mut writer)
} }
} }
@ -240,13 +243,13 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin, S: Session> AsyncWrite for Stream<'
Pin::new(&mut this.io).poll_flush(cx) Pin::new(&mut this.io).poll_flush(cx)
} }
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let this = self.get_mut(); let this = self.get_mut();
while this.session.wants_write() { while this.session.wants_write() {
futures::ready!(this.complete_inner_io(cx, Focus::Writable))?; futures::ready!(this.complete_inner_io(cx, Focus::Writable))?;
} }
Pin::new(&mut this.io).poll_close(cx) Pin::new(&mut this.io).poll_shutdown(cx)
} }
} }

View File

@ -12,7 +12,8 @@ use std::sync::Arc;
use std::pin::Pin; use std::pin::Pin;
use std::future::Future; use std::future::Future;
use std::task::{ Poll, Context }; use std::task::{ Poll, Context };
use futures::io::{ AsyncRead, AsyncWrite, Initializer }; use tokio_io::{ AsyncRead, AsyncWrite };
use tokio_futures as futures;
use rustls::{ ClientConfig, ClientSession, ServerConfig, ServerSession }; use rustls::{ ClientConfig, ClientSession, ServerConfig, ServerSession };
use webpki::DNSNameRef; use webpki::DNSNameRef;
use common::Stream; use common::Stream;

View File

@ -67,8 +67,8 @@ impl<IO> AsyncRead for TlsStream<IO>
where where
IO: AsyncRead + AsyncWrite + Unpin, IO: AsyncRead + AsyncWrite + Unpin,
{ {
unsafe fn initializer(&self) -> Initializer { unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
Initializer::nop() self.io.prepare_uninitialized_buffer(buf)
} }
fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> { fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
@ -119,7 +119,7 @@ where
stream.as_mut_pin().poll_flush(cx) stream.as_mut_pin().poll_flush(cx)
} }
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
if self.state.writeable() { if self.state.writeable() {
self.session.send_close_notify(); self.session.send_close_notify();
self.state.shutdown_write(); self.state.shutdown_write();
@ -128,6 +128,6 @@ where
let this = self.get_mut(); let this = self.get_mut();
let mut stream = Stream::new(&mut this.io, &mut this.session) let mut stream = Stream::new(&mut this.io, &mut this.session)
.set_eof(!this.state.readable()); .set_eof(!this.state.readable());
stream.as_mut_pin().poll_close(cx) stream.as_mut_pin().poll_shutdown(cx)
} }
} }