use std::{fmt::Display, net::SocketAddr, time::Duration}; use async_stream::try_stream; use futures::Stream; use serde::Deserialize; use tokio::{io::Interest, net::TcpSocket}; use super::IntoService; pub(crate) type Error = std::io::Error; #[derive(Debug, Clone, Deserialize)] pub struct Tcp { pub address: SocketAddr, #[serde(default = "super::default_interval")] pub interval: Duration, } impl Display for Tcp { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "tcp://{}", self.address) } } impl IntoService for Tcp { type Error = Error; fn into_service(self) -> impl Stream> { let mut interval = tokio::time::interval(self.interval); try_stream! { loop { interval.tick().await; let sock = TcpSocket::new_v4()?; sock.set_keepalive(true)?; let conn = sock.connect(self.address).await?; // TODO: figure out how to wait for connection to close conn.ready(Interest::READABLE).await?; yield (); } } } }