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. logs.SetLogger("store")
  16. if cl != nil {
  17. closeBefore = 1
  18. cl.Close()
  19. }
  20. cl = client.NewRPClient(C.GoString(serverAddr), C.GoString(verifyKey), C.GoString(connType), C.GoString(proxyUrl), nil)
  21. closeBefore = 0
  22. go func() {
  23. for {
  24. status = 1
  25. cl.Start()
  26. status = 0
  27. if closeBefore == 1 {
  28. return
  29. }
  30. time.Sleep(time.Second * 5)
  31. }
  32. }()
  33. return 1
  34. }
  35. //export GetClientStatus
  36. func GetClientStatus() int {
  37. return status
  38. }
  39. //export CloseClient
  40. func CloseClient() {
  41. closeBefore = 1
  42. cl.Close()
  43. }
  44. //export Version
  45. func Version() *C.char {
  46. return C.CString(version.VERSION)
  47. }
  48. //export Logs
  49. func Logs() *C.char {
  50. return C.CString(common.GetLogMsg())
  51. }
  52. func main() {
  53. // Need a main function to make CGO compile package as C shared library
  54. }