nps/cmd/npc/sdk.go

62 lines
1.1 KiB
Go
Raw Normal View History

2019-12-03 12:25:22 +00:00
package main
import (
2019-12-07 16:29:00 +00:00
"C"
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"
2019-12-07 16:29:00 +00:00
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/version"
2019-12-03 12:25:22 +00:00
"time"
)
2019-12-04 10:36:29 +00:00
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-07 16:29:00 +00:00
//export Version
func Version() *C.char {
return C.CString(version.VERSION)
}
func Logs() *C.char {
return C.CString(common.GetLogMsg())
}
2019-12-03 12:25:22 +00:00
func main() {
// Need a main function to make CGO compile package as C shared library
2019-12-07 16:29:00 +00:00
logs.SetLogger("store")
2019-12-03 12:25:22 +00:00
}