sdk.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "C"
  4. "github.com/astaxie/beego/logs"
  5. "github.com/cnlh/nps/client"
  6. "github.com/cnlh/nps/lib/common"
  7. "github.com/cnlh/nps/lib/version"
  8. "time"
  9. )
  10. var status int
  11. var closeBefore int
  12. var cl *client.TRPClient
  13. //export StartClientByVerifyKey
  14. func StartClientByVerifyKey(serverAddr, verifyKey, connType, proxyUrl *C.char) int {
  15. if cl != nil {
  16. closeBefore = 1
  17. cl.Close()
  18. }
  19. cl = client.NewRPClient(C.GoString(serverAddr), C.GoString(verifyKey), C.GoString(connType), C.GoString(proxyUrl), nil)
  20. closeBefore = 0
  21. go func() {
  22. for {
  23. status = 1
  24. cl.Start()
  25. status = 0
  26. if closeBefore == 1 {
  27. return
  28. }
  29. time.Sleep(time.Second * 5)
  30. }
  31. }()
  32. return 1
  33. }
  34. //export GetClientStatus
  35. func GetClientStatus() int {
  36. return status
  37. }
  38. //export CloseClient
  39. func CloseClient() {
  40. closeBefore = 1
  41. cl.Close()
  42. }
  43. //export Version
  44. func Version() *C.char {
  45. return C.CString(version.VERSION)
  46. }
  47. func Logs() *C.char {
  48. return C.CString(common.GetLogMsg())
  49. }
  50. func main() {
  51. // Need a main function to make CGO compile package as C shared library
  52. logs.SetLogger("store")
  53. }