2024-12-03 21:47:12 +08:00
|
|
|
//go:build cgo
|
|
|
|
|
|
2024-11-09 20:17:57 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
*/
|
|
|
|
|
import "C"
|
|
|
|
|
import (
|
|
|
|
|
bridge "core/dart-bridge"
|
2025-01-13 19:08:17 +08:00
|
|
|
"encoding/json"
|
2024-11-09 20:17:57 +08:00
|
|
|
"unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-13 19:08:17 +08:00
|
|
|
var messagePort int64 = -1
|
|
|
|
|
|
2024-12-03 21:47:12 +08:00
|
|
|
//export initNativeApiBridge
|
|
|
|
|
func initNativeApiBridge(api unsafe.Pointer) {
|
|
|
|
|
bridge.InitDartApi(api)
|
|
|
|
|
}
|
2024-11-09 20:17:57 +08:00
|
|
|
|
2025-01-13 19:08:17 +08:00
|
|
|
//export attachMessagePort
|
|
|
|
|
func attachMessagePort(mPort C.longlong) {
|
|
|
|
|
messagePort = int64(mPort)
|
2024-11-09 20:17:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//export getTraffic
|
2025-01-13 19:08:17 +08:00
|
|
|
func getTraffic() *C.char {
|
|
|
|
|
return C.CString(handleGetTraffic())
|
2024-11-09 20:17:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//export getTotalTraffic
|
2025-01-13 19:08:17 +08:00
|
|
|
func getTotalTraffic() *C.char {
|
|
|
|
|
return C.CString(handleGetTotalTraffic())
|
2024-11-09 20:17:57 +08:00
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:08:17 +08:00
|
|
|
//export freeCString
|
|
|
|
|
func freeCString(s *C.char) {
|
|
|
|
|
C.free(unsafe.Pointer(s))
|
2024-11-09 20:17:57 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-02 02:24:12 +08:00
|
|
|
func (result ActionResult) send() {
|
|
|
|
|
data, err := result.Json()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
bridge.SendToPort(result.Port, string(data))
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:08:17 +08:00
|
|
|
//export invokeAction
|
|
|
|
|
func invokeAction(paramsChar *C.char, port C.longlong) {
|
|
|
|
|
params := C.GoString(paramsChar)
|
2024-12-09 01:40:39 +08:00
|
|
|
i := int64(port)
|
2025-01-13 19:08:17 +08:00
|
|
|
var action = &Action{}
|
|
|
|
|
err := json.Unmarshal([]byte(params), action)
|
|
|
|
|
if err != nil {
|
|
|
|
|
bridge.SendToPort(i, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-05-02 02:24:12 +08:00
|
|
|
result := ActionResult{
|
|
|
|
|
Id: action.Id,
|
|
|
|
|
Method: action.Method,
|
|
|
|
|
Port: i,
|
|
|
|
|
}
|
|
|
|
|
go handleAction(action, result)
|
2024-12-09 01:40:39 +08:00
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:08:17 +08:00
|
|
|
func sendMessage(message Message) {
|
|
|
|
|
if messagePort == -1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-05-02 02:24:12 +08:00
|
|
|
result := ActionResult{
|
|
|
|
|
Method: messageMethod,
|
|
|
|
|
Port: messagePort,
|
|
|
|
|
Data: message,
|
|
|
|
|
}
|
|
|
|
|
result.send()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//export getConfig
|
|
|
|
|
func getConfig(s *C.char) *C.char {
|
|
|
|
|
path := C.GoString(s)
|
|
|
|
|
config, err := handleGetConfig(path)
|
2025-01-13 19:08:17 +08:00
|
|
|
if err != nil {
|
2025-05-02 02:24:12 +08:00
|
|
|
return C.CString("")
|
2025-01-13 19:08:17 +08:00
|
|
|
}
|
2025-05-02 02:24:12 +08:00
|
|
|
marshal, err := json.Marshal(config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return C.CString("")
|
|
|
|
|
}
|
|
|
|
|
return C.CString(string(marshal))
|
2024-11-09 20:17:57 +08:00
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:08:17 +08:00
|
|
|
//export startListener
|
|
|
|
|
func startListener() {
|
|
|
|
|
handleStartListener()
|
2024-11-09 20:17:57 +08:00
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:08:17 +08:00
|
|
|
//export stopListener
|
|
|
|
|
func stopListener() {
|
|
|
|
|
handleStopListener()
|
2024-11-09 20:17:57 +08:00
|
|
|
}
|