(data []byte, opts ...ziface.MsgSendOption)
| 399 | } |
| 400 | |
| 401 | func (c *Connection) SendToQueue(data []byte, opts ...ziface.MsgSendOption) error { |
| 402 | |
| 403 | if c.msgBuffChan == nil && c.setStartWriterFlag() { |
| 404 | c.msgBuffChan = make(chan []byte, zconf.GlobalObject.MaxMsgChanLen) |
| 405 | // Start a Goroutine to write data back to the client |
| 406 | // This method only reads data from the MsgBuffChan without allocating memory or starting a Goroutine |
| 407 | // (开启用于写回客户端数据流程的Goroutine |
| 408 | // 此方法只读取MsgBuffChan中的数据没调用SendBuffMsg可以分配内存和启用协程) |
| 409 | go c.StartWriter() |
| 410 | } |
| 411 | |
| 412 | opt := ziface.MsgSendOptionObj{ |
| 413 | Timeout: 5 * time.Millisecond, |
| 414 | } |
| 415 | |
| 416 | for _, o := range opts { |
| 417 | o(&opt) |
| 418 | } |
| 419 | |
| 420 | idleTimeout := time.NewTimer(opt.Timeout) |
| 421 | defer idleTimeout.Stop() |
| 422 | |
| 423 | if c.isClosed() == true { |
| 424 | return errors.New("Connection closed when send buff msg") |
| 425 | } |
| 426 | |
| 427 | if data == nil { |
| 428 | zlog.Ins().ErrorF("Pack data is nil") |
| 429 | return errors.New("Pack data is nil") |
| 430 | } |
| 431 | |
| 432 | // Send timeout |
| 433 | select { |
| 434 | case <-c.ctx.Done(): |
| 435 | // Close all channels associated with the connection |
| 436 | close(c.msgBuffChan) |
| 437 | return errors.New("connection closed when send buff msg") |
| 438 | case <-idleTimeout.C: |
| 439 | return errors.New("send buff msg timeout") |
| 440 | case c.msgBuffChan <- data: |
| 441 | return nil |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // SendMsg directly sends Message data to the remote TCP client. |
| 446 | // (直接将Message数据发送数据给远程的TCP客户端) |
no test coverage detected