use ByteCount type for Frame, Packet and PublicHeader lengths

This commit is contained in:
Marten Seemann
2016-05-10 23:40:22 +07:00
parent 174095d5aa
commit e345270e84
19 changed files with 53 additions and 53 deletions

View File

@@ -94,13 +94,13 @@ func (f *AckFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, pa
}
// MinLength of a written frame
func (f *AckFrame) MinLength() int {
func (f *AckFrame) MinLength() protocol.ByteCount {
l := 1 + 1 + 6 + 2 + 1 + 1 + 4
l += (1 + 2) * 0 /* TODO: num_timestamps */
if f.HasNACK() {
l += 1 + (6+1)*len(f.NackRanges)
}
return l
return protocol.ByteCount(l)
}
// HasNACK returns if the frame has NACK ranges

View File

@@ -404,7 +404,7 @@ var _ = Describe("AckFrame", func() {
LargestObserved: 1,
}
f.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(f.MinLength()).To(Equal(b.Len()))
Expect(f.MinLength()).To(Equal(protocol.ByteCount(b.Len())))
})
It("has proper min length with nack ranges", func() {
@@ -415,7 +415,7 @@ var _ = Describe("AckFrame", func() {
}
err := f.Write(b, 1, protocol.PacketNumberLen6, 32)
Expect(err).ToNot(HaveOccurred())
Expect(f.MinLength()).To(Equal(b.Len()))
Expect(f.MinLength()).To(Equal(protocol.ByteCount(b.Len())))
})
})
})

View File

@@ -26,7 +26,7 @@ func (f *BlockedFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber
}
// MinLength of a written frame
func (f *BlockedFrame) MinLength() int {
func (f *BlockedFrame) MinLength() protocol.ByteCount {
return 1 + 4
}

View File

@@ -28,7 +28,7 @@ var _ = Describe("BlockedFrame", func() {
It("has the correct min length", func() {
frame := BlockedFrame{StreamID: 3}
Expect(frame.MinLength()).To(Equal(5))
Expect(frame.MinLength()).To(Equal(protocol.ByteCount(5)))
})
})
})

View File

@@ -47,8 +47,8 @@ func ParseConnectionCloseFrame(r *bytes.Reader) (*ConnectionCloseFrame, error) {
}
// MinLength of a written frame
func (f *ConnectionCloseFrame) MinLength() int {
return 1 + 4 + 2 + len(f.ReasonPhrase)
func (f *ConnectionCloseFrame) MinLength() protocol.ByteCount {
return 1 + 4 + 2 + protocol.ByteCount(len(f.ReasonPhrase))
}
// Write writes an CONNECTION_CLOSE frame.

View File

@@ -78,7 +78,7 @@ var _ = Describe("ConnectionCloseFrame", func() {
ReasonPhrase: "foobar",
}
f.Write(b, 1, protocol.PacketNumberLen6, 0)
Expect(f.MinLength()).To(Equal(b.Len()))
Expect(f.MinLength()).To(Equal(protocol.ByteCount(b.Len())))
})
})

View File

@@ -9,5 +9,5 @@ import (
// A Frame in QUIC
type Frame interface {
Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, packetNumberLen protocol.PacketNumberLen, version protocol.VersionNumber) error
MinLength() int
MinLength() protocol.ByteCount
}

View File

@@ -28,6 +28,6 @@ func (f *PingFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber, p
}
// MinLength of a written frame
func (f *PingFrame) MinLength() int {
func (f *PingFrame) MinLength() protocol.ByteCount {
return 1
}

View File

@@ -28,7 +28,7 @@ var _ = Describe("PingFrame", func() {
It("has the correct min length", func() {
frame := PingFrame{}
Expect(frame.MinLength()).To(Equal(1))
Expect(frame.MinLength()).To(Equal(protocol.ByteCount(1)))
})
})
})

View File

@@ -20,7 +20,7 @@ func (f *RstStreamFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumb
}
// MinLength of a written frame
func (f *RstStreamFrame) MinLength() int {
func (f *RstStreamFrame) MinLength() protocol.ByteCount {
panic("RstStreamFrame: Write not yet implemented")
}

View File

@@ -31,7 +31,7 @@ func (f *StopWaitingFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNu
}
// MinLength of a written frame
func (f *StopWaitingFrame) MinLength() int {
func (f *StopWaitingFrame) MinLength() protocol.ByteCount {
return 1 + 1 + 6
}

View File

@@ -91,20 +91,20 @@ func (f *StreamFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketNumber,
}
// MinLength of a written frame
func (f *StreamFrame) MinLength() int {
func (f *StreamFrame) MinLength() protocol.ByteCount {
return 1 + 4 + 8 + 2 + 1
}
// MaybeSplitOffFrame removes the first n bytes and returns them as a separate frame. If n >= len(n), nil is returned and nothing is modified.
func (f *StreamFrame) MaybeSplitOffFrame(n int) *StreamFrame {
if n >= f.MinLength()-1+len(f.Data) {
func (f *StreamFrame) MaybeSplitOffFrame(n protocol.ByteCount) *StreamFrame {
if n >= f.MinLength()-1+protocol.ByteCount(len(f.Data)) {
return nil
}
n -= f.MinLength() - 1
defer func() {
f.Data = f.Data[n:]
f.Offset += protocol.ByteCount(n)
f.Offset += n
}()
return &StreamFrame{

View File

@@ -59,7 +59,7 @@ var _ = Describe("StreamFrame", func() {
Offset: 1,
}
f.Write(b, 1, protocol.PacketNumberLen6, 0)
Expect(f.MinLength()).To(Equal(b.Len()))
Expect(f.MinLength()).To(Equal(protocol.ByteCount(b.Len())))
})
})

View File

@@ -19,7 +19,7 @@ func (f *WindowUpdateFrame) Write(b *bytes.Buffer, packetNumber protocol.PacketN
}
// MinLength of a written frame
func (f *WindowUpdateFrame) MinLength() int {
func (f *WindowUpdateFrame) MinLength() protocol.ByteCount {
panic("WindowUpdateFrame: Write not yet implemented")
}