pong_rs/task/tcp/
tcp_executor.rs1use crate::executor::Executor;
2use crate::ping_error::PingError;
3use crate::task::tcp::tcp_ping::TcpPing;
4use async_trait::async_trait;
5use log::trace;
6use std::net::SocketAddr;
7use std::time::Duration;
8use wheel_rs::dns_utils::parse_host_port;
9
10#[derive(Clone)]
11pub struct TcpExecutor {
12 ip_addr: String,
13 port: u16,
14 tcp_ping: TcpPing,
15 timeout: Duration,
16}
17
18impl TcpExecutor {
19 pub fn new(host_port: String, timeout: Duration) -> Self {
24 let (ip_addr, port) = parse_host_port(&host_port).unwrap();
26 let socket_addr = SocketAddr::new(ip_addr, port);
28
29 let tcp_ping = TcpPing::new(socket_addr);
30
31 Self {
32 ip_addr: ip_addr.to_string(),
33 port,
34 tcp_ping,
35 timeout,
36 }
37 }
38}
39
40#[async_trait]
41impl Executor for TcpExecutor {
42 fn get_name(&self) -> String {
43 String::from("TCP")
44 }
45
46 async fn exec(&self) -> Result<(), PingError> {
47 trace!(
48 "开始执行 TCP 任务: ping {}",
49 format!("{}:{}", self.ip_addr, self.port)
50 );
51 self.tcp_ping.ping(self.timeout)
52 }
53}