Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues encountered while using TcpStream.read() #318

Open
piperck opened this issue Dec 11, 2024 · 0 comments
Open

Issues encountered while using TcpStream.read() #318

piperck opened this issue Dec 11, 2024 · 0 comments

Comments

@piperck
Copy link

piperck commented Dec 11, 2024

Hi team.
When I tried using the tokio-uring TcpStream.read method, I found that polling it never yields ready data.
Here is a code example where I attempt to implement the hyper::rt::Read trait using the read method provided by TcpStream

pub struct HyperStream {
    inner: TcpStream,
}

impl hyper::rt::Read for HyperStream {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        mut buf: ReadBufCursor<'_>,
    ) -> Poll<Result<(), Error>> {
        unsafe {
            println!("call poll read");
            let read_slice = {
                let buffer = buf.as_mut();
                buffer.as_mut_ptr().write_bytes(0, buffer.len());
                std::slice::from_raw_parts_mut(buffer.as_mut_ptr() as *mut u8, buffer.len())
            };

            println!("call self.inner.read before");
            let mut read_future = self.inner.read(read_slice.to_vec());
            println!("leave self.inner.read");
            tokio::pin!(read_future);

            match read_future.poll(cx) {
                Poll::Ready((Ok(bytes_read), _)) => {
                    buf.advance(bytes_read);
                    Poll::Ready(Ok(()))
                }
                Poll::Ready((Err(e), _)) => Poll::Ready(Err(e)),
                Poll::Pending =>
                    {
                        println!("is pending status?");
                        let _ = cx.waker();
                        Poll::Pending
                    },
            }
        }
    }
}

The hyper::rt::Read requires me to return a Poll<Result>. So I can not directly use self.inner.read(read_slice.to_vec()).await
So my idea was to use self.inner.read(read_slice.to_vec()) to get a future, and then manually use poll to get the result.
However, I found that in the example below, using read_future.poll can never yields a result.

Can someone point out where I went wrong? I checked the tcp_eco example and found that directly using TcpStream.read().await can get a result. I don't understand what the difference is here.🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant