style(phantun) remove unnecessary tokio::select call

This commit is contained in:
Datong Sun 2022-04-15 07:58:16 -07:00
parent 2f4eaafccd
commit 74183071f1
2 changed files with 108 additions and 106 deletions

View File

@ -4,6 +4,7 @@ use fake_tcp::{Socket, Stack};
use log::{debug, error, info}; use log::{debug, error, info};
use phantun::utils::new_udp_reuseport; use phantun::utils::new_udp_reuseport;
use std::collections::HashMap; use std::collections::HashMap;
use std::io;
use std::net::{Ipv4Addr, SocketAddr}; use std::net::{Ipv4Addr, SocketAddr};
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::{Notify, RwLock}; use tokio::sync::{Notify, RwLock};
@ -14,7 +15,7 @@ use tokio_util::sync::CancellationToken;
use phantun::UDP_TTL; use phantun::UDP_TTL;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() -> io::Result<()> {
pretty_env_logger::init(); pretty_env_logger::init();
let matches = Command::new("Phantun Client") let matches = Command::new("Phantun Client")
@ -122,8 +123,7 @@ async fn main() {
let mut buf_r = [0u8; MAX_PACKET_LEN]; let mut buf_r = [0u8; MAX_PACKET_LEN];
loop { loop {
tokio::select! { let (size, addr) = udp_sock.recv_from(&mut buf_r).await?;
Ok((size, addr)) = udp_sock.recv_from(&mut buf_r) => {
// seen UDP packet to listening socket, this means: // seen UDP packet to listening socket, this means:
// 1. It is a new UDP connection, or // 1. It is a new UDP connection, or
// 2. It is some extra packets not filtered by more specific // 2. It is some extra packets not filtered by more specific
@ -147,7 +147,11 @@ async fn main() {
continue; continue;
} }
assert!(connections.write().await.insert(addr, sock.clone()).is_none()); assert!(connections
.write()
.await
.insert(addr, sock.clone())
.is_none());
debug!("inserted fake TCP socket into connection table"); debug!("inserted fake TCP socket into connection table");
// spawn "fastpath" UDP socket and task, this will offload main task // spawn "fastpath" UDP socket and task, this will offload main task
@ -231,10 +235,8 @@ async fn main() {
} }
} }
}); });
},
}
} }
}); });
tokio::join!(main_loop).0.unwrap(); tokio::join!(main_loop).0.unwrap()
} }

View File

@ -3,6 +3,7 @@ use fake_tcp::packet::MAX_PACKET_LEN;
use fake_tcp::Stack; use fake_tcp::Stack;
use log::{debug, error, info}; use log::{debug, error, info};
use phantun::utils::new_udp_reuseport; use phantun::utils::new_udp_reuseport;
use std::io;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::sync::Arc; use std::sync::Arc;
use tokio::net::UdpSocket; use tokio::net::UdpSocket;
@ -14,7 +15,7 @@ use tokio_util::sync::CancellationToken;
use phantun::UDP_TTL; use phantun::UDP_TTL;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() -> io::Result<()> {
pretty_env_logger::init(); pretty_env_logger::init();
let matches = Command::new("Phantun Server") let matches = Command::new("Phantun Server")
@ -128,9 +129,8 @@ async fn main() {
} else { } else {
"[::]:0" "[::]:0"
}) })
.await .await?;
.unwrap(); let local_addr = udp_sock.local_addr()?;
let local_addr = udp_sock.local_addr().unwrap();
drop(udp_sock); drop(udp_sock);
for i in 0..num_cpus { for i in 0..num_cpus {
@ -199,5 +199,5 @@ async fn main() {
} }
}); });
tokio::join!(main_loop).0.unwrap(); tokio::join!(main_loop).0.unwrap()
} }