From 207d6dff48a888917d308d48b85621b428142351 Mon Sep 17 00:00:00 2001 From: lijunqiang Date: Mon, 25 Sep 2023 11:05:41 +0800 Subject: [PATCH] =?UTF-8?q?SDK=E4=B8=AD=E5=A2=9E=E5=8A=A0=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=9C=AC=E5=9C=B0P2P=E5=92=8C=E7=A7=81=E5=AF=86?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/npc/sdk.go | 38 +++++++++++++++++++++++++++++++++++++- docs/npc_sdk.md | 13 ++++++++----- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/cmd/npc/sdk.go b/cmd/npc/sdk.go index 51f60d0..a706b42 100644 --- a/cmd/npc/sdk.go +++ b/cmd/npc/sdk.go @@ -4,11 +4,17 @@ import ( "C" "ehang.io/nps/client" "ehang.io/nps/lib/common" + "ehang.io/nps/lib/config" + "ehang.io/nps/lib/file" "ehang.io/nps/lib/version" "github.com/astaxie/beego/logs" + "strconv" ) -var cl *client.TRPClient +var ( + cl *client.TRPClient + ls bool +) //export StartClientByVerifyKey func StartClientByVerifyKey(serverAddr, verifyKey, connType, proxyUrl *C.char) int { @@ -21,8 +27,34 @@ func StartClientByVerifyKey(serverAddr, verifyKey, connType, proxyUrl *C.char) i return 1 } +//export StartLocalServer +func StartLocalServer(serverAddr, verifyKey, connType, password, localType, localPortStr, target, proxyUrl *C.char) int { + ls = true + _ = logs.SetLogger("store") + var localPort int + localPort, _ = strconv.Atoi(C.GoString(localPortStr)) + client.CloseLocalServer() + commonConfig := new(config.CommonConfig) + commonConfig.Server = C.GoString(serverAddr) + commonConfig.VKey = C.GoString(verifyKey) + commonConfig.Tp = C.GoString(connType) + commonConfig.ProxyUrl = C.GoString(proxyUrl) + localServer := new(config.LocalServer) + localServer.Type = C.GoString(localType) + localServer.Password = C.GoString(password) + localServer.Target = C.GoString(target) + localServer.Port = localPort + commonConfig.Client = new(file.Client) + commonConfig.Client.Cnf = new(file.Config) + client.StartLocalServer(localServer, commonConfig) + return 1 +} + //export GetClientStatus func GetClientStatus() int { + if ls && len(client.LocalServer) > 0 { + return 1 + } return client.NowStatus } @@ -31,6 +63,10 @@ func CloseClient() { if cl != nil { cl.Close() } + if ls { + client.CloseLocalServer() + ls = false + } } //export Version diff --git a/docs/npc_sdk.md b/docs/npc_sdk.md index 7ee0a49..cd5328f 100644 --- a/docs/npc_sdk.md +++ b/docs/npc_sdk.md @@ -3,12 +3,15 @@ ``` 命令行模式启动客户端 从v0.26.10开始,此函数会阻塞,直到客户端退出返回,请自行管理是否重连 -p0->连接地址 -p1->vkey -p2->连接类型(tcp or udp) -p3->连接代理 +serverAddr->连接地址 +verifyKey->vkey +connType->连接类型(tcp or udp) +proxyUrl->连接代理 -extern GoInt StartClientByVerifyKey(char* p0, char* p1, char* p2, char* p3); +extern GoInt StartClientByVerifyKey(char* serverAddr, char* verifyKey, char* connType, char* proxyUrl); + +命令行模式启动本地P2P或私密连接 +extern GoInt StartLocalServer(char* serverAddr, char* verifyKey, char* connType, char* password, char* localType, char* localPortStr, char* target, char* proxyUrl); 查看当前启动的客户端状态,在线为1,离线为0 extern GoInt GetClientStatus();