//! tests/tcp_server.rs use ugly_smart_lib::{net_pub::spawn_and_log_error, smart_pub::StdArc, INetCallBack, INetwork}; use crate::helper::init_log; #[tokio::test] async fn udp_server_test() { struct MyServer { sender: ugly_smart_lib::smart_pub::ArcMutex)>>>, } init_log(); impl INetCallBack<(String, Vec)> for MyServer { fn data_callback( self: ugly_smart_lib::smart_pub::StdArc, data: Vec, handle: String, _serv_type: ugly_smart_lib::net_pub::ServiceType, ) { tracing::info!("data call back"); let sender_arc = self.sender.clone(); let sender_mutex = sender_arc.lock().unwrap(); match &*sender_mutex { Some(sender) => { // tracing::info!("will send {:?}", data); sender.send((handle, data)).unwrap(); } None => {} } } fn state_callback( self: ugly_smart_lib::smart_pub::StdArc, // &mut self, _handle: String, state: ugly_smart_lib::net_pub::EConnectionState<(String, Vec)>, _serv_type: ugly_smart_lib::net_pub::ServiceType, ) { match state { ugly_smart_lib::net_pub::EConnectionState::Running(sender) => { let sender_arc = self.sender.clone(); let mut sender_mutex = sender_arc.lock().unwrap(); *sender_mutex = Some(sender); } _ => {} } } } let my_server = StdArc::new(MyServer { sender: ugly_smart_lib::smart_pub::StdArc::new(ugly_smart_lib::smart_pub::StdMutex::new(None)), }); let params = ugly_smart_lib::SUdpServerParams { // connection_addr: "127.0.0.1:8868".into(), call_back: Box::new(my_server), extra_params: "127.0.0.1:8868".into(), local_addr: "127.0.0.1:28686".into(), }; let server = ugly_smart_lib::smart_pub::StdArc::new(ugly_smart_lib::udp_server::CUdpServer::new(params)); spawn_and_log_error(server.clone().start()); // tokio::time::sleep(std::time::Duration::from_secs(10)).await; let _ = tokio::signal::ctrl_c().await; spawn_and_log_error(server.stop()); tokio::time::sleep(std::time::Duration::from_secs(1)).await; }