writeFrameHeader writes the bytes of the header to w. See https://tools.ietf.org/html/rfc6455#section-5.2
(h header, w *bufio.Writer, buf []byte)
| 108 | // writeFrameHeader writes the bytes of the header to w. |
| 109 | // See https://tools.ietf.org/html/rfc6455#section-5.2 |
| 110 | func writeFrameHeader(h header, w *bufio.Writer, buf []byte) (err error) { |
| 111 | defer errd.Wrap(&err, "failed to write frame header") |
| 112 | |
| 113 | var b byte |
| 114 | if h.fin { |
| 115 | b |= 1 << 7 |
| 116 | } |
| 117 | if h.rsv1 { |
| 118 | b |= 1 << 6 |
| 119 | } |
| 120 | if h.rsv2 { |
| 121 | b |= 1 << 5 |
| 122 | } |
| 123 | if h.rsv3 { |
| 124 | b |= 1 << 4 |
| 125 | } |
| 126 | |
| 127 | b |= byte(h.opcode) |
| 128 | |
| 129 | err = w.WriteByte(b) |
| 130 | if err != nil { |
| 131 | return err |
| 132 | } |
| 133 | |
| 134 | lengthByte := byte(0) |
| 135 | if h.masked { |
| 136 | lengthByte |= 1 << 7 |
| 137 | } |
| 138 | |
| 139 | switch { |
| 140 | case h.payloadLength > math.MaxUint16: |
| 141 | lengthByte |= 127 |
| 142 | case h.payloadLength > 125: |
| 143 | lengthByte |= 126 |
| 144 | case h.payloadLength >= 0: |
| 145 | lengthByte |= byte(h.payloadLength) |
| 146 | } |
| 147 | err = w.WriteByte(lengthByte) |
| 148 | if err != nil { |
| 149 | return err |
| 150 | } |
| 151 | |
| 152 | switch { |
| 153 | case h.payloadLength > math.MaxUint16: |
| 154 | binary.BigEndian.PutUint64(buf, uint64(h.payloadLength)) |
| 155 | _, err = w.Write(buf) |
| 156 | case h.payloadLength > 125: |
| 157 | binary.BigEndian.PutUint16(buf, uint16(h.payloadLength)) |
| 158 | _, err = w.Write(buf[:2]) |
| 159 | } |
| 160 | if err != nil { |
| 161 | return err |
| 162 | } |
| 163 | |
| 164 | if h.masked { |
| 165 | binary.LittleEndian.PutUint32(buf, h.maskKey) |
| 166 | _, err = w.Write(buf[:4]) |
| 167 | if err != nil { |
searching dependent graphs…