mirror of
https://ghfast.top/https://github.com/StarCitizenToolBox/app.git
synced 2025-06-28 09:04:45 +08:00
子进程切换到 rust win32job 实现
This commit is contained in:
@ -1,4 +1,3 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
@ -11,8 +10,11 @@ import 'package:starcitizen_doctor/common/conf/app_conf.dart';
|
||||
import 'package:starcitizen_doctor/common/conf/binary_conf.dart';
|
||||
import 'package:starcitizen_doctor/common/helper/system_helper.dart';
|
||||
|
||||
import 'package:starcitizen_doctor/common/rust/api/process_api.dart'
|
||||
as rs_process;
|
||||
|
||||
class Aria2cManager {
|
||||
static int? _daemonPID;
|
||||
static bool _isDaemonRunning = false;
|
||||
|
||||
static final String _aria2cDir =
|
||||
"${AppConf.applicationSupportDir}\\modules\\aria2c";
|
||||
@ -24,7 +26,7 @@ class Aria2cManager {
|
||||
throw "not connect!";
|
||||
}
|
||||
|
||||
static bool get isAvailable => _daemonPID != null && _aria2c != null;
|
||||
static bool get isAvailable => _isDaemonRunning && _aria2c != null;
|
||||
|
||||
static Future checkLazyLoad() async {
|
||||
try {
|
||||
@ -40,7 +42,7 @@ class Aria2cManager {
|
||||
}
|
||||
|
||||
static Future launchDaemon() async {
|
||||
if (_daemonPID != null) return;
|
||||
if (_isDaemonRunning) return;
|
||||
await BinaryModuleConf.extractModule(["aria2c"]);
|
||||
|
||||
/// skip for debug hot reload
|
||||
@ -63,9 +65,10 @@ class Aria2cManager {
|
||||
final trackerList = await Api.getTorrentTrackerList();
|
||||
dPrint("trackerList === $trackerList");
|
||||
dPrint("Aria2cManager .----- aria2c start $port------");
|
||||
final p = await Process.start(
|
||||
exePath,
|
||||
[
|
||||
|
||||
final stream = rs_process.startProcess(
|
||||
executable: exePath,
|
||||
arguments: [
|
||||
"-V",
|
||||
"-c",
|
||||
"-x 10",
|
||||
@ -82,36 +85,24 @@ class Aria2cManager {
|
||||
"--seed-time=0",
|
||||
],
|
||||
workingDirectory: _aria2cDir);
|
||||
p.stdout.transform(utf8.decoder).listen((event) async {
|
||||
if (event.trim().isEmpty) return;
|
||||
dPrint("[aria2c]: ${event.trim()}");
|
||||
if (event.contains("IPv4 RPC: listening on TCP port")) {
|
||||
_daemonPID = p.pid;
|
||||
_aria2c = Aria2c("ws://127.0.0.1:$port/jsonrpc", "websocket", pwd);
|
||||
_aria2c!.getVersion().then((value) {
|
||||
dPrint("Aria2cManager.connected! version == ${value.version}");
|
||||
});
|
||||
final box = await Hive.openBox("app_conf");
|
||||
_aria2c!.changeGlobalOption(Aria2Option()
|
||||
..maxOverallUploadLimit =
|
||||
textToByte(box.get("downloader_up_limit", defaultValue: "0"))
|
||||
..maxOverallDownloadLimit =
|
||||
textToByte(box.get("downloader_down_limit", defaultValue: "0"))
|
||||
..btTracker = trackerList);
|
||||
|
||||
stream.listen((event) {
|
||||
dPrint("Aria2cManager.rs_process event === $event");
|
||||
if (event.startsWith("output:")) {
|
||||
if (event.contains("IPv4 RPC: listening on TCP port")) {
|
||||
_onLaunch(port, pwd, trackerList);
|
||||
}
|
||||
} else if (event.startsWith("error:")) {
|
||||
_isDaemonRunning = false;
|
||||
_aria2c = null;
|
||||
} else if (event.startsWith("exit:")) {
|
||||
_isDaemonRunning = false;
|
||||
_aria2c = null;
|
||||
}
|
||||
}, onDone: () {
|
||||
dPrint("[aria2c] onDone: ");
|
||||
_daemonPID = null;
|
||||
}, onError: (e) {
|
||||
dPrint("[aria2c] stdout ERROR: $e");
|
||||
_daemonPID = null;
|
||||
});
|
||||
p.pid;
|
||||
p.stderr.transform(utf8.decoder).listen((event) {
|
||||
dPrint("[aria2c] stderr ERROR : $event");
|
||||
});
|
||||
|
||||
while (true) {
|
||||
if (_daemonPID != null) return;
|
||||
if (_isDaemonRunning) return;
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
}
|
||||
@ -150,4 +141,20 @@ class Aria2cManager {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static Future<void> _onLaunch(
|
||||
int port, String pwd, String trackerList) async {
|
||||
_isDaemonRunning = true;
|
||||
_aria2c = Aria2c("ws://127.0.0.1:$port/jsonrpc", "websocket", pwd);
|
||||
_aria2c!.getVersion().then((value) {
|
||||
dPrint("Aria2cManager.connected! version == ${value.version}");
|
||||
});
|
||||
final box = await Hive.openBox("app_conf");
|
||||
_aria2c!.changeGlobalOption(Aria2Option()
|
||||
..maxOverallUploadLimit =
|
||||
textToByte(box.get("downloader_up_limit", defaultValue: "0"))
|
||||
..maxOverallDownloadLimit =
|
||||
textToByte(box.get("downloader_down_limit", defaultValue: "0"))
|
||||
..btTracker = trackerList);
|
||||
}
|
||||
}
|
||||
|
18
lib/common/rust/api/process_api.dart
Normal file
18
lib/common/rust/api/process_api.dart
Normal file
@ -0,0 +1,18 @@
|
||||
// This file is automatically generated, so please do not edit it.
|
||||
// Generated by `flutter_rust_bridge`@ 2.0.0-dev.24.
|
||||
|
||||
// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import
|
||||
|
||||
import '../frb_generated.dart';
|
||||
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
|
||||
|
||||
Stream<String> startProcess(
|
||||
{required String executable,
|
||||
required List<String> arguments,
|
||||
required String workingDirectory,
|
||||
dynamic hint}) =>
|
||||
RustLib.instance.api.startProcess(
|
||||
executable: executable,
|
||||
arguments: arguments,
|
||||
workingDirectory: workingDirectory,
|
||||
hint: hint);
|
@ -4,6 +4,7 @@
|
||||
// ignore_for_file: unused_import, unused_element, unnecessary_import, duplicate_ignore, invalid_use_of_internal_member, annotate_overrides, non_constant_identifier_names, curly_braces_in_flow_control_structures, prefer_const_literals_to_create_immutables, unused_field
|
||||
|
||||
import 'api/http_api.dart';
|
||||
import 'api/process_api.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'frb_generated.io.dart' if (dart.library.html) 'frb_generated.web.dart';
|
||||
@ -75,6 +76,12 @@ abstract class RustLibApi extends BaseApi {
|
||||
Future<void> setDefaultHeader(
|
||||
{required Map<String, String> headers, dynamic hint});
|
||||
|
||||
Stream<String> startProcess(
|
||||
{required String executable,
|
||||
required List<String> arguments,
|
||||
required String workingDirectory,
|
||||
dynamic hint});
|
||||
|
||||
RustArcIncrementStrongCountFnType
|
||||
get rust_arc_increment_strong_count_ReqwestVersion;
|
||||
|
||||
@ -177,6 +184,37 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
|
||||
argNames: ["headers"],
|
||||
);
|
||||
|
||||
@override
|
||||
Stream<String> startProcess(
|
||||
{required String executable,
|
||||
required List<String> arguments,
|
||||
required String workingDirectory,
|
||||
dynamic hint}) {
|
||||
return handler.executeStream(StreamTask(
|
||||
callFfi: (port_) {
|
||||
final serializer = SseSerializer(generalizedFrbRustBinding);
|
||||
sse_encode_String(executable, serializer);
|
||||
sse_encode_list_String(arguments, serializer);
|
||||
sse_encode_String(workingDirectory, serializer);
|
||||
pdeCallFfi(generalizedFrbRustBinding, serializer,
|
||||
funcId: 4, port: port_);
|
||||
},
|
||||
codec: SseCodec(
|
||||
decodeSuccessData: sse_decode_String,
|
||||
decodeErrorData: null,
|
||||
),
|
||||
constMeta: kStartProcessConstMeta,
|
||||
argValues: [executable, arguments, workingDirectory],
|
||||
apiImpl: this,
|
||||
hint: hint,
|
||||
));
|
||||
}
|
||||
|
||||
TaskConstMeta get kStartProcessConstMeta => const TaskConstMeta(
|
||||
debugName: "start_process",
|
||||
argNames: ["executable", "arguments", "workingDirectory"],
|
||||
);
|
||||
|
||||
RustArcIncrementStrongCountFnType
|
||||
get rust_arc_increment_strong_count_ReqwestVersion => wire
|
||||
.rust_arc_increment_strong_count_RustOpaque_flutter_rust_bridgefor_generatedrust_asyncRwLockreqwestVersion;
|
||||
|
@ -4,6 +4,7 @@
|
||||
// ignore_for_file: unused_import, unused_element, unnecessary_import, duplicate_ignore, invalid_use_of_internal_member, annotate_overrides, non_constant_identifier_names, curly_braces_in_flow_control_structures, prefer_const_literals_to_create_immutables, unused_field
|
||||
|
||||
import 'api/http_api.dart';
|
||||
import 'api/process_api.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:ffi' as ffi;
|
||||
|
@ -4,6 +4,7 @@
|
||||
// ignore_for_file: unused_import, unused_element, unnecessary_import, duplicate_ignore, invalid_use_of_internal_member, annotate_overrides, non_constant_identifier_names, curly_braces_in_flow_control_structures, prefer_const_literals_to_create_immutables, unused_field
|
||||
|
||||
import 'api/http_api.dart';
|
||||
import 'api/process_api.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'frb_generated.dart';
|
||||
|
Reference in New Issue
Block a user