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

Method SendBuffMsg

znet/ws_connection.go:434–477  ·  view source on GitHub ↗

SendBuffMsg sends BuffMsg

(msgID uint32, data []byte, opts ...ziface.MsgSendOption)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected