MCPcopy
hub / github.com/aceld/zinx / SendBuffMsg

Method SendBuffMsg

znet/kcp_connection.go:431–468  ·  view source on GitHub ↗
(msgID uint32, data []byte, opts ...ziface.MsgSendOption)

Source from the content-addressed store, hash-verified

429}
430
431func (c *KcpConnection) SendBuffMsg(msgID uint32, data []byte, opts ...ziface.MsgSendOption) error {
432 if c.isClosed() {
433 return errors.New("connection closed when send buff msg")
434 }
435 if c.msgBuffChan == nil {
436 c.msgBuffChan = make(chan []byte, zconf.GlobalObject.MaxMsgChanLen)
437 // Start a Goroutine to write data back to the client
438 // This method only reads data from the MsgBuffChan without allocating memory or starting a Goroutine
439 // (开启用于写回客户端数据流程的Goroutine
440 // 此方法只读取MsgBuffChan中的数据没调用SendBuffMsg可以分配内存和启用协程)
441 go c.StartWriter()
442 }
443
444 opt := ziface.MsgSendOptionObj{
445 Timeout: 5 * time.Millisecond,
446 }
447
448 for _, o := range opts {
449 o(&opt)
450 }
451
452 idleTimeout := time.NewTimer(opt.Timeout)
453 defer idleTimeout.Stop()
454
455 msg, err := c.packet.Pack(zpack.NewMsgPackage(msgID, data))
456 if err != nil {
457 zlog.Ins().ErrorF("Pack error msg ID = %d", msgID)
458 return errors.New("Pack error msg ")
459 }
460
461 // send timeout
462 select {
463 case <-idleTimeout.C:
464 return errors.New("send buff msg timeout")
465 case c.msgBuffChan <- msg:
466 return nil
467 }
468}
469
470func (c *KcpConnection) SetProperty(key string, value interface{}) {
471 c.propertyLock.Lock()

Callers

nothing calls this directly

Calls 7

isClosedMethod · 0.95
StartWriterMethod · 0.95
NewMsgPackageFunction · 0.92
InsFunction · 0.92
StopMethod · 0.65
PackMethod · 0.65
ErrorFMethod · 0.65

Tested by

no test coverage detected