dongxiai3003 2018-02-21 19:54
浏览 716
已采纳

在Golang中将控制信号转换为字节ASCII十六进制代码

I am trying to capture signals like ctrl+c, ctrl+z, ctrl+\ in golang and write its equivalent hex code to websocket. I am doing something like below.

func (c *poc) writePump() {
    var b = make([]byte, 1)
    d := make(chan os.Signal, 1)
    signal.Notify(d, syscall.SIGINT, syscall.SIGTSTP, syscall.SIGQUIT)

    for {
        os.Stdin.Read(b)
        err = c.ws.WriteMessage(websocket.TextMessage, b)

        go func() {
            for {
                s := <-d
                err = cli.sendControlSignalToWebsockets(s)
            }
        }()

        if err != nil {
            log.Printf("Failed to send UTF8 char: %s", err)
        }
    }
}

func (c *poc) sendControlSignalToWebsockets(s os.Signal) error {
    var err error

    switch s {
    case syscall.SIGINT:
        err = c.ws.WriteMessage(websocket.TextMessage, []byte{'\003'})
    case syscall.SIGTSTP:
        err = c.ws.WriteMessage(websocket.TextMessage, []byte{'\x1a'})
    case syscall.SIGQUIT:
        err = c.ws.WriteMessage(websocket.TextMessage, []byte{'\x1c'})
    }

    if err != nil {
        return err
    }
    return nil
}

Instead I want to do something like, capturing all signals and instead of hardcoding each signals equivalent to it's hexcode, convert signal to hexcode.

func (c *poc) writePump() {
    var b = make([]byte, 1)
    d := make(chan os.Signal, 1)
    signal.Notify(d)

    for {
        os.Stdin.Read(b)
        err = c.ws.WriteMessage(websocket.TextMessage, b)

        go func() {
            for {
                s := <-d
                //???? do some conversions and write it to websocket
            }
        }()

        if err != nil {
            log.Printf("Failed to send UTF8 char: %s", err)
        }
    }
}

Curious to know if anyone has any idea. Thanks!

  • 写回答

1条回答 默认 最新

  • douyoupingji7238 2018-02-23 03:50
    关注

    It sounds like you're trying to communicate a signal over a websocket. And thus you need a way to encode it won't get confused with other characters so using the unprintable ascii characters is a reasonable move.

    Since there is no convention for mapping syscall signals into a byte, you can't just import it - but good news! - you get to create your own!

    Create a map[syscall.Signal]byte or map[os.Signal]byte and do the translation from there:

    // Create a mapping between syscalls and bytes
    var syscallByteMap = map[syscall.Signal]byte{
      syscall.SIGINT: '\003',
      ...more here...
    }
    
    ...truncated...
    
            go func() {
            for {
                s := <-d
                // Look up the signal in the map
                b, ok := syscallByteMap[s]
                if ok {
                    err = c.ws.WriteMessage(websocket.TextMessage, []byte{b})
                } else {
                    err = nil
                }
    
            }
        }()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转