WriteHeader writes header binary representation into w.
(w io.Writer, h Header)
| 47 | |
| 48 | // WriteHeader writes header binary representation into w. |
| 49 | func WriteHeader(w io.Writer, h Header) error { |
| 50 | // Make slice of bytes with capacity 14 that could hold any header. |
| 51 | bts := make([]byte, MaxHeaderSize) |
| 52 | |
| 53 | if h.Fin { |
| 54 | bts[0] |= bit0 |
| 55 | } |
| 56 | bts[0] |= h.Rsv << 4 |
| 57 | bts[0] |= byte(h.OpCode) |
| 58 | |
| 59 | var n int |
| 60 | switch { |
| 61 | case h.Length <= len7: |
| 62 | bts[1] = byte(h.Length) |
| 63 | n = 2 |
| 64 | |
| 65 | case h.Length <= len16: |
| 66 | bts[1] = 126 |
| 67 | binary.BigEndian.PutUint16(bts[2:4], uint16(h.Length)) |
| 68 | n = 4 |
| 69 | |
| 70 | case h.Length <= len64: |
| 71 | bts[1] = 127 |
| 72 | binary.BigEndian.PutUint64(bts[2:10], uint64(h.Length)) |
| 73 | n = 10 |
| 74 | |
| 75 | default: |
| 76 | return ErrHeaderLengthUnexpected |
| 77 | } |
| 78 | |
| 79 | if h.Masked { |
| 80 | bts[1] |= bit0 |
| 81 | n += copy(bts[n:], h.Mask[:]) |
| 82 | } |
| 83 | |
| 84 | _, err := w.Write(bts[:n]) |
| 85 | |
| 86 | return err |
| 87 | } |
| 88 | |
| 89 | // WriteFrame writes frame binary representation into w. |
| 90 | func WriteFrame(w io.Writer, f Frame) error { |
searching dependent graphs…