wire: remove FrameParser interface, expose FrameParser struct (#4284)

Instead, expose the FrameParser struct. This allows us to embed it
directly into the connection struct, avoiding a pointer indirection.
This commit is contained in:
Marten Seemann 2024-01-29 13:34:28 +07:00 committed by GitHub
parent 34c4d89e8b
commit be4838bd64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 14 additions and 19 deletions

View file

@ -454,7 +454,7 @@ func (s *connection) preSetup() {
s.handshakeStream = newCryptoStream() s.handshakeStream = newCryptoStream()
s.sendQueue = newSendQueue(s.conn) s.sendQueue = newSendQueue(s.conn)
s.retransmissionQueue = newRetransmissionQueue() s.retransmissionQueue = newRetransmissionQueue()
s.frameParser = wire.NewFrameParser(s.config.EnableDatagrams) s.frameParser = *wire.NewFrameParser(s.config.EnableDatagrams)
s.rttStats = &utils.RTTStats{} s.rttStats = &utils.RTTStats{}
s.connFlowController = flowcontrol.NewConnectionFlowController( s.connFlowController = flowcontrol.NewConnectionFlowController(
protocol.ByteCount(s.config.InitialConnectionReceiveWindow), protocol.ByteCount(s.config.InitialConnectionReceiveWindow),

View file

@ -36,7 +36,8 @@ const (
handshakeDoneFrameType = 0x1e handshakeDoneFrameType = 0x1e
) )
type frameParser struct { // The FrameParser parses QUIC frames, one by one.
type FrameParser struct {
r bytes.Reader // cached bytes.Reader, so we don't have to repeatedly allocate them r bytes.Reader // cached bytes.Reader, so we don't have to repeatedly allocate them
ackDelayExponent uint8 ackDelayExponent uint8
@ -47,11 +48,9 @@ type frameParser struct {
ackFrame *AckFrame ackFrame *AckFrame
} }
var _ FrameParser = &frameParser{}
// NewFrameParser creates a new frame parser. // NewFrameParser creates a new frame parser.
func NewFrameParser(supportsDatagrams bool) *frameParser { func NewFrameParser(supportsDatagrams bool) *FrameParser {
return &frameParser{ return &FrameParser{
r: *bytes.NewReader(nil), r: *bytes.NewReader(nil),
supportsDatagrams: supportsDatagrams, supportsDatagrams: supportsDatagrams,
ackFrame: &AckFrame{}, ackFrame: &AckFrame{},
@ -60,7 +59,7 @@ func NewFrameParser(supportsDatagrams bool) *frameParser {
// ParseNext parses the next frame. // ParseNext parses the next frame.
// It skips PADDING frames. // It skips PADDING frames.
func (p *frameParser) ParseNext(data []byte, encLevel protocol.EncryptionLevel, v protocol.VersionNumber) (int, Frame, error) { func (p *FrameParser) ParseNext(data []byte, encLevel protocol.EncryptionLevel, v protocol.VersionNumber) (int, Frame, error) {
startLen := len(data) startLen := len(data)
p.r.Reset(data) p.r.Reset(data)
frame, err := p.parseNext(&p.r, encLevel, v) frame, err := p.parseNext(&p.r, encLevel, v)
@ -69,7 +68,7 @@ func (p *frameParser) ParseNext(data []byte, encLevel protocol.EncryptionLevel,
return n, frame, err return n, frame, err
} }
func (p *frameParser) parseNext(r *bytes.Reader, encLevel protocol.EncryptionLevel, v protocol.VersionNumber) (Frame, error) { func (p *FrameParser) parseNext(r *bytes.Reader, encLevel protocol.EncryptionLevel, v protocol.VersionNumber) (Frame, error) {
for r.Len() != 0 { for r.Len() != 0 {
typ, err := quicvarint.Read(r) typ, err := quicvarint.Read(r)
if err != nil { if err != nil {
@ -95,7 +94,7 @@ func (p *frameParser) parseNext(r *bytes.Reader, encLevel protocol.EncryptionLev
return nil, nil return nil, nil
} }
func (p *frameParser) parseFrame(r *bytes.Reader, typ uint64, encLevel protocol.EncryptionLevel, v protocol.VersionNumber) (Frame, error) { func (p *FrameParser) parseFrame(r *bytes.Reader, typ uint64, encLevel protocol.EncryptionLevel, v protocol.VersionNumber) (Frame, error) {
var frame Frame var frame Frame
var err error var err error
if typ&0xf8 == 0x8 { if typ&0xf8 == 0x8 {
@ -163,7 +162,7 @@ func (p *frameParser) parseFrame(r *bytes.Reader, typ uint64, encLevel protocol.
return frame, nil return frame, nil
} }
func (p *frameParser) isAllowedAtEncLevel(f Frame, encLevel protocol.EncryptionLevel) bool { func (p *FrameParser) isAllowedAtEncLevel(f Frame, encLevel protocol.EncryptionLevel) bool {
switch encLevel { switch encLevel {
case protocol.EncryptionInitial, protocol.EncryptionHandshake: case protocol.EncryptionInitial, protocol.EncryptionHandshake:
switch f.(type) { switch f.(type) {
@ -186,6 +185,8 @@ func (p *frameParser) isAllowedAtEncLevel(f Frame, encLevel protocol.EncryptionL
} }
} }
func (p *frameParser) SetAckDelayExponent(exp uint8) { // SetAckDelayExponent sets the acknowledgment delay exponent (sent in the transport parameters).
// This value is used to scale the ACK Delay field in the ACK frame.
func (p *FrameParser) SetAckDelayExponent(exp uint8) {
p.ackDelayExponent = exp p.ackDelayExponent = exp
} }

View file

@ -14,7 +14,7 @@ var _ = Describe("Frame parsing", func() {
var parser FrameParser var parser FrameParser
BeforeEach(func() { BeforeEach(func() {
parser = NewFrameParser(true) parser = *NewFrameParser(true)
}) })
It("returns nil if there's nothing more to read", func() { It("returns nil if there's nothing more to read", func() {
@ -315,7 +315,7 @@ var _ = Describe("Frame parsing", func() {
}) })
It("errors when DATAGRAM frames are not supported", func() { It("errors when DATAGRAM frames are not supported", func() {
parser = NewFrameParser(false) parser = *NewFrameParser(false)
f := &DatagramFrame{Data: []byte("foobar")} f := &DatagramFrame{Data: []byte("foobar")}
b, err := f.Append(nil, protocol.Version1) b, err := f.Append(nil, protocol.Version1)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())

View file

@ -9,9 +9,3 @@ type Frame interface {
Append(b []byte, version protocol.VersionNumber) ([]byte, error) Append(b []byte, version protocol.VersionNumber) ([]byte, error)
Length(version protocol.VersionNumber) protocol.ByteCount Length(version protocol.VersionNumber) protocol.ByteCount
} }
// A FrameParser parses QUIC frames, one by one.
type FrameParser interface {
ParseNext([]byte, protocol.EncryptionLevel, protocol.VersionNumber) (int, Frame, error)
SetAckDelayExponent(uint8)
}