防止被多次初始化
(cherry picked from commit 77f1a8b30d2c30c03f0ec45fc32fe8eef9d2b4c7)
This commit is contained in:
parent
4250f61345
commit
fbdf4a2337
@ -99,6 +99,9 @@ class KRSingBoxImp {
|
|||||||
/// Stream 订阅管理器
|
/// Stream 订阅管理器
|
||||||
final List<StreamSubscription<dynamic>> _kr_subscriptions = [];
|
final List<StreamSubscription<dynamic>> _kr_subscriptions = [];
|
||||||
|
|
||||||
|
/// 初始化标志,防止重复初始化
|
||||||
|
bool _kr_isInitialized = false;
|
||||||
|
|
||||||
/// 当前混合代理端口是否就绪
|
/// 当前混合代理端口是否就绪
|
||||||
bool get kr_isProxyReady => kr_status.value is SingboxStarted;
|
bool get kr_isProxyReady => kr_status.value is SingboxStarted;
|
||||||
|
|
||||||
@ -131,6 +134,12 @@ class KRSingBoxImp {
|
|||||||
|
|
||||||
/// 初始化
|
/// 初始化
|
||||||
Future<void> init() async {
|
Future<void> init() async {
|
||||||
|
// 防止重复初始化
|
||||||
|
if (_kr_isInitialized) {
|
||||||
|
KRLogUtil.kr_i('SingBox 已经初始化,跳过重复初始化', tag: 'SingBox');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (_initialized) {
|
if (_initialized) {
|
||||||
KRLogUtil.kr_i('SingBox 已经初始化,跳过重复初始化');
|
KRLogUtil.kr_i('SingBox 已经初始化,跳过重复初始化');
|
||||||
@ -249,9 +258,12 @@ class KRSingBoxImp {
|
|||||||
});
|
});
|
||||||
|
|
||||||
KRLogUtil.kr_i('SingBox 初始化完成');
|
KRLogUtil.kr_i('SingBox 初始化完成');
|
||||||
|
_kr_isInitialized = true;
|
||||||
} catch (e, stackTrace) {
|
} catch (e, stackTrace) {
|
||||||
KRLogUtil.kr_e('SingBox 初始化失败: $e');
|
KRLogUtil.kr_e('SingBox 初始化失败: $e');
|
||||||
KRLogUtil.kr_e('错误堆栈: $stackTrace');
|
KRLogUtil.kr_e('错误堆栈: $stackTrace');
|
||||||
|
// 如果初始化失败,允许下次重试
|
||||||
|
_kr_isInitialized = false;
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,6 +31,9 @@ class FFISingboxService with InfraLogger implements SingboxService {
|
|||||||
late final ReceivePort _statusReceiver;
|
late final ReceivePort _statusReceiver;
|
||||||
Stream<SingboxStats>? _serviceStatsStream;
|
Stream<SingboxStats>? _serviceStatsStream;
|
||||||
Stream<List<SingboxOutboundGroup>>? _outboundsStream;
|
Stream<List<SingboxOutboundGroup>>? _outboundsStream;
|
||||||
|
|
||||||
|
/// 初始化标志,防止重复初始化
|
||||||
|
bool _isInitialized = false;
|
||||||
|
|
||||||
static SingboxNativeLibrary _gen() {
|
static SingboxNativeLibrary _gen() {
|
||||||
String fullPath = "";
|
String fullPath = "";
|
||||||
@ -52,6 +55,12 @@ class FFISingboxService with InfraLogger implements SingboxService {
|
|||||||
@override
|
@override
|
||||||
|
|
||||||
Future<void> init() async {
|
Future<void> init() async {
|
||||||
|
// 防止重复初始化
|
||||||
|
if (_isInitialized) {
|
||||||
|
loggy.debug("already initialized, skipping");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
loggy.debug("initializing");
|
loggy.debug("initializing");
|
||||||
_statusReceiver = ReceivePort('service status receiver');
|
_statusReceiver = ReceivePort('service status receiver');
|
||||||
final source = _statusReceiver
|
final source = _statusReceiver
|
||||||
@ -62,6 +71,8 @@ class FFISingboxService with InfraLogger implements SingboxService {
|
|||||||
source,
|
source,
|
||||||
const SingboxStopped(),
|
const SingboxStopped(),
|
||||||
).autoConnect();
|
).autoConnect();
|
||||||
|
|
||||||
|
_isInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user