Kaynağa Gözat

1, 优化修改sshd客户端代码, 使得Ctrl+C中断按键更加丝滑; 2, 上传目前打包好的可发布使用包

niujiuru 1 hafta önce
ebeveyn
işleme
ffe15fd9af

BIN
package/rtu_linux_modules_1.0.0.1.tar.gz


BIN
package/yfkj_ssh_client_1.0.0.1.tar.gz


+ 10 - 6
sshd/client/client.go

@@ -167,7 +167,7 @@ func term(pingState *atomic.Bool) {
 			break
 		}
 
-		fmt.Println("读取用户输入失败:", err)
+		fmt.Println(err)
 		continue
 
 	inputOK:
@@ -195,7 +195,7 @@ func term(pingState *atomic.Bool) {
 		executing.Store(false)
 
 		if err != nil {
-			fmt.Printf("执行错误: %v\n", err)
+			fmt.Printf("%v\n", err)
 			continue
 		}
 
@@ -231,14 +231,18 @@ func interruptLoop(pingState, executing, interrupted *atomic.Bool) {
 	go func() {
 		for range sigCh {
 			interrupted.Store(true)
-			if executing.Load() && pingState.Load() {
+			if executing.Load() {
 				select {
 				case coupler.interrupted <- struct{}{}:
 				default:
 				}
-				_, _ = coupler.stop()
-			}
-		}
+				if pingState.Load() {
+					go func() {
+						_, _ = coupler.stop()
+					}()
+				} // end if2
+			} //// end if1
+		} ////// end for
 	}()
 }
 

+ 20 - 13
sshd/client/coupler.go

@@ -169,23 +169,30 @@ func (c *MQTTCoupler) doCmd(method string, params any, id ...int) (*jsonrpc2.Res
 		return zero, token.Error()
 	}
 
-	var timer *time.Timer
-	var timeout <-chan time.Time
-	if c.isCtrlCommand(method) {
+	if c.isCtrlCommand(method) { // 控制指令, 等待结果或超时
+		var timer *time.Timer
+		var timeout <-chan time.Time
 		timer = time.NewTimer(3 * time.Second)
 		timeout = timer.C
 		defer timer.Stop()
-	}
 
-	select {
-	case <-c.ctx.Done():
-		return zero, c.ctx.Err()
-	case resp := <-ch:
-		return resp, nil
-	case <-timeout:
-		return zero, fmt.Errorf("command timeout")
-	case <-c.interrupted:
-		return zero, fmt.Errorf("command interrupted by user")
+		select {
+		case <-c.ctx.Done():
+			return zero, c.ctx.Err()
+		case resp := <-ch:
+			return resp, nil
+		case <-timeout:
+			return zero, fmt.Errorf("command timeout")
+		}
+	} else { // 用户指令, 等待结果或用户主动中断取消: Ctrl+C
+		select {
+		case <-c.ctx.Done():
+			return zero, c.ctx.Err()
+		case resp := <-ch:
+			return resp, nil
+		case <-c.interrupted:
+			return zero, fmt.Errorf("command interrupted by user")
+		}
 	}
 }