(port: u16)
| 18 | } |
| 19 | |
| 20 | pub async fn start(port: u16) -> Result<mpsc::Sender<()>> { |
| 21 | // Create a channel for stop signal |
| 22 | let (stop_tx, mut stop_rx) = mpsc::channel(1); |
| 23 | |
| 24 | let tcp_addr = format!("localhost:{}", port); |
| 25 | |
| 26 | // Spawn TCP ponger in a non-blocking task |
| 27 | tokio::spawn(async move { |
| 28 | debug!("Starting TCP ponger on {}", tcp_addr); |
| 29 | match TcpListener::bind(&tcp_addr).await { |
| 30 | Ok(acceptor) => { |
| 31 | tokio::spawn(async move { |
| 32 | loop { |
| 33 | tokio::select! { |
| 34 | _ = stop_rx.recv() => { |
| 35 | debug!("TCP ponger received stop signal"); |
| 36 | break; |
| 37 | } |
| 38 | accept_result = acceptor.accept() => { |
| 39 | match accept_result { |
| 40 | Ok((client, addr)) => { |
| 41 | debug!("TCP client connected from {}", addr); |
| 42 | tokio::spawn(pong_tcp(client)); |
| 43 | } |
| 44 | Err(e) => { |
| 45 | error!("Failed to accept TCP connection: {}", e); |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | }); |
| 53 | } |
| 54 | Err(e) => { |
| 55 | error!("Failed to bind TCP ponger to {}: {}", tcp_addr, e); |
| 56 | } |
| 57 | } |
| 58 | }); |
| 59 | |
| 60 | Ok(stop_tx) |
| 61 | } |
| 62 | |
| 63 | pub async fn publish(command_tx: mpsc::Sender<Message>) -> Result<()> { |
| 64 | let port = find_free_tcp_port() |
no outgoing calls
no test coverage detected