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
}
}