refactor header writing to append to a byte slice (#3646)

This avoids having to allocate a bytes.Buffer.
This commit is contained in:
Marten Seemann
2023-01-17 01:56:06 -08:00
committed by GitHub
parent 3d4bbc28ba
commit c24fbb094c
18 changed files with 282 additions and 279 deletions

View File

@@ -1,7 +1,7 @@
package testutils
import (
"bytes"
"fmt"
"github.com/lucas-clemente/quic-go/internal/handshake"
"github.com/lucas-clemente/quic-go/internal/protocol"
@@ -13,9 +13,11 @@ import (
// writePacket returns a new raw packet with the specified header and payload
func writePacket(hdr *wire.ExtendedHeader, data []byte) []byte {
buf := &bytes.Buffer{}
hdr.Write(buf, hdr.Version)
return append(buf.Bytes(), data...)
b, err := hdr.Append(nil, hdr.Version)
if err != nil {
panic(fmt.Sprintf("failed to write header: %s", err))
}
return append(b, data...)
}
// packRawPayload returns a new raw payload containing given frames