1pub mod stream;
4
5mod buf;
6
7use std::{
8 io,
9 net::SocketAddr,
10 pin::Pin,
11 task::{Context, Poll},
12};
13
14use futures_core::ready;
15use tokio::io::{AsyncRead, ReadBuf};
16
17use crate::net::buf::OwnedReadBuf;
18
19pub mod constants {
21 pub const MAXIMUM_PAYLOAD_SIZE: usize = 0x3FFF;
23 }
26
27pub async fn lookup_host(host: &str) -> io::Result<SocketAddr> {
31 tokio::net::lookup_host(host)
32 .await?
33 .find(|x| x.is_ipv4())
34 .ok_or(io::Error::from(io::ErrorKind::NotFound))
35}
36
37pub(crate) fn poll_read_exact<R>(
38 reader: &mut R,
39 owned_read_buf: &mut OwnedReadBuf,
40 cx: &mut Context<'_>,
41 buf: &mut [u8],
42) -> Poll<io::Result<()>>
43where
44 R: AsyncRead + Unpin + ?Sized,
45{
46 if owned_read_buf.is_full() {
47 owned_read_buf.require(buf.len());
50 }
51
52 debug_assert_eq!(owned_read_buf.capacity(), buf.len());
53
54 while !owned_read_buf.is_full() {
55 let mut read_buf = ReadBuf::new(owned_read_buf.uninitialized_mut());
56 let remaining = read_buf.remaining();
57
58 ready!(Pin::new(&mut *reader).poll_read(cx, &mut read_buf))?;
59
60 let nread = remaining - read_buf.remaining();
61 if nread == 0 {
62 return Err(io::ErrorKind::UnexpectedEof.into()).into();
63 }
64
65 owned_read_buf.add_filled(nread);
66 }
67
68 buf.copy_from_slice(owned_read_buf.filled());
69
70 Ok(()).into()
71}