sdk.go 988 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import "C"
  3. import (
  4. "github.com/astaxie/beego/logs"
  5. "github.com/cnlh/nps/client"
  6. "time"
  7. )
  8. func init() {
  9. logs.SetLogger(logs.AdapterFile, `{"filename":"npc.log","daily":false,"maxlines":100000,"color":true}`)
  10. }
  11. var status int
  12. var closeBefore int
  13. var cl *client.TRPClient
  14. //export StartClientByVerifyKey
  15. func StartClientByVerifyKey(serverAddr, verifyKey, connType, proxyUrl *C.char) int {
  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. func main() {
  45. // Need a main function to make CGO compile package as C shared library
  46. }