restore h2 impl

This commit is contained in:
Nikolay Kim 2021-12-15 21:32:24 +06:00
parent 3dbba47ab1
commit 399b238621
11 changed files with 180 additions and 147 deletions

View file

@ -425,7 +425,6 @@ impl IoRef {
let is_write_sleep = buf.is_empty();
codec.encode(item, &mut buf).map_err(Either::Left)?;
filter.release_write_buf(buf).map_err(Either::Right)?;
self.0.insert_flags(Flags::WR_WAIT);
if is_write_sleep {
self.0.write_task.wake();
}
@ -681,7 +680,7 @@ impl<'a> WriteRef<'a> {
}
#[inline]
/// Write item to a buffer and wake up write task
/// Encode and write item to a buffer and wake up write task
///
/// Returns write buffer state, false is returned if write buffer if full.
pub fn encode<U>(
@ -724,6 +723,36 @@ impl<'a> WriteRef<'a> {
}
}
#[inline]
/// Write item to a buffer and wake up write task
///
/// Returns write buffer state, false is returned if write buffer if full.
pub fn write(&self, src: &[u8]) -> Result<bool, io::Error> {
let flags = self.0.flags.get();
if !flags.intersects(Flags::IO_ERR | Flags::IO_SHUTDOWN) {
let filter = self.0.filter.get();
let mut buf = filter
.get_write_buf()
.unwrap_or_else(|| self.0.pool.get().get_write_buf());
let is_write_sleep = buf.is_empty();
// write and wake write task
buf.extend_from_slice(src);
let result = buf.len() < self.0.pool.get().write_params_high();
if is_write_sleep {
self.0.write_task.wake();
}
if let Err(err) = filter.release_write_buf(buf) {
self.0.set_error(Some(err));
}
Ok(result)
} else {
Ok(true)
}
}
#[inline]
/// Wake write task and instruct to write data.
///

View file

@ -1,11 +1,12 @@
use std::task::{Context, Poll};
use std::{cell::RefCell, future::Future, io, pin::Pin, rc::Rc};
use std::{cell::RefCell, cmp, future::Future, io, pin::Pin, rc::Rc};
use ntex_bytes::{Buf, BufMut};
use ntex_util::time::{sleep, Sleep};
use tok_io::{io::AsyncRead, io::AsyncWrite, io::ReadBuf, net::TcpStream};
use tok_io::io::{AsyncRead, AsyncWrite, ReadBuf};
use tok_io::net::TcpStream;
use super::{IoStream, ReadContext, WriteContext, WriteReadiness};
use super::{Filter, Io, IoStream, ReadContext, WriteContext, WriteReadiness};
impl IoStream for TcpStream {
fn start(self, read: ReadContext, write: WriteContext) {
@ -340,3 +341,50 @@ pub(super) fn flush_io<T: AsyncRead + AsyncWrite + Unpin>(
Poll::Ready(true)
}
}
impl<F: Filter> AsyncRead for Io<F> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let read = self.read();
let len = read.with_buf(|src| {
let len = cmp::min(src.len(), buf.capacity());
buf.put_slice(&src.split_to(len));
len
});
if len == 0 && !self.0.is_io_open() {
if let Some(err) = self.0.take_error() {
return Poll::Ready(Err(err));
}
}
if read.poll_ready(cx)?.is_ready() {
Poll::Ready(Ok(()))
} else {
Poll::Pending
}
}
}
impl<F: Filter> AsyncWrite for Io<F> {
fn poll_write(
self: Pin<&mut Self>,
_: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(self.write().write(buf).map(|_| buf.len()))
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.write().poll_flush(cx, false)
}
fn poll_shutdown(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<io::Result<()>> {
self.0.poll_shutdown(cx)
}
}