MCPcopy
hub / github.com/gorilla/websocket / WriteControl

Method WriteControl

conn.go:413–472  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

411// WriteControl writes a control message with the given deadline. The allowed
412// message types are CloseMessage, PingMessage and PongMessage.
413func (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 }

Callers 10

advanceFrameMethod · 0.95
handleProtocolErrorMethod · 0.95
SetCloseHandlerMethod · 0.95
SetPingHandlerMethod · 0.95
TestControlFunction · 0.80
TestReadLimitFunction · 0.80
pingFunction · 0.80
echoCopyFunction · 0.80
echoReadAllFunction · 0.80

Calls 6

writeFatalMethod · 0.95
isControlFunction · 0.85
newMaskKeyFunction · 0.85
maskBytesFunction · 0.70
SetWriteDeadlineMethod · 0.45
WriteMethod · 0.45

Tested by 3

TestControlFunction · 0.64
TestReadLimitFunction · 0.64