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

Method flushFrame

conn.go:556–641  ·  conn.go::messageWriter.flushFrame

flushFrame writes buffered data and extra as a frame to the network. The final argument indicates that this is the last frame in the message.

(final bool, extra []byte)

Source from the content-addressed store, hash-verified

554// flushFrame writes buffered data and extra as a frame to the network. The
555// final argument indicates that this is the last frame in the message.
556func (w *messageWriter) flushFrame(final bool, extra []byte) error {
557 c := w.c
558 length := w.pos - maxFrameHeaderSize + len(extra)
559
560 // Check for invalid control frames.
561 if isControl(w.frameType) &&
562 (!final || length > maxControlFramePayloadSize) {
563 return w.endMessage(errInvalidControlFrame)
564 }
565
566 b0 := byte(w.frameType)
567 if final {
568 b0 |= finalBit
569 }
570 if w.compress {
571 b0 |= rsv1Bit
572 }
573 w.compress = false
574
575 b1 := byte(0)
576 if !c.isServer {
577 b1 |= maskBit
578 }
579
580 // Assume that the frame starts at beginning of c.writeBuf.
581 framePos := 0
582 if c.isServer {
583 // Adjust up if mask not included in the header.
584 framePos = 4
585 }
586
587 switch {
588 case length >= 65536:
589 c.writeBuf[framePos] = b0
590 c.writeBuf[framePos+1] = b1 | 127
591 binary.BigEndian.PutUint64(c.writeBuf[framePos+2:], uint64(length))
592 case length > 125:
593 framePos += 6
594 c.writeBuf[framePos] = b0
595 c.writeBuf[framePos+1] = b1 | 126
596 binary.BigEndian.PutUint16(c.writeBuf[framePos+2:], uint16(length))
597 default:
598 framePos += 8
599 c.writeBuf[framePos] = b0
600 c.writeBuf[framePos+1] = b1 | byte(length)
601 }
602
603 if !c.isServer {
604 key := newMaskKey()
605 copy(c.writeBuf[maxFrameHeaderSize-4:], key[:])
606 maskBytes(key, 0, c.writeBuf[maxFrameHeaderSize:w.pos])
607 if len(extra) > 0 {
608 return w.endMessage(c.writeFatal(errors.New("websocket: internal error, extra used in client mode")))
609 }
610 }
611
612 // Write the buffers to the connection with best-effort detection of
613 // concurrent writes. See the concurrency section in the package

Callers 5

ncopyMethod · 0.95
WriteMethod · 0.95
ReadFromMethod · 0.95
CloseMethod · 0.95
WriteMessageMethod · 0.95

Calls 6

endMessageMethod · 0.95
isControlFunction · 0.85
newMaskKeyFunction · 0.85
writeFatalMethod · 0.80
writeMethod · 0.80
maskBytesFunction · 0.70

Tested by

no test coverage detected