Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection.go SendBuffMsg函数代码重复 #292

Open
trash-boy opened this issue Dec 15, 2023 · 3 comments
Open

connection.go SendBuffMsg函数代码重复 #292

trash-boy opened this issue Dec 15, 2023 · 3 comments

Comments

@trash-boy
Copy link
Contributor

原函数

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的函数,却没有调用,因此代码可以简化为

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)
}
@aceld
Copy link
Owner

aceld commented Dec 18, 2023

@trash-boy 您可以对此提交PR

@trash-boy
Copy link
Contributor Author

SendBuffMsg

提交PR了,请您看一看

@aceld
Copy link
Owner

aceld commented Dec 19, 2023

@trash-boy 感谢提交PR,已merge, #295

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants