lib.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package fclashgobridge
  2. /*
  3. #include "stdint.h"
  4. #include "stdlib.h"
  5. #include "dart_api_dl/dart_api_dl.h"
  6. #include "dart_api_dl/dart_api_dl.c"
  7. #include "dart_api_dl/dart_native_api.h"
  8. // Go does not allow calling C function pointers directly.
  9. // we mock a function to call Dart_PostCObject_DL
  10. bool GoDart_PostCObject(Dart_Port_DL port, Dart_CObject* obj) {
  11. return Dart_PostCObject_DL(port, obj);
  12. }
  13. */
  14. import "C"
  15. import (
  16. "fmt"
  17. "unsafe"
  18. )
  19. func InitDartApi(api unsafe.Pointer) {
  20. if C.Dart_InitializeApiDL(api) != 0 {
  21. panic("failed to create fclash dart bridge")
  22. } else {
  23. fmt.Println("Dart Api DL is initialized")
  24. }
  25. }
  26. func SendToPort(port int64, msg string) {
  27. var obj C.Dart_CObject
  28. obj._type = C.Dart_CObject_kString
  29. msg_obj := C.CString(msg) // go string -> char*s
  30. // union type, we do a force convertion
  31. ptr := unsafe.Pointer(&obj.value[0])
  32. *(**C.char)(ptr) = msg_obj
  33. defer C.free(unsafe.Pointer(msg_obj))
  34. ret := C.GoDart_PostCObject(C.Dart_Port_DL(port), &obj)
  35. if !ret {
  36. fmt.Println("ERROR: post to port ", port, " failed", msg)
  37. }
  38. }