remove caching of streamID lengths in StreamFrame

fixes the final failing test of #83
This commit is contained in:
Lucas Clemente 2016-07-08 18:05:47 +02:00
parent 87c1a2f004
commit 37f2066933
3 changed files with 16 additions and 38 deletions

View file

@ -14,7 +14,6 @@ import (
type StreamFrame struct { type StreamFrame struct {
FinBit bool FinBit bool
StreamID protocol.StreamID StreamID protocol.StreamID
streamIDLen protocol.ByteCount
Offset protocol.ByteCount Offset protocol.ByteCount
Data []byte Data []byte
DataLenPresent bool DataLenPresent bool
@ -40,9 +39,9 @@ func ParseStreamFrame(r *bytes.Reader) (*StreamFrame, error) {
if offsetLen != 0 { if offsetLen != 0 {
offsetLen++ offsetLen++
} }
frame.streamIDLen = protocol.ByteCount(typeByte&0x03 + 1) streamIDLen := typeByte&0x03 + 1
sid, err := utils.ReadUintN(r, uint8(frame.streamIDLen)) sid, err := utils.ReadUintN(r, streamIDLen)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -104,14 +103,12 @@ func (f *StreamFrame) Write(b *bytes.Buffer, version protocol.VersionNumber) err
typeByte ^= (uint8(offsetLength) - 1) << 2 typeByte ^= (uint8(offsetLength) - 1) << 2
} }
if f.streamIDLen == 0 { streamIDLen := f.calculateStreamIDLength()
f.calculateStreamIDLength() typeByte ^= streamIDLen - 1
}
typeByte ^= uint8(f.streamIDLen) - 1
b.WriteByte(typeByte) b.WriteByte(typeByte)
switch f.streamIDLen { switch streamIDLen {
case 1: case 1:
b.WriteByte(uint8(f.StreamID)) b.WriteByte(uint8(f.StreamID))
case 2: case 2:
@ -153,16 +150,15 @@ func (f *StreamFrame) Write(b *bytes.Buffer, version protocol.VersionNumber) err
return nil return nil
} }
func (f *StreamFrame) calculateStreamIDLength() { func (f *StreamFrame) calculateStreamIDLength() uint8 {
if f.StreamID < (1 << 8) { if f.StreamID < (1 << 8) {
f.streamIDLen = 1 return 1
} else if f.StreamID < (1 << 16) { } else if f.StreamID < (1 << 16) {
f.streamIDLen = 2 return 2
} else if f.StreamID < (1 << 24) { } else if f.StreamID < (1 << 24) {
f.streamIDLen = 3 return 3
} else {
f.streamIDLen = 4
} }
return 4
} }
func (f *StreamFrame) getOffsetLength() protocol.ByteCount { func (f *StreamFrame) getOffsetLength() protocol.ByteCount {
@ -192,11 +188,7 @@ func (f *StreamFrame) getOffsetLength() protocol.ByteCount {
// MinLength of a written frame // MinLength of a written frame
func (f *StreamFrame) MinLength(protocol.VersionNumber) (protocol.ByteCount, error) { func (f *StreamFrame) MinLength(protocol.VersionNumber) (protocol.ByteCount, error) {
if f.streamIDLen == 0 { length := protocol.ByteCount(1) + protocol.ByteCount(f.calculateStreamIDLength()) + f.getOffsetLength()
f.calculateStreamIDLength()
}
length := protocol.ByteCount(1) + f.streamIDLen + f.getOffsetLength()
if f.DataLenPresent { if f.DataLenPresent {
length += 2 length += 2
} }

View file

@ -248,16 +248,6 @@ var _ = Describe("StreamFrame", func() {
}) })
Context("lengths of StreamIDs", func() { Context("lengths of StreamIDs", func() {
It("returns an error for a non-valid StreamID length", func() {
b := &bytes.Buffer{}
err := (&StreamFrame{
StreamID: 1,
streamIDLen: 13,
Data: []byte("foobar"),
}).Write(b, 0)
Expect(err).To(MatchError(errInvalidStreamIDLen))
})
It("writes a 1 byte StreamID", func() { It("writes a 1 byte StreamID", func() {
b := &bytes.Buffer{} b := &bytes.Buffer{}
err := (&StreamFrame{ err := (&StreamFrame{
@ -320,26 +310,22 @@ var _ = Describe("StreamFrame", func() {
Context("shortening of StreamIDs", func() { Context("shortening of StreamIDs", func() {
It("determines the length of a 1 byte StreamID", func() { It("determines the length of a 1 byte StreamID", func() {
f := &StreamFrame{StreamID: 0xFF} f := &StreamFrame{StreamID: 0xFF}
f.calculateStreamIDLength() Expect(f.calculateStreamIDLength()).To(Equal(uint8(1)))
Expect(f.streamIDLen).To(Equal(protocol.ByteCount(1)))
}) })
It("determines the length of a 2 byte StreamID", func() { It("determines the length of a 2 byte StreamID", func() {
f := &StreamFrame{StreamID: 0xFFFF} f := &StreamFrame{StreamID: 0xFFFF}
f.calculateStreamIDLength() Expect(f.calculateStreamIDLength()).To(Equal(uint8(2)))
Expect(f.streamIDLen).To(Equal(protocol.ByteCount(2)))
}) })
It("determines the length of a 3 byte StreamID", func() { It("determines the length of a 3 byte StreamID", func() {
f := &StreamFrame{StreamID: 0xFFFFFF} f := &StreamFrame{StreamID: 0xFFFFFF}
f.calculateStreamIDLength() Expect(f.calculateStreamIDLength()).To(Equal(uint8(3)))
Expect(f.streamIDLen).To(Equal(protocol.ByteCount(3)))
}) })
It("determines the length of a 4 byte StreamID", func() { It("determines the length of a 4 byte StreamID", func() {
f := &StreamFrame{StreamID: 0xFFFFFFFF} f := &StreamFrame{StreamID: 0xFFFFFFFF}
f.calculateStreamIDLength() Expect(f.calculateStreamIDLength()).To(Equal(uint8(4)))
Expect(f.streamIDLen).To(Equal(protocol.ByteCount(4)))
}) })
}) })

View file

@ -45,7 +45,7 @@ var _ = Describe("Chrome tests", func() {
close(done) close(done)
}, 5) }, 5)
PIt("loads a large number of files", func(done Done) { It("loads a large number of files", func(done Done) {
err := wd.Get("https://quic.clemente.io/tiles") err := wd.Get("https://quic.clemente.io/tiles")
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Eventually(func() error { Eventually(func() error {