Skip to content

Commit 81d7ab7

Browse files
update
1 parent f716e6e commit 81d7ab7

File tree

3 files changed

+74
-62
lines changed

3 files changed

+74
-62
lines changed

tun_test10/src/.main.rs.swp

0 Bytes
Binary file not shown.

tun_test10/src/.server.rs.swp

0 Bytes
Binary file not shown.

tun_test10/src/server.rs

+74-62
Original file line numberDiff line numberDiff line change
@@ -66,57 +66,61 @@ pub async fn start(arc_map: Arc<std::sync::Mutex<HashMap<IpAddr,super::Target>>>
6666
let peer = stream.peer_addr().expect("connected streams should have a peer address");
6767
//tokio::spawn(accept_connection(arc_map.clone(), stream));
6868
//tokio::spawn(accept_connection(arc_map.clone(), peer, stream));
69-
let handle = tokio::spawn(handle_connection(arc_map.clone(), peer, stream));
70-
//let _ = handle.await.unwrap();
69+
70+
//tokio::spawn(handle_connection(arc_map.clone(), peer, stream));
71+
tokio::spawn(handle_connection(arc_map.clone(), peer, stream));
72+
73+
//let handle = tokio::spawn(handle_connection(arc_map, peer, stream));
7174

7275
}
7376

7477
Ok(())
7578
}
7679

77-
async fn accept_connection(arc_map: Arc<std::sync::Mutex<HashMap<IpAddr,super::Target>>>,peer: SocketAddr,stream: TcpStream)
78-
-> Result<()> {
79-
/*
80-
let addr = stream.peer_addr().expect("connected streams should have a peer address");
81-
//info!("Peer address: {}", addr);
82-
83-
let ws_stream = tokio_tungstenite::accept_async(stream)
84-
.await
85-
.expect("Error during the websocket handshake occurred");
86-
87-
//info!("New WebSocket connection: {}", addr);
88-
89-
let (write, read) = ws_stream.split();
90-
read.forward(write).await.expect("Failed to forward message");
91-
*/
9280

93-
94-
if let Err(e) = handle_connection(arc_map, peer, stream).await {
95-
match e {
96-
Error::ConnectionClosed | Error::Protocol(_) | Error::Utf8 => (),
97-
err => error!("Error processing connection: {}", err),
98-
}
99-
}
100-
Ok(())
81+
//async fn accept_connection(arc_map: Arc<std::sync::Mutex<HashMap<IpAddr,super::Target>>>,peer: SocketAddr,stream: TcpStream)
82+
// -> Result<()> {
83+
// /*
84+
// let addr = stream.peer_addr().expect("connected streams should have a peer address");
85+
// //info!("Peer address: {}", addr);
86+
//
87+
// let ws_stream = tokio_tungstenite::accept_async(stream)
88+
// .await
89+
// .expect("Error during the websocket handshake occurred");
90+
//
91+
// //info!("New WebSocket connection: {}", addr);
92+
//
93+
// let (write, read) = ws_stream.split();
94+
// read.forward(write).await.expect("Failed to forward message");
95+
//*/
96+
//
97+
//
98+
//// if let Err(e) = handle_connection(arc_map, peer, stream).await {
99+
//// match e {
100+
//// Error::ConnectionClosed | Error::Protocol(_) | Error::Utf8 => (),
101+
//// err => error!("Error processing connection: {}", err),
102+
//// }
103+
//// }
104+
//// Ok(())
105+
//
106+
///*
107+
// let ipaddr = IpAddr::V4(Ipv4Addr::new(10, 1, 0, 2));
108+
// let mut map = arc_map.lock().unwrap();
109+
// if map.contains_key(&ipaddr) {
110+
// match map.get(&ipaddr) {
111+
// Some(target) => { //&target.tx,
112+
// println!(" is match.");
113+
// },
114+
// None => {
115+
// println!(" is unreviewed.");
116+
// return;
117+
// }
118+
// }
119+
// }
120+
//*/
121+
//}
101122

102123
/*
103-
let ipaddr = IpAddr::V4(Ipv4Addr::new(10, 1, 0, 2));
104-
let mut map = arc_map.lock().unwrap();
105-
if map.contains_key(&ipaddr) {
106-
match map.get(&ipaddr) {
107-
Some(target) => { //&target.tx,
108-
println!(" is match.");
109-
},
110-
None => {
111-
println!(" is unreviewed.");
112-
return;
113-
}
114-
}
115-
}
116-
*/
117-
}
118-
119-
120124
//async fn parser(arc_map: &Arc<std::sync::Mutex<HashMap<IpAddr,super::Target>>>, msg :&Message) -> Result<()> {
121125
async fn parser(arc_map: &Arc<std::sync::Mutex<HashMap<IpAddr,super::Target>>>, msg :&Message) {
122126
@@ -153,6 +157,7 @@ async fn parser(arc_map: &Arc<std::sync::Mutex<HashMap<IpAddr,super::Target>>>,
153157
//Ok(())
154158
155159
}
160+
*/
156161
/*
157162
async fn parser(arc_map: &Arc<std::sync::Mutex<HashMap<IpAddr,super::Target>>>, msg :&Message) -> Result<()> {
158163
@@ -205,34 +210,41 @@ async fn handle_connection(arc_map: Arc<std::sync::Mutex<HashMap<IpAddr,super::T
205210
if msg.is_text() ||msg.is_binary() {
206211

207212
// parser(&arc_map, &msg).await;
213+
208214
ws_sender.send(msg).await?;
209215

210-
let ipaddr = IpAddr::V4(Ipv4Addr::new(10, 1, 0, 2));
211-
//let map = arc_map.lock().unwrap();
212-
let map_lock = Arc::clone(&arc_map);
213-
let map = map_lock.lock().unwrap();
214-
let rtx_ = if map.contains_key(&ipaddr) {
215-
match map.get(&ipaddr) {
216-
Some(target) => {
217-
println!("map ok ");
218-
Some(target.tx.clone())
219-
},
220-
None => {
221-
println!("map ng ");
222-
None
223-
}
224-
}
225-
} else {
226-
None
227-
};
216+
let rtx_ = {
217+
let ipaddr = IpAddr::V4(Ipv4Addr::new(10, 1, 0, 2));
218+
219+
let map = arc_map.lock().unwrap();
220+
//let map_lock = Arc::clone(&arc_map);
221+
//let map = map_lock.lock().unwrap();
222+
let tmp_tx = if map.contains_key(&ipaddr) {
223+
match map.get(&ipaddr) {
224+
Some(target) => {
225+
println!("map ok ");
226+
//Some(target.tx.clone())
227+
Some(target.tx.clone())
228+
},
229+
None => {
230+
println!("map ng ");
231+
None
232+
}
233+
}
234+
} else {
235+
None
236+
};
237+
tmp_tx
238+
}; // arc_map.locked drop ....
228239

229240
match rtx_ {
230241

231242
Some(tx) => {
232243
let cmd = super::Command::Cmd {
233-
key: "*command".to_string(),
244+
key: "*command*".to_string(),
234245
};
235-
//tx.send(cmd).await.unwrap();
246+
tx.send(cmd).await.unwrap();
247+
println!("tx send ok ");
236248

237249
},
238250
None => {

0 commit comments

Comments
 (0)