Browse Source

优化修改sshd模块client端代码, 提升性能

niujiuru 2 weeks ago
parent
commit
07b4b6d8f8
5 changed files with 21 additions and 20 deletions
  1. 2 2
      Makefile
  2. 1 1
      readme.txt
  3. 1 1
      sshd/client/config.go
  4. 11 10
      sshd/client/coupler.go
  5. 6 6
      sshd/client/invoker.go

+ 2 - 2
Makefile

@@ -127,13 +127,13 @@ LIB6 = -Wl,-Bstatic -L./swapi -lswapi -L./air530z -lair530z -Wl,-Bdynamic -lc -l
 yfkj_sshd.out       : libswapi.a libair530z.a ./sshd/server/server.go
 	mkdir -p ./build
 	$(GO) mod tidy
-	$(SETGO_ENV) CGO_LDFLAGS="$(LIB6)" $(GO_BUILD) $(GO_FLAGS) -o $@ ./sshd/server/server.go
+	$(SETGO_ENV) CGO_LDFLAGS="$(LIB6)" $(GO_BUILD) $(GO_FLAGS) -o $@ ./sshd/server/*.go
 	@cp $@ ./build/$(basename $@)_$(DATE)$(suffix $@)
 
 yfkj_ssh_client.out : libswapi.a libair530z.a ./sshd/client/client.go
 	mkdir -p ./build
 	$(GO) mod tidy
-	$(SETGO_ENV) CGO_LDFLAGS="$(LIB6)" $(GO_BUILD) $(GO_FLAGS) -o $@ ./sshd/client
+	$(SETGO_ENV) CGO_LDFLAGS="$(LIB6)" $(GO_BUILD) $(GO_FLAGS) -o $@ ./sshd/client/*.go
 	@cp $@ ./build/$(basename $@)_$(DATE)$(suffix $@)
 
 # 综合应用测试程序

+ 1 - 1
readme.txt

@@ -37,7 +37,7 @@ SHELL脚本封装库, 后台守护软件运行等功能, 可以平替硬件看
 12、./camera          (相机统一调用)
 封装各类相机支持, 自动适配, 便于项目集成时灵活的使用各类相机
 
-13、./sshd            (远程运维模块) Todo: 待完善
+13、./sshd            (远程运维模块)
 实现SSH远程登录功能(reverse ssh),实现远程控制功能,运维管理
 
 14、./ymodem          (串口传输文件)

+ 1 - 1
sshd/client/config.go

@@ -29,7 +29,7 @@ var (
 )
 
 func loadAppConfig() error {
-	cfgFile := filepath.Join(baseapp.CFG_DIR, "config.ini")
+	cfgFile := filepath.Join(baseapp.EXEC_DIR, "config.ini")
 	cfgIni, err := ini.Load(cfgFile)
 	if err != nil {
 		return err

+ 11 - 10
sshd/client/coupler.go

@@ -28,16 +28,17 @@ type MQTTCoupler struct {
 
 	client      mqtt.Client
 	clientID    string
-	isConnected atomic.Bool /////// 标记是否已连接MQTT的Broker服务
+	isConnected atomic.Bool /// 标记是否已连接MQTT的Broker服务
 
 	imei     string // 设备唯一标识
 	subTopic string // 订阅应答主题:/yfkj/device/rpc/imei/ack
 	pubTopic string // 发布指令主题:/yfkj/device/rpc/imei/cmd
 	cwd      string // 当前工作目录
 
-	cmdMu     sync.Mutex                     ///// 串行执行的锁
-	pending   map[int]chan jsonrpc2.Response ///// 等待命令结果
-	pendingMu sync.Mutex                     ///// 等待结果的锁
+	// 交互式命令高频执行, 为了性能上的考虑-这里不使用"sync.Map"
+	cmdMu     sync.Mutex                      // 串行执行的锁
+	pending   map[int]chan *jsonrpc2.Response // 等待命令结果
+	pendingMu sync.Mutex                      // 等待结果的锁
 }
 
 func (c *MQTTCoupler) init2() error {
@@ -75,7 +76,7 @@ func (c *MQTTCoupler) init2() error {
 		}
 	}
 
-	c.pending = make(map[int]chan jsonrpc2.Response)
+	c.pending = make(map[int]chan *jsonrpc2.Response)
 
 	c.client = mqtt.NewClient(opts)
 	go c.keepOnline()
@@ -124,8 +125,8 @@ func (c *MQTTCoupler) connect() error {
 	return token.Error()
 }
 
-func (c *MQTTCoupler) doCmd(method string, params any, id ...int) (jsonrpc2.Response, error) {
-	zero := jsonrpc2.Response{}
+func (c *MQTTCoupler) doCmd(method string, params any, id ...int) (*jsonrpc2.Response, error) {
+	zero := &jsonrpc2.Response{}
 
 	if c.needSerialize(method) {
 		c.cmdMu.Lock()
@@ -143,7 +144,7 @@ func (c *MQTTCoupler) doCmd(method string, params any, id ...int) (jsonrpc2.Resp
 		return zero, err
 	}
 
-	ch := make(chan jsonrpc2.Response, 1)
+	ch := make(chan *jsonrpc2.Response, 1)
 
 	c.pendingMu.Lock()
 	c.pending[reqID] = ch
@@ -187,8 +188,8 @@ func (c *MQTTCoupler) doCmd(method string, params any, id ...int) (jsonrpc2.Resp
 func (c *MQTTCoupler) onCmdAck(client mqtt.Client, msg mqtt.Message) {
 	p := msg.Payload()
 
-	var resp jsonrpc2.Response
-	if err := json.Unmarshal(p, &resp); err != nil {
+	resp, err := jsonrpc2.ParseResponse(p)
+	if err != nil { //////////// 解析应答失败,忽略不管
 		return
 	}
 

+ 6 - 6
sshd/client/invoker.go

@@ -40,7 +40,7 @@ func (c *MQTTCoupler) needTimeoutEnd(method string) bool {
 }
 
 // 心跳检测
-func (c *MQTTCoupler) ping() (jsonrpc2.Response, error) {
+func (c *MQTTCoupler) ping() (*jsonrpc2.Response, error) {
 	params := struct {
 		ClientID string `json:"client_id"`
 	}{
@@ -52,7 +52,7 @@ func (c *MQTTCoupler) ping() (jsonrpc2.Response, error) {
 
 // 执行命令
 func (c *MQTTCoupler) exec(
-	cmd string) (shell.ExecuteResult, error) {
+	cmd string) (*shell.ExecuteResult, error) {
 	params := struct {
 		ClientID string `json:"client_id"`
 		shell.ExecuteParams
@@ -72,7 +72,7 @@ func (c *MQTTCoupler) exec(
 	exrs := shell.ExecuteResult{}
 	resp, err := c.doCmd(rpc_exec, params)
 	if err != nil {
-		return exrs, err
+		return &exrs, err
 	}
 
 	if resp.Error != nil { ////////////////// 错误应答
@@ -84,11 +84,11 @@ func (c *MQTTCoupler) exec(
 			exrs.Stderr = err.Error()
 		}
 	}
-	return exrs, nil
+	return &exrs, nil
 }
 
 // 中断执行
-func (c *MQTTCoupler) stop() (jsonrpc2.Response, error) {
+func (c *MQTTCoupler) stop() (*jsonrpc2.Response, error) {
 	params := struct {
 		ClientID string `json:"client_id"`
 	}{
@@ -99,7 +99,7 @@ func (c *MQTTCoupler) stop() (jsonrpc2.Response, error) {
 }
 
 // 关闭退出
-func (c *MQTTCoupler) quit() (jsonrpc2.Response, error) {
+func (c *MQTTCoupler) quit() (*jsonrpc2.Response, error) {
 	params := struct {
 		ClientID string `json:"client_id"`
 	}{