Implementing poll_next for TCP poll_accept and Stream poll_read

I am trying to create one function called "receive" which is returning a stream.

In that function, I am trying to do 2 things :

  • Pool for any new incoming TCP connection.
  • If we have a new TCP connection then I need to read/poll all the data that exist in steam.

based on the below code I am not able to achieve that, it's only accepting the first connection then it's not serving any new connection .. so what is the issue?

impl Stream for Server {
    type Item = Vec<u8>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {

        if let Poll::Ready(val) = Pin::new(&self.tcp).poll_accept(cx) {
            match val {
                Ok((mut tcp_stream, address)) => {
                    let mut buf = [0; 10];
                    let mut buf = ReadBuf::new(&mut buf);

                    if let Poll::Ready(_data) = Pin::new(&mut tcp_stream).poll_read(cx, &mut buf) {
                        return Poll::Ready(Some(Vec::from(buf)));
                    }

                }
                Err(err) => {
                    eprintln!("Error checking for new requests:{:?}", err);
                }
            }
        }
        Poll::Pending
    }
}

I'm pretty sure I've seen this question twice. Did you post it twice? Do you still need help?

This is was the first question .... when nobody answer the question I created a new one.

I will delete this one to avoid duplication ... I also can please check my last comment on : Cannot move out of dereference of `Pin<&mut Server>` - #8 by RustCaptin

Ah, ok. I wasn't sure which one was first.

Can you delete this post ... I am not able to do it :frowning: