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
                }
    
            }
        }()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况