(data []byte, opts ...ziface.MsgSendOption)
| 363 | } |
| 364 | |
| 365 | func (c *WsConnection) SendToQueue(data []byte, opts ...ziface.MsgSendOption) error { |
| 366 | c.msgLock.Lock() |
| 367 | defer c.msgLock.Unlock() |
| 368 | |
| 369 | if c.msgBuffChan == nil { |
| 370 | c.msgBuffChan = make(chan []byte, zconf.GlobalObject.MaxMsgChanLen) |
| 371 | // Start a goroutine for writing data back to the client, |
| 372 | // which only reads data from MsgBuffChan and hasn't allocated memory or started the coroutine until SendBuffMsg is called |
| 373 | // (开启用于写回客户端数据流程的Goroutine |
| 374 | // 此方法只读取MsgBuffChan中的数据没调用SendBuffMsg可以分配内存和启用协程) |
| 375 | go c.StartWriter() |
| 376 | } |
| 377 | |
| 378 | opt := ziface.MsgSendOptionObj{ |
| 379 | Timeout: 5 * time.Millisecond, |
| 380 | } |
| 381 | |
| 382 | for _, o := range opts { |
| 383 | o(&opt) |
| 384 | } |
| 385 | |
| 386 | idleTimeout := time.NewTimer(opt.Timeout) |
| 387 | defer idleTimeout.Stop() |
| 388 | |
| 389 | if c.isClosed == true { |
| 390 | return errors.New("WsConnection closed when send buff msg") |
| 391 | } |
| 392 | |
| 393 | if data == nil { |
| 394 | zlog.Ins().ErrorF("Pack data is nil") |
| 395 | return errors.New("Pack data is nil ") |
| 396 | } |
| 397 | |
| 398 | select { |
| 399 | case <-idleTimeout.C: |
| 400 | return errors.New("send buff msg timeout") |
| 401 | case c.msgBuffChan <- data: |
| 402 | return nil |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // SendMsg directly sends the Message data to the remote TCP client. |
| 407 | // (直接将Message数据发送数据给远程的TCP客户端) |
nothing calls this directly
no test coverage detected