refactor should_exit

This commit is contained in:
open-trade 2022-02-07 18:39:49 +08:00
parent 809c220eaf
commit 6c05f72d90
4 changed files with 16 additions and 19 deletions

View File

@ -565,9 +565,6 @@ impl Config {
pub fn set_option(k: String, v: String) {
let mut config = CONFIG2.write().unwrap();
if k == "custom-rendezvous-server" {
config.rendezvous_server = "".to_owned();
}
let v2 = if v.is_empty() { None } else { Some(&v) };
if v2 != config.options.get(&k) {
if v2.is_none() {

View File

@ -1,3 +1,4 @@
use crate::rendezvous_mediator::RendezvousMediator;
use hbb_common::{
allow_err, bail, bytes,
bytes_codec::BytesCodec,
@ -213,7 +214,7 @@ async fn handle(data: Data, stream: &mut Connection) {
Config::set_socks(Some(data));
}
crate::common::test_nat_type();
crate::rendezvous_mediator::RendezvousMediator::restart();
RendezvousMediator::restart();
log::info!("socks updated");
}
},
@ -255,7 +256,14 @@ async fn handle(data: Data, stream: &mut Connection) {
allow_err!(stream.send(&Data::Options(Some(v))).await);
}
Some(value) => {
let v0 = Config::get_option("stop-service");
let v1 = Config::get_rendezvous_servers();
Config::set_options(value);
if v0 != Config::get_option("stop-service")
|| v1 != Config::get_rendezvous_servers()
{
RendezvousMediator::restart();
}
}
},
Data::NatType(_) => {

View File

@ -37,7 +37,6 @@ pub struct RendezvousMediator {
addr: TargetAddr<'static>,
host: String,
host_prefix: String,
rendezvous_servers: Vec<String>,
last_id_pk_registry: String,
}
@ -77,7 +76,7 @@ impl RendezvousMediator {
let server = server.clone();
let servers = servers.clone();
futs.push(tokio::spawn(async move {
allow_err!(Self::start(server, host, servers).await);
allow_err!(Self::start(server, host).await);
// SHOULD_EXIT here is to ensure once one exits, the others also exit.
SHOULD_EXIT.store(true, Ordering::SeqCst);
}));
@ -88,11 +87,7 @@ impl RendezvousMediator {
}
}
pub async fn start(
server: ServerPtr,
host: String,
rendezvous_servers: Vec<String>,
) -> ResultType<()> {
pub async fn start(server: ServerPtr, host: String) -> ResultType<()> {
log::info!("start rendezvous mediator of {}", host);
let host_prefix: String = host
.split(".")
@ -109,7 +104,6 @@ impl RendezvousMediator {
addr: Config::get_any_listen_addr().into_target_addr()?,
host: host.clone(),
host_prefix,
rendezvous_servers,
last_id_pk_registry: "".to_owned(),
};
@ -207,8 +201,12 @@ impl RendezvousMediator {
});
}
Some(rendezvous_message::Union::configure_update(cu)) => {
let v0 = Config::get_rendezvous_servers();
Config::set_option("rendezvous-servers".to_owned(), cu.rendezvous_servers.join(","));
Config::set_serial(cu.serial);
if v0 != Config::get_rendezvous_servers() {
Self::restart();
}
}
_ => {}
}
@ -223,12 +221,6 @@ impl RendezvousMediator {
}
},
_ = timer.tick() => {
if Config::get_rendezvous_servers() != rz.rendezvous_servers {
break;
}
if !Config::get_option("stop-service").is_empty() {
break;
}
if SHOULD_EXIT.load(Ordering::SeqCst) {
break;
}

View File

@ -151,8 +151,8 @@ class MyIdMenu: Reactor.Component {
<li #enable-tunnel><span>{svg_checkmark}</span>{translate('Enable TCP Tunneling')}</li>
<AudioInputs />
<div .separator />
<li #whitelist title={translate('whitelist_tip')}>{translate('IP Whitelisting')}</li>
<li #custom-server>{translate('ID/Relay Server')}</li>
<li #whitelist title={translate('whitelist_tip')}>{translate('IP Whitelisting')}</li>
<li #socks5-server>{translate('Socks5 Proxy')}</li>
<div .separator />
<li #stop-service class={service_stopped ? "line-through" : "selected"}><span>{svg_checkmark}</span>{translate("Enable Service")}</li>