2022-06-04 23:06:40 +00:00
|
|
|
package chunk
|
2022-05-13 17:04:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Chunk3 is a type 3 chunk.
|
|
|
|
// Type 3 chunks have no message header. The stream ID, message length
|
|
|
|
// and timestamp delta fields are not present; chunks of this type take
|
|
|
|
// values from the preceding chunk for the same Chunk Stream ID. When a
|
|
|
|
// single message is split into chunks, all chunks of a message except
|
|
|
|
// the first one SHOULD use this type.
|
|
|
|
type Chunk3 struct {
|
|
|
|
ChunkStreamID byte
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:57:29 +00:00
|
|
|
// Read reads the chunk.
|
2022-06-08 18:47:36 +00:00
|
|
|
func (c *Chunk3) Read(r io.Reader, chunkBodyLen uint32) error {
|
2022-05-16 09:57:29 +00:00
|
|
|
header := make([]byte, 1)
|
2022-07-17 13:17:18 +00:00
|
|
|
_, err := io.ReadFull(r, header)
|
2022-05-16 09:57:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ChunkStreamID = header[0] & 0x3F
|
|
|
|
|
|
|
|
c.Body = make([]byte, chunkBodyLen)
|
2022-07-17 13:17:18 +00:00
|
|
|
_, err = io.ReadFull(r, c.Body)
|
2022-05-16 09:57:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-27 15:51:54 +00:00
|
|
|
// Marshal writes the chunk.
|
|
|
|
func (c Chunk3) Marshal() ([]byte, error) {
|
2022-06-08 12:07:30 +00:00
|
|
|
buf := make([]byte, 1+len(c.Body))
|
|
|
|
buf[0] = 3<<6 | c.ChunkStreamID
|
|
|
|
copy(buf[1:], c.Body)
|
|
|
|
return buf, nil
|
2022-05-13 17:04:47 +00:00
|
|
|
}
|