WriteControl writes a control message with the given deadline. The allowed message types are CloseMessage, PingMessage and PongMessage.
(messageType int, data []byte, deadline time.Time)
| 411 | // WriteControl writes a control message with the given deadline. The allowed |
| 412 | // message types are CloseMessage, PingMessage and PongMessage. |
| 413 | func (c *Conn) WriteControl(messageType int, data []byte, deadline time.Time) error { |
| 414 | if !isControl(messageType) { |
| 415 | return errBadWriteOpCode |
| 416 | } |
| 417 | if len(data) > maxControlFramePayloadSize { |
| 418 | return errInvalidControlFrame |
| 419 | } |
| 420 | |
| 421 | b0 := byte(messageType) | finalBit |
| 422 | b1 := byte(len(data)) |
| 423 | if !c.isServer { |
| 424 | b1 |= maskBit |
| 425 | } |
| 426 | |
| 427 | buf := make([]byte, 0, maxFrameHeaderSize+maxControlFramePayloadSize) |
| 428 | buf = append(buf, b0, b1) |
| 429 | |
| 430 | if c.isServer { |
| 431 | buf = append(buf, data...) |
| 432 | } else { |
| 433 | key := newMaskKey() |
| 434 | buf = append(buf, key[:]...) |
| 435 | buf = append(buf, data...) |
| 436 | maskBytes(key, 0, buf[6:]) |
| 437 | } |
| 438 | |
| 439 | d := 1000 * time.Hour |
| 440 | if !deadline.IsZero() { |
| 441 | d = deadline.Sub(time.Now()) |
| 442 | if d < 0 { |
| 443 | return errWriteTimeout |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | timer := time.NewTimer(d) |
| 448 | select { |
| 449 | case <-c.mu: |
| 450 | timer.Stop() |
| 451 | case <-timer.C: |
| 452 | return errWriteTimeout |
| 453 | } |
| 454 | defer func() { c.mu <- struct{}{} }() |
| 455 | |
| 456 | c.writeErrMu.Lock() |
| 457 | err := c.writeErr |
| 458 | c.writeErrMu.Unlock() |
| 459 | if err != nil { |
| 460 | return err |
| 461 | } |
| 462 | |
| 463 | c.conn.SetWriteDeadline(deadline) |
| 464 | _, err = c.conn.Write(buf) |
| 465 | if err != nil { |
| 466 | return c.writeFatal(err) |
| 467 | } |
| 468 | if messageType == CloseMessage { |
| 469 | c.writeFatal(ErrCloseSent) |
| 470 | } |