nps/cmd/npc/sdk.go

54 lines
988 B
Go
Raw Normal View History

2019-12-03 12:25:22 +00:00
package main
import "C"
import (
2019-12-04 10:36:29 +00:00
"github.com/astaxie/beego/logs"
2019-12-03 12:25:22 +00:00
"github.com/cnlh/nps/client"
"time"
)
2019-12-04 10:36:29 +00:00
func init() {
logs.SetLogger(logs.AdapterFile, `{"filename":"npc.log","daily":false,"maxlines":100000,"color":true}`)
}
var status int
var closeBefore int
2019-12-03 17:54:23 +00:00
var cl *client.TRPClient
2019-12-03 12:25:22 +00:00
2019-12-03 17:54:23 +00:00
//export StartClientByVerifyKey
2019-12-04 10:36:29 +00:00
func StartClientByVerifyKey(serverAddr, verifyKey, connType, proxyUrl *C.char) int {
2019-12-03 17:54:23 +00:00
if cl != nil {
2019-12-04 10:36:29 +00:00
closeBefore = 1
2019-12-03 17:54:23 +00:00
cl.Close()
}
2019-12-04 10:11:24 +00:00
cl = client.NewRPClient(C.GoString(serverAddr), C.GoString(verifyKey), C.GoString(connType), C.GoString(proxyUrl), nil)
2019-12-04 10:36:29 +00:00
closeBefore = 0
2019-12-03 12:25:22 +00:00
go func() {
for {
2019-12-04 10:36:29 +00:00
status = 1
2019-12-03 17:54:23 +00:00
cl.Start()
2019-12-04 10:36:29 +00:00
status = 0
if closeBefore == 1 {
2019-12-03 17:54:23 +00:00
return
}
2019-12-03 12:25:22 +00:00
time.Sleep(time.Second * 5)
}
}()
2019-12-04 10:36:29 +00:00
return 1
2019-12-03 12:25:22 +00:00
}
2019-12-03 17:54:23 +00:00
//export GetClientStatus
2019-12-04 10:36:29 +00:00
func GetClientStatus() int {
2019-12-03 17:54:23 +00:00
return status
}
//export CloseClient
func CloseClient() {
2019-12-04 10:36:29 +00:00
closeBefore = 1
2019-12-03 17:54:23 +00:00
cl.Close()
}
2019-12-03 12:25:22 +00:00
func main() {
// Need a main function to make CGO compile package as C shared library
}