aceld / aceld/zinx

connection.go SendBuffMsg函数代码重复

未关闭
#292 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Go
星标
7.8k
派生
1.3k
PR 合并指标
30 天内没有已合并 PR

描述

原函数
```ruby
func (c *Connection) SendBuffMsg(msgID uint32, data []byte) error {
if c.isClosed() == true {
return errors.New("connection closed when send buff msg")
}
if c.msgBuffChan == nil && c.setStartWriterFlag() {
c.msgBuffChan = make(chan []byte, zconf.GlobalObject.MaxMsgChanLen)
// Start a Goroutine to write data back to the client
// This method only reads data from the MsgBuffChan without allocating memory or starting a Goroutine
// (开启用于写回客户端数据流程的Goroutine
// 此方法只读取MsgBuffChan中的数据没调用SendBuffMsg可以分配内存和启用协程)
go c.StartWriter()
}

idleTimeout := time.NewTimer(5 * time.Millisecond)
defer idleTimeout.Stop()

msg, err := c.packet.Pack(zpack.NewMsgPackage(msgID, data))
if err != nil {
zlog.Ins().ErrorF("Pack error msg ID = %d", msgID)
return errors.New("Pack error msg ")
}

// send timeout
select {
case <-idleTimeout.C:
return errors.New("send buff msg timeout")
case c.msgBuffChan <- msg:
return nil
}
}
```
其实前文已经写了一个SendToQueue的函数,却没有调用,因此代码可以简化为
```ruby
func (c *Connection) SendBuffMsg(msgID uint32, data []byte) error {
msg, err := c.packet.Pack(zpack.NewMsgPackage(msgID, data))
if err != nil {
zlog.Ins().ErrorF("Pack error msg ID = %d", msgID)
return errors.New("Pack error msg ")
}
return c.SendToQueue(msg)
}
```

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。