use core::net::{Ipv4Addr, SocketAddr};
use core::pin::Pin;
use core::task::{Context, Poll};
use std::future::Future;
use futures_util::stream::{Stream, StreamExt, TryStreamExt};
use crate::BufDnsStreamHandle;
use crate::error::NetError;
use crate::multicast::mdns_stream::{MDNS_IPV4, MDNS_IPV6};
use crate::multicast::{MdnsQueryType, MdnsStream};
use crate::proto::op::SerialMessage;
use crate::runtime::TokioTime;
use crate::xfer::DnsClientStream;
#[must_use = "futures do nothing unless polled"]
pub struct MdnsClientStream {
mdns_stream: MdnsStream,
}
impl MdnsClientStream {
pub fn new_ipv4(
mdns_query_type: MdnsQueryType,
packet_ttl: Option<u32>,
ipv4_if: Option<Ipv4Addr>,
) -> (
impl Future<Output = Result<Self, NetError>>,
BufDnsStreamHandle,
) {
Self::new(*MDNS_IPV4, mdns_query_type, packet_ttl, ipv4_if, None)
}
pub fn new_ipv6(
mdns_query_type: MdnsQueryType,
packet_ttl: Option<u32>,
ipv6_if: Option<u32>,
) -> (
impl Future<Output = Result<Self, NetError>>,
BufDnsStreamHandle,
) {
Self::new(*MDNS_IPV6, mdns_query_type, packet_ttl, None, ipv6_if)
}
#[allow(clippy::new_ret_no_self)]
pub fn new(
mdns_addr: SocketAddr,
mdns_query_type: MdnsQueryType,
packet_ttl: Option<u32>,
ipv4_if: Option<Ipv4Addr>,
ipv6_if: Option<u32>,
) -> (
impl Future<Output = Result<Self, NetError>>,
BufDnsStreamHandle,
) {
let (stream_future, sender) =
MdnsStream::new(mdns_addr, mdns_query_type, packet_ttl, ipv4_if, ipv6_if);
(
async {
Ok(Self {
mdns_stream: stream_future.await?,
})
},
sender,
)
}
}
impl DnsClientStream for MdnsClientStream {
type Time = TokioTime;
fn name_server_addr(&self) -> SocketAddr {
self.mdns_stream.multicast_addr()
}
}
impl Stream for MdnsClientStream {
type Item = Result<SerialMessage, NetError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mdns_stream = &mut self.as_mut().mdns_stream;
mdns_stream.map_err(NetError::from).poll_next_unpin(cx)
}
}