mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-18 14:23:42 +00:00
fix: ffi instance be singleton
This commit is contained in:
1 parent
91dfd78b77
commit
4b4c03fd90
18 files changed
+844
-5336
No files matched your search
+10
-37
@@ -5,51 +5,24 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"github.com/GopeedLab/gopeed/pkg/api"
|
"github.com/GopeedLab/gopeed/pkg/api"
|
||||||
"github.com/GopeedLab/gopeed/pkg/api/model"
|
"github.com/GopeedLab/gopeed/pkg/api/model"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type instanceArray struct {
|
// Global singleton instance
|
||||||
mu sync.RWMutex
|
var instance *api.Instance
|
||||||
array []*api.Instance
|
|
||||||
}
|
|
||||||
|
|
||||||
// append appends an instance to the array.
|
func Init(config *model.StartConfig) (err error) {
|
||||||
// Returns the index of the instance in the array.
|
if instance != nil {
|
||||||
func (ia *instanceArray) append(instance *api.Instance) int {
|
return nil
|
||||||
ia.mu.Lock()
|
|
||||||
defer ia.mu.Unlock()
|
|
||||||
|
|
||||||
ia.array = append(ia.array, instance)
|
|
||||||
return len(ia.array) - 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ia *instanceArray) get(index int) (*api.Instance, bool) {
|
|
||||||
ia.mu.RLock()
|
|
||||||
defer ia.mu.RUnlock()
|
|
||||||
if index < 0 || index >= len(ia.array) {
|
|
||||||
return nil, false
|
|
||||||
}
|
}
|
||||||
return ia.array[index], true
|
|
||||||
}
|
|
||||||
|
|
||||||
var instances = &instanceArray{
|
|
||||||
mu: sync.RWMutex{},
|
|
||||||
array: make([]*api.Instance, 0),
|
|
||||||
}
|
|
||||||
|
|
||||||
func Create(config *model.StartConfig) string {
|
|
||||||
config.ProductionMode = true
|
config.ProductionMode = true
|
||||||
instance, err := api.Create(config)
|
instance, err = api.Create(config)
|
||||||
if err != nil {
|
return
|
||||||
return BuildResult(err)
|
|
||||||
}
|
|
||||||
return BuildResult(instances.append(instance))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Invoke(index int, request *api.Request) string {
|
func Invoke(request *api.Request) string {
|
||||||
instance, ok := instances.get(index)
|
if instance == nil {
|
||||||
if !ok {
|
return BuildResult(errors.New("instance not initialized"))
|
||||||
return BuildResult(errors.New("instance not found"))
|
|
||||||
}
|
}
|
||||||
return jsonEncode(api.Invoke(instance, request))
|
return jsonEncode(api.Invoke(instance, request))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,22 +10,25 @@ import (
|
|||||||
|
|
||||||
func main() {}
|
func main() {}
|
||||||
|
|
||||||
//export Create
|
//export Init
|
||||||
func Create(cfg *C.char) *C.char {
|
func Init(cfg *C.char) *C.char {
|
||||||
var config model.StartConfig
|
var config model.StartConfig
|
||||||
if err := json.Unmarshal([]byte(C.GoString(cfg)), &config); err != nil {
|
if err := json.Unmarshal([]byte(C.GoString(cfg)), &config); err != nil {
|
||||||
return C.CString(bind.BuildResult(err))
|
return C.CString(err.Error())
|
||||||
|
}
|
||||||
|
if err := bind.Init(&config); err != nil {
|
||||||
|
return C.CString(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return C.CString(bind.Create(&config))
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//export Invoke
|
//export Invoke
|
||||||
func Invoke(index int, req *C.char) *C.char {
|
func Invoke(req *C.char) *C.char {
|
||||||
var request api.Request
|
var request api.Request
|
||||||
if err := json.Unmarshal([]byte(C.GoString(req)), &request); err != nil {
|
if err := json.Unmarshal([]byte(C.GoString(req)), &request); err != nil {
|
||||||
return C.CString(bind.BuildResult(err))
|
return C.CString(bind.BuildResult(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return C.CString(bind.Invoke(index, &request))
|
return C.CString(bind.Invoke(&request))
|
||||||
}
|
}
|
||||||
+4
-4
@@ -8,10 +8,10 @@ import (
|
|||||||
"github.com/GopeedLab/gopeed/pkg/api/model"
|
"github.com/GopeedLab/gopeed/pkg/api/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Create(cfg *model.StartConfig) string {
|
func Init(cfg *model.StartConfig) error {
|
||||||
return bind.Create(cfg)
|
return bind.Init(cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Invoke(index int, request *api.Request) string {
|
func Invoke(request *api.Request) string {
|
||||||
return bind.Invoke(index, request)
|
return bind.Invoke(request)
|
||||||
}
|
}
|
||||||
+1
-1
@@ -60,7 +60,7 @@ func Start(cfg *model.StartConfig) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Server start success on http://%s:%d\n", result.Host, result.Port)
|
fmt.Printf("Server start success on http://%s:%d\n", cfg.DownloadConfig.Http.Host, result)
|
||||||
start()
|
start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+33
-32
@@ -17,12 +17,10 @@ import (
|
|||||||
type Instance struct {
|
type Instance struct {
|
||||||
startCfg *model.StartConfig
|
startCfg *model.StartConfig
|
||||||
downloader *download.Downloader
|
downloader *download.Downloader
|
||||||
running bool
|
|
||||||
|
|
||||||
httpLock sync.Mutex
|
httpLock sync.Mutex
|
||||||
srv *http.Server
|
srv *http.Server
|
||||||
listener net.Listener
|
listener net.Listener
|
||||||
runningPort int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Create(startCfg *model.StartConfig) (*Instance, error) {
|
func Create(startCfg *model.StartConfig) (*Instance, error) {
|
||||||
@@ -51,44 +49,45 @@ func Create(startCfg *model.StartConfig) (*Instance, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
i.downloader = downloader
|
i.downloader = downloader
|
||||||
i.running = true
|
|
||||||
i.httpLock = sync.Mutex{}
|
i.httpLock = sync.Mutex{}
|
||||||
|
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Instance) StartHttp() *model.Result[*model.HttpListenResult] {
|
func (i *Instance) StartHttp() *model.Result[any] {
|
||||||
i.httpLock.Lock()
|
i.httpLock.Lock()
|
||||||
defer i.httpLock.Unlock()
|
defer i.httpLock.Unlock()
|
||||||
|
|
||||||
return i.startHttp()
|
return i.startHttp()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Instance) startHttp() *model.Result[*model.HttpListenResult] {
|
func (i *Instance) startHttp() *model.Result[any] {
|
||||||
|
httpCfg := i.getHttpConfig()
|
||||||
|
if httpCfg == nil || !httpCfg.Enable {
|
||||||
|
return model.NewErrorResult[any]("HTTP API server not enabled")
|
||||||
|
}
|
||||||
|
if i.srv != nil {
|
||||||
|
return model.NewErrorResult[any]("HTTP API server already started")
|
||||||
|
}
|
||||||
|
|
||||||
|
port, start, err := ListenHttp(httpCfg, i)
|
||||||
|
if err != nil {
|
||||||
|
return model.NewErrorResult[any](err.Error())
|
||||||
|
}
|
||||||
|
httpCfg.RunningPort = port
|
||||||
|
go start()
|
||||||
|
return model.NewNilResult()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Instance) getHttpConfig() *base.DownloaderHttpConfig {
|
||||||
var httpCfg *base.DownloaderHttpConfig
|
var httpCfg *base.DownloaderHttpConfig
|
||||||
// if startCfg has http config, first use it
|
|
||||||
if i.startCfg.DownloadConfig != nil && i.startCfg.DownloadConfig.Http != nil {
|
if i.startCfg.DownloadConfig != nil && i.startCfg.DownloadConfig.Http != nil {
|
||||||
httpCfg = i.startCfg.DownloadConfig.Http
|
httpCfg = i.startCfg.DownloadConfig.Http
|
||||||
} else {
|
} else {
|
||||||
cfg, err := i.downloader.GetConfig()
|
cfg, _ := i.downloader.GetConfig()
|
||||||
if err != nil {
|
|
||||||
return model.NewErrorResult[*model.HttpListenResult](err.Error())
|
|
||||||
}
|
|
||||||
httpCfg = cfg.Http
|
httpCfg = cfg.Http
|
||||||
}
|
}
|
||||||
|
return httpCfg
|
||||||
if httpCfg == nil || !httpCfg.Enable {
|
|
||||||
return model.NewErrorResult[*model.HttpListenResult]("HTTP API server not enabled")
|
|
||||||
}
|
|
||||||
if i.srv != nil {
|
|
||||||
return model.NewErrorResult[*model.HttpListenResult]("HTTP API server already started")
|
|
||||||
}
|
|
||||||
result, start, err := ListenHttp(httpCfg, i)
|
|
||||||
if err != nil {
|
|
||||||
return model.NewErrorResult[*model.HttpListenResult](err.Error())
|
|
||||||
}
|
|
||||||
go start()
|
|
||||||
return model.NewOkResult[*model.HttpListenResult](result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Instance) StopHttp() *model.Result[any] {
|
func (i *Instance) StopHttp() *model.Result[any] {
|
||||||
@@ -105,12 +104,15 @@ func (i *Instance) stopHttp() *model.Result[any] {
|
|||||||
}
|
}
|
||||||
i.srv = nil
|
i.srv = nil
|
||||||
i.listener = nil
|
i.listener = nil
|
||||||
i.runningPort = 0
|
}
|
||||||
|
httpCfg := i.getHttpConfig()
|
||||||
|
if httpCfg != nil {
|
||||||
|
httpCfg.RunningPort = 0
|
||||||
}
|
}
|
||||||
return model.NewNilResult()
|
return model.NewNilResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Instance) RestartHttp() *model.Result[*model.HttpListenResult] {
|
func (i *Instance) RestartHttp() *model.Result[any] {
|
||||||
i.httpLock.Lock()
|
i.httpLock.Lock()
|
||||||
defer i.httpLock.Unlock()
|
defer i.httpLock.Unlock()
|
||||||
|
|
||||||
@@ -337,7 +339,6 @@ func (i *Instance) Close() *model.Result[any] {
|
|||||||
i.downloader.Logger.Warn().Err(err).Msg("close downloader failed")
|
i.downloader.Logger.Warn().Err(err).Msg("close downloader failed")
|
||||||
}
|
}
|
||||||
i.downloader = nil
|
i.downloader = nil
|
||||||
i.running = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return model.NewNilResult()
|
return model.NewNilResult()
|
||||||
@@ -349,7 +350,7 @@ type Request struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Invoke support dynamic call method
|
// Invoke support dynamic call method
|
||||||
func Invoke(server *Instance, request *Request) (ret any) {
|
func Invoke(instance *Instance, request *Request) (ret any) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
ret = model.NewErrorResult[any](fmt.Sprintf("%v", err))
|
ret = model.NewErrorResult[any](fmt.Sprintf("%v", err))
|
||||||
@@ -357,7 +358,7 @@ func Invoke(server *Instance, request *Request) (ret any) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
method, args := request.Method, request.Params
|
method, args := request.Method, request.Params
|
||||||
dsType := reflect.ValueOf(server)
|
dsType := reflect.ValueOf(instance)
|
||||||
fn := dsType.MethodByName(method)
|
fn := dsType.MethodByName(method)
|
||||||
numIn := fn.Type().NumIn()
|
numIn := fn.Type().NumIn()
|
||||||
in := make([]reflect.Value, numIn)
|
in := make([]reflect.Value, numIn)
|
||||||
@@ -378,5 +379,5 @@ func Invoke(server *Instance, request *Request) (ret any) {
|
|||||||
in[i] = param
|
in[i] = param
|
||||||
}
|
}
|
||||||
retVals := fn.Call(in)
|
retVals := fn.Call(in)
|
||||||
return retVals[0]
|
return retVals[0].Interface()
|
||||||
}
|
}
|
||||||
+24
-648
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
+7
-10
@@ -22,7 +22,7 @@ import (
|
|||||||
|
|
||||||
const instanceKey string = "apiInstance"
|
const instanceKey string = "apiInstance"
|
||||||
|
|
||||||
func ListenHttp(httpCfg *base.DownloaderHttpConfig, ai *Instance) (*model.HttpListenResult, func(), error) {
|
func ListenHttp(httpCfg *base.DownloaderHttpConfig, ai *Instance) (int, func(), error) {
|
||||||
var r = mux.NewRouter()
|
var r = mux.NewRouter()
|
||||||
r.Methods(http.MethodGet).Path("/api/v1/info").HandlerFunc(Info)
|
r.Methods(http.MethodGet).Path("/api/v1/info").HandlerFunc(Info)
|
||||||
r.Methods(http.MethodPost).Path("/api/v1/resolve").HandlerFunc(Resolve)
|
r.Methods(http.MethodPost).Path("/api/v1/resolve").HandlerFunc(Resolve)
|
||||||
@@ -106,19 +106,16 @@ func ListenHttp(httpCfg *base.DownloaderHttpConfig, ai *Instance) (*model.HttpLi
|
|||||||
|
|
||||||
listener, err := net.Listen("tcp", net.JoinHostPort(httpCfg.Host, fmt.Sprintf("%d", httpCfg.Port)))
|
listener, err := net.Listen("tcp", net.JoinHostPort(httpCfg.Host, fmt.Sprintf("%d", httpCfg.Port)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
ai.srv = srv
|
ai.srv = srv
|
||||||
ai.listener = listener
|
ai.listener = listener
|
||||||
|
|
||||||
return &model.HttpListenResult{
|
return listener.Addr().(*net.TCPAddr).Port, func() {
|
||||||
Host: httpCfg.Host,
|
if err := srv.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
Port: listener.Addr().(*net.TCPAddr).Port,
|
ai.downloader.Logger.Error().Err(err).Msg("http server listen error")
|
||||||
}, func() {
|
}
|
||||||
if err := srv.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
}, nil
|
||||||
ai.downloader.Logger.Error().Err(err).Msg("http server listen error")
|
|
||||||
}
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getInstance(r *http.Request) *Instance {
|
func getInstance(r *http.Request) *Instance {
|
||||||
|
|||||||
@@ -228,6 +228,8 @@ type DownloaderHttpConfig struct {
|
|||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
// ApiToken is the auth token for http API
|
// ApiToken is the auth token for http API
|
||||||
ApiToken string `json:"apiToken"`
|
ApiToken string `json:"apiToken"`
|
||||||
|
// RunningPort is the http server real listen port.
|
||||||
|
RunningPort int `json:"runningPort"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DownloaderProxyConfig struct {
|
type DownloaderProxyConfig struct {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/* package github.com/GopeedLab/gopeed/bind/desktop */
|
/* package github.com/GopeedLab/gopeed/bind/desktop */
|
||||||
|
|
||||||
|
|
||||||
#line 1 "cgo-builtin-export-prolog"
|
#line 1 "cgo-builtin-export-prolog"
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@@ -10,24 +11,19 @@
|
|||||||
#define GO_CGO_EXPORT_PROLOGUE_H
|
#define GO_CGO_EXPORT_PROLOGUE_H
|
||||||
|
|
||||||
#ifndef GO_CGO_GOSTRING_TYPEDEF
|
#ifndef GO_CGO_GOSTRING_TYPEDEF
|
||||||
typedef struct {
|
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
|
||||||
const char *p;
|
|
||||||
ptrdiff_t n;
|
|
||||||
} _GoString_;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Start of preamble from import "C" comments. */
|
/* Start of preamble from import "C" comments. */
|
||||||
|
|
||||||
#line 3 "main.go"
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#line 1 "cgo-generated-wrapper"
|
|
||||||
|
|
||||||
/* End of preamble from import "C" comments. */
|
/* End of preamble from import "C" comments. */
|
||||||
|
|
||||||
|
|
||||||
/* Start of boilerplate cgo prologue. */
|
/* Start of boilerplate cgo prologue. */
|
||||||
#line 1 "cgo-gcc-export-header-prolog"
|
#line 1 "cgo-gcc-export-header-prolog"
|
||||||
|
|
||||||
@@ -60,22 +56,15 @@ typedef double _Complex GoComplex128;
|
|||||||
static assertion to make sure the file is being used on architecture
|
static assertion to make sure the file is being used on architecture
|
||||||
at least with matching size of GoInt.
|
at least with matching size of GoInt.
|
||||||
*/
|
*/
|
||||||
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void *) == 64 / 8 ? 1 : -1];
|
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
|
||||||
|
|
||||||
#ifndef GO_CGO_GOSTRING_TYPEDEF
|
#ifndef GO_CGO_GOSTRING_TYPEDEF
|
||||||
typedef _GoString_ GoString;
|
typedef _GoString_ GoString;
|
||||||
#endif
|
#endif
|
||||||
typedef void *GoMap;
|
typedef void *GoMap;
|
||||||
typedef void *GoChan;
|
typedef void *GoChan;
|
||||||
typedef struct {
|
typedef struct { void *t; void *v; } GoInterface;
|
||||||
void *t;
|
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
||||||
void *v;
|
|
||||||
} GoInterface;
|
|
||||||
typedef struct {
|
|
||||||
void *data;
|
|
||||||
GoInt len;
|
|
||||||
GoInt cap;
|
|
||||||
} GoSlice;
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -85,8 +74,8 @@ typedef struct {
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern __declspec(dllexport) char *Create(char *cfg);
|
extern __declspec(dllexport) char* Init(char* cfg);
|
||||||
extern __declspec(dllexport) char *Invoke(GoInt index, char *req);
|
extern __declspec(dllexport) char* Invoke(char* req);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,9 +33,10 @@ import 'libgopeed_boot_base.dart';
|
|||||||
|
|
||||||
LibgopeedBoot create() => LibgopeedBootNative();
|
LibgopeedBoot create() => LibgopeedBootNative();
|
||||||
|
|
||||||
class LibgopeedBootNative with LibgopeedBootBase implements LibgopeedBoot {
|
class LibgopeedBootNative
|
||||||
|
with LibgopeedBootBase
|
||||||
|
implements LibgopeedBoot, LibgopeedApi {
|
||||||
late LibgopeedAbi _libgopeedAbi;
|
late LibgopeedAbi _libgopeedAbi;
|
||||||
late int _apiInstance;
|
|
||||||
late Dio _dio;
|
late Dio _dio;
|
||||||
|
|
||||||
LibgopeedBootNative() {
|
LibgopeedBootNative() {
|
||||||
@@ -69,8 +70,7 @@ class LibgopeedBootNative with LibgopeedBootBase implements LibgopeedBoot {
|
|||||||
}
|
}
|
||||||
return jsonEncode(e);
|
return jsonEncode(e);
|
||||||
}).toList());
|
}).toList());
|
||||||
final resp =
|
final resp = await _libgopeedAbi.invoke(jsonEncode(invokeRequest));
|
||||||
await _libgopeedAbi.invoke(_apiInstance, jsonEncode(invokeRequest));
|
|
||||||
final result =
|
final result =
|
||||||
Result<T>.fromJson(jsonDecode(resp), fromJsonT ??= (json) => null as T);
|
Result<T>.fromJson(jsonDecode(resp), fromJsonT ??= (json) => null as T);
|
||||||
return handleResult(result);
|
return handleResult(result);
|
||||||
@@ -78,87 +78,86 @@ class LibgopeedBootNative with LibgopeedBootBase implements LibgopeedBoot {
|
|||||||
|
|
||||||
List<dynamic> _parseTaskFilter(TaskFilter? filter) {
|
List<dynamic> _parseTaskFilter(TaskFilter? filter) {
|
||||||
if (filter == null) {
|
if (filter == null) {
|
||||||
return [];
|
return [null];
|
||||||
}
|
}
|
||||||
return [filter];
|
return [filter];
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> init(StartConfig cfg) async {
|
Future<LibgopeedApi> init(StartConfig cfg) async {
|
||||||
final result = await _libgopeedAbi.create(jsonEncode(cfg.toJson()));
|
await _libgopeedAbi.init(jsonEncode(cfg.toJson()));
|
||||||
_apiInstance =
|
|
||||||
Result.fromJson(jsonDecode(result), (data) => data as int).data!;
|
|
||||||
_dio = createDio();
|
_dio = createDio();
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<HttpListenResult> startHttp() async {
|
Future<HttpListenResult> startHttp() async {
|
||||||
return _invoke<HttpListenResult>(
|
return _invoke<HttpListenResult>(
|
||||||
"startHttp", [], (json) => HttpListenResult.fromJson(json));
|
"StartHttp", [], (json) => HttpListenResult.fromJson(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> stopHttp() async {
|
Future<void> stopHttp() async {
|
||||||
await _invoke<void>("stopHttp", [], null);
|
await _invoke<void>("StopHttp", [], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> restartHttp() async {
|
Future<void> restartHttp() async {
|
||||||
await _invoke<void>("restartHttp", [], null);
|
await _invoke<void>("RestartHttp", [], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Info> info() async {
|
Future<Info> info() async {
|
||||||
return _invoke<Info>("info", [], (json) => Info.fromJson(json));
|
return _invoke<Info>("Info", [], (json) => Info.fromJson(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<ResolveResult> resolve(Request request) async {
|
Future<ResolveResult> resolve(Request request) async {
|
||||||
return _invoke<ResolveResult>(
|
return _invoke<ResolveResult>(
|
||||||
"resolve", [request], (json) => ResolveResult.fromJson(json));
|
"Resolve", [request], (json) => ResolveResult.fromJson(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> createTask(CreateTask createTask) async {
|
Future<String> createTask(CreateTask createTask) async {
|
||||||
return _invoke<String>(
|
return _invoke<String>(
|
||||||
"createTask", [createTask], (json) => json as String);
|
"CreateTask", [createTask], (json) => json as String);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> createTaskBatch(CreateTaskBatch createTask) async {
|
Future<String> createTaskBatch(CreateTaskBatch createTask) async {
|
||||||
return _invoke<String>(
|
return _invoke<String>(
|
||||||
"createTaskBatch", [createTask], (json) => json as String);
|
"CreateTaskBatch", [createTask], (json) => json as String);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> pauseTask(String id) async {
|
Future<void> pauseTask(String id) async {
|
||||||
await _invoke<void>("pauseTask", [id], null);
|
await _invoke<void>("PauseTask", [id], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> pauseTasks(TaskFilter? filter) async {
|
Future<void> pauseTasks(TaskFilter? filter) async {
|
||||||
await _invoke<void>("pauseTasks", _parseTaskFilter(filter), null);
|
await _invoke<void>("PauseTasks", _parseTaskFilter(filter), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> continueTask(String id) async {
|
Future<void> continueTask(String id) async {
|
||||||
await _invoke<void>("continueTask", [id], null);
|
await _invoke<void>("ContinueTask", [id], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> continueTasks(TaskFilter? filter) async {
|
Future<void> continueTasks(TaskFilter? filter) async {
|
||||||
await _invoke<void>("continueTasks", _parseTaskFilter(filter), null);
|
await _invoke<void>("ContinueTasks", _parseTaskFilter(filter), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> deleteTask(String id, bool force) async {
|
Future<void> deleteTask(String id, bool force) async {
|
||||||
await _invoke<void>("deleteTask", [id, force], null);
|
await _invoke<void>("DeleteTask", [id, force], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> deleteTasks(TaskFilter? filter, bool force) async {
|
Future<void> deleteTasks(TaskFilter? filter, bool force) async {
|
||||||
await _invoke<void>(
|
await _invoke<void>(
|
||||||
"deleteTasks",
|
"DeleteTasks",
|
||||||
_parseTaskFilter(filter).apply((it) {
|
_parseTaskFilter(filter).apply((it) {
|
||||||
it.add(force);
|
it.add(force);
|
||||||
}),
|
}),
|
||||||
@@ -167,40 +166,40 @@ class LibgopeedBootNative with LibgopeedBootBase implements LibgopeedBoot {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Task> getTask(String id) async {
|
Future<Task> getTask(String id) async {
|
||||||
return _invoke<Task>("getTask", [id], (json) => Task.fromJson(json));
|
return _invoke<Task>("GetTask", [id], (json) => Task.fromJson(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<Task>> getTasks(TaskFilter? filter) async {
|
Future<List<Task>> getTasks(TaskFilter? filter) async {
|
||||||
return _invoke<List<Task>>("getTasks", _parseTaskFilter(filter),
|
return _invoke<List<Task>>("GetTasks", _parseTaskFilter(filter),
|
||||||
(json) => json.map((e) => Task.fromJson(e)).toList());
|
(json) => json.map((e) => Task.fromJson(e)).toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Map<String, dynamic>> getTaskStats(String id) async {
|
Future<Map<String, dynamic>> getTaskStats(String id) async {
|
||||||
return _invoke<Map<String, dynamic>>(
|
return _invoke<Map<String, dynamic>>(
|
||||||
"getTaskStats", [id], (json) => json as Map<String, dynamic>);
|
"GetTaskStats", [id], (json) => json as Map<String, dynamic>);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<DownloaderConfig> getConfig() async {
|
Future<DownloaderConfig> getConfig() async {
|
||||||
return _invoke<DownloaderConfig>(
|
return _invoke<DownloaderConfig>(
|
||||||
"getConfig", [], (json) => DownloaderConfig.fromJson(json));
|
"GetConfig", [], (json) => DownloaderConfig.fromJson(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> putConfig(DownloaderConfig config) async {
|
Future<void> putConfig(DownloaderConfig config) async {
|
||||||
await _invoke<void>("putConfig", [config], null);
|
await _invoke<void>("PutConfig", [config], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> installExtension(InstallExtension installExtension) async {
|
Future<void> installExtension(InstallExtension installExtension) async {
|
||||||
await _invoke<void>("installExtension", [installExtension], null);
|
await _invoke<void>("InstallExtension", [installExtension], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<Extension>> getExtensions() async {
|
Future<List<Extension>> getExtensions() async {
|
||||||
return _invoke<List<Extension>>("getExtensions", [],
|
return _invoke<List<Extension>>("GetExtensions", [],
|
||||||
(json) => json.map((e) => Extension.fromJson(e)).toList());
|
(json) => json.map((e) => Extension.fromJson(e)).toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,33 +207,35 @@ class LibgopeedBootNative with LibgopeedBootBase implements LibgopeedBoot {
|
|||||||
Future<void> updateExtensionSettings(
|
Future<void> updateExtensionSettings(
|
||||||
String identity, UpdateExtensionSettings updateExtensionSettings) async {
|
String identity, UpdateExtensionSettings updateExtensionSettings) async {
|
||||||
await _invoke<void>(
|
await _invoke<void>(
|
||||||
"updateExtensionSettings", [identity, updateExtensionSettings], null);
|
"UpdateExtensionSettings", [identity, updateExtensionSettings], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> switchExtension(
|
Future<void> switchExtension(
|
||||||
String identity, SwitchExtension switchExtension) async {}
|
String identity, SwitchExtension switchExtension) async {
|
||||||
|
await _invoke<void>("SwitchExtension", [identity, switchExtension], null);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> deleteExtension(String identity) async {
|
Future<void> deleteExtension(String identity) async {
|
||||||
await _invoke<void>("deleteExtension", [identity], null);
|
await _invoke<void>("DeleteExtension", [identity], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<UpdateCheckExtensionResp> upgradeCheckExtension(
|
Future<UpdateCheckExtensionResp> upgradeCheckExtension(
|
||||||
String identity) async {
|
String identity) async {
|
||||||
return _invoke<UpdateCheckExtensionResp>("upgradeCheckExtension",
|
return _invoke<UpdateCheckExtensionResp>("UpgradeCheckExtension",
|
||||||
[identity], (json) => UpdateCheckExtensionResp.fromJson(json));
|
[identity], (json) => UpdateCheckExtensionResp.fromJson(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> upgradeExtension(String identity) async {
|
Future<void> upgradeExtension(String identity) async {
|
||||||
await _invoke<void>("upgradeExtension", [identity], null);
|
await _invoke<void>("UpgradeExtension", [identity], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> close() async {
|
Future<void> close() async {
|
||||||
await _invoke<void>("close", [], null);
|
await _invoke<void>("Close", [], null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -20,12 +20,15 @@ import '../../util/extensions.dart';
|
|||||||
import '../../util/util.dart';
|
import '../../util/util.dart';
|
||||||
import '../api_exception.dart';
|
import '../api_exception.dart';
|
||||||
import '../libgopeed_boot.dart';
|
import '../libgopeed_boot.dart';
|
||||||
|
import '../native/libgopeed_interface.dart';
|
||||||
import '../native/model/start_config.dart';
|
import '../native/model/start_config.dart';
|
||||||
import 'libgopeed_boot_base.dart';
|
import 'libgopeed_boot_base.dart';
|
||||||
|
|
||||||
LibgopeedBoot create() => LibgopeedBootWeb();
|
LibgopeedBoot create() => LibgopeedBootWeb();
|
||||||
|
|
||||||
class LibgopeedBootWeb with LibgopeedBootBase implements LibgopeedBoot {
|
class LibgopeedBootWeb
|
||||||
|
with LibgopeedBootBase
|
||||||
|
implements LibgopeedBoot, LibgopeedApi {
|
||||||
late Dio _dio;
|
late Dio _dio;
|
||||||
static const String _apiPrefix = '/api/v1';
|
static const String _apiPrefix = '/api/v1';
|
||||||
|
|
||||||
@@ -72,9 +75,10 @@ class LibgopeedBootWeb with LibgopeedBootBase implements LibgopeedBoot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> init(StartConfig cfg) async {
|
Future<LibgopeedApi> init(StartConfig cfg) async {
|
||||||
_dio = createDio();
|
_dio = createDio();
|
||||||
_dio.options.baseUrl = kDebugMode ? 'http://127.0.0.1:9999' : '';
|
_dio.options.baseUrl = kDebugMode ? 'http://127.0.0.1:9999' : '';
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -3,11 +3,14 @@ import "libgopeed_boot_stub.dart"
|
|||||||
if (dart.library.io) 'entry/libgopeed_boot_native.dart';
|
if (dart.library.io) 'entry/libgopeed_boot_native.dart';
|
||||||
import 'native/libgopeed_interface.dart';
|
import 'native/libgopeed_interface.dart';
|
||||||
|
|
||||||
abstract class LibgopeedBoot implements LibgopeedApi {
|
abstract class LibgopeedBoot implements LibgopeedApiSingleton {
|
||||||
static LibgopeedBoot? _instance;
|
static LibgopeedApi? _instance;
|
||||||
|
|
||||||
static LibgopeedBoot get instance {
|
static void singleton(LibgopeedApi instance) {
|
||||||
_instance ??= LibgopeedBoot();
|
_instance ??= instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
static LibgopeedApi get instance {
|
||||||
return _instance!;
|
return _instance!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,17 +6,15 @@ class LibgopeedChannel implements LibgopeedAbi {
|
|||||||
static const _channel = MethodChannel('gopeed.com/libgopeed');
|
static const _channel = MethodChannel('gopeed.com/libgopeed');
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> create(String cfg) async {
|
Future<void> init(String cfg) async {
|
||||||
final result = await _channel.invokeMethod<String>('create', {
|
await _channel.invokeMethod<String>('init', {
|
||||||
'cfg': cfg,
|
'cfg': cfg,
|
||||||
});
|
});
|
||||||
return result!;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> invoke(int instance, String params) async {
|
Future<String> invoke(String params) async {
|
||||||
final result = await _channel.invokeMethod<String>('invoke', {
|
final result = await _channel.invokeMethod<String>('invoke', {
|
||||||
'instance': instance,
|
|
||||||
'params': params,
|
'params': params,
|
||||||
});
|
});
|
||||||
return result!;
|
return result!;
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -1,4 +1,5 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:ffi';
|
||||||
|
|
||||||
import 'package:ffi/ffi.dart';
|
import 'package:ffi/ffi.dart';
|
||||||
|
|
||||||
@@ -13,21 +14,25 @@ class LibgopeedFFi implements LibgopeedAbi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> create(String cfg) {
|
Future<void> init(String cfg) {
|
||||||
final cfgPtr = cfg.toNativeUtf8();
|
final cfgPtr = cfg.toNativeUtf8();
|
||||||
try {
|
try {
|
||||||
final result = _libgopeed.Create(cfgPtr.cast());
|
final result = _libgopeed.Init(cfgPtr.cast());
|
||||||
return Future.value(result.cast<Utf8>().toDartString());
|
if (result != nullptr) {
|
||||||
|
throw Exception(
|
||||||
|
'Libgopeed init failed: ${result.cast<Utf8>().toDartString()}');
|
||||||
|
}
|
||||||
|
return Future.value();
|
||||||
} finally {
|
} finally {
|
||||||
calloc.free(cfgPtr);
|
calloc.free(cfgPtr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> invoke(int instance, String params) {
|
Future<String> invoke(String params) {
|
||||||
final paramsPtr = params.toNativeUtf8();
|
final paramsPtr = params.toNativeUtf8();
|
||||||
try {
|
try {
|
||||||
final result = _libgopeed.Invoke(instance, paramsPtr.cast());
|
final result = _libgopeed.Invoke(paramsPtr.cast());
|
||||||
return Future.value(result.cast<Utf8>().toDartString());
|
return Future.value(result.cast<Utf8>().toDartString());
|
||||||
} finally {
|
} finally {
|
||||||
calloc.free(paramsPtr);
|
calloc.free(paramsPtr);
|
||||||
|
|||||||
@@ -17,13 +17,15 @@ import '../model/update_extension_settings.dart';
|
|||||||
import 'model/start_config.dart';
|
import 'model/start_config.dart';
|
||||||
|
|
||||||
abstract class LibgopeedAbi {
|
abstract class LibgopeedAbi {
|
||||||
Future<String> create(String cfg);
|
Future<void> init(String cfg);
|
||||||
Future<String> invoke(int instance, String params);
|
Future<String> invoke(String params);
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class LibgopeedApiSingleton {
|
||||||
|
Future<LibgopeedApi> init(StartConfig config);
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class LibgopeedApi {
|
abstract class LibgopeedApi {
|
||||||
Future<void> init(StartConfig cfg);
|
|
||||||
|
|
||||||
Future<HttpListenResult> startHttp();
|
Future<HttpListenResult> startHttp();
|
||||||
|
|
||||||
Future<void> stopHttp();
|
Future<void> stopHttp();
|
||||||
|
|||||||
@@ -73,10 +73,11 @@ Future<void> init(Args args) async {
|
|||||||
|
|
||||||
final controller = Get.put(AppController());
|
final controller = Get.put(AppController());
|
||||||
try {
|
try {
|
||||||
await LibgopeedBoot.instance.init(StartConfig(
|
final instance = await LibgopeedBoot().init(StartConfig(
|
||||||
storage: "bolt",
|
storage: "bolt",
|
||||||
storageDir: Util.getStorageDir(),
|
storageDir: Util.getStorageDir(),
|
||||||
));
|
));
|
||||||
|
LibgopeedBoot.singleton(instance);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.e("libgopeed init fail", e);
|
logger.e("libgopeed init fail", e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user