Skip to content

Commit

Permalink
忽略绑定失败的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
vnt-dev committed Aug 7, 2024
1 parent 803af55 commit 7e0de2c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions vnt-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn main0(config: Config, _show_cmd: bool) {
let vnt_util = match Vnt::new(config, callback::VntHandler {}) {
Ok(vnt) => vnt,
Err(e) => {
log::error!("vnt create error {:?}", e);
println!("error: {:?}", e);
std::process::exit(1);
}
Expand Down
8 changes: 6 additions & 2 deletions vnt/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,13 @@ pub(crate) fn init_context(
let socket = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None)?;
(socket, address)
};
socket.set_reuse_address(true)?;
socket
.set_reuse_address(true)
.context("set_reuse_address")?;
#[cfg(unix)]
socket.set_reuse_port(true)?;
if let Err(e) = socket.set_reuse_port(true) {
log::warn!("set_reuse_port {:?}", e)
}
if let Err(e) = socket.bind(&address.into()) {
if ports[0] == 0 {
//端口可能冲突,则使用任意端口
Expand Down
4 changes: 3 additions & 1 deletion vnt/src/channel/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ pub fn create_tcp0(
if bind_port != 0 {
socket.set_reuse_address(true)?;
#[cfg(unix)]
socket.set_reuse_port(true)?;
if let Err(e) = socket.set_reuse_port(true) {
log::warn!("set_reuse_port {:?}", e)
}
if v4 {
let addr: SocketAddr = format!("0.0.0.0:{}", bind_port).parse().unwrap();
socket.bind(&addr.into())?;
Expand Down
18 changes: 13 additions & 5 deletions vnt/src/channel/tcp_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tokio::sync::mpsc::{channel, Receiver};
use crate::channel::context::ChannelContext;
use crate::channel::handler::RecvChannelHandler;
use crate::channel::sender::PacketSender;
use crate::channel::socket::create_tcp0;
use crate::channel::{ConnectProtocol, RouteKey, BUFFER_SIZE, TCP_MAX_PACKET_SIZE};
use crate::util::StopManager;

Expand Down Expand Up @@ -99,11 +100,18 @@ async fn connect_tcp0<H>(
where
H: RecvChannelHandler,
{
let mut stream = tokio::time::timeout(
Duration::from_secs(3),
crate::channel::socket::connect_tcp(addr, bind_port, context.default_interface()),
)
.await??;
let socket = if bind_port != 0 {
match create_tcp0(addr.is_ipv4(), bind_port, context.default_interface()) {
Ok(socket) => socket,
Err(e) => {
log::warn!("{:?}", e);
create_tcp0(addr.is_ipv4(), 0, context.default_interface())?
}
}
} else {
create_tcp0(addr.is_ipv4(), 0, context.default_interface())?
};
let mut stream = tokio::time::timeout(Duration::from_secs(3), socket.connect(addr)).await??;
tcp_write(&mut stream, &data).await?;

tcp_stream_handle(stream, addr, recv_handler, context).await;
Expand Down

0 comments on commit 7e0de2c

Please sign in to comment.