store the payload of sent packets as a slice of Frames

This commit is contained in:
Marten Seemann 2016-04-26 20:35:30 +07:00
parent eada642cc1
commit 4659bf7acb
5 changed files with 43 additions and 35 deletions

View file

@ -1,11 +1,14 @@
package ackhandler package ackhandler
import "github.com/lucas-clemente/quic-go/protocol" import (
"github.com/lucas-clemente/quic-go/frames"
"github.com/lucas-clemente/quic-go/protocol"
)
// A Packet is a packet // A Packet is a packet
type Packet struct { type Packet struct {
PacketNumber protocol.PacketNumber PacketNumber protocol.PacketNumber
Plaintext []byte Frames []frames.Frame
EntropyBit bool EntropyBit bool
Entropy EntropyAccumulator Entropy EntropyAccumulator

View file

@ -9,15 +9,20 @@ import (
var _ = Describe("SentPacketHandler", func() { var _ = Describe("SentPacketHandler", func() {
var handler *sentPacketHandler var handler *sentPacketHandler
var streamFrame frames.StreamFrame
BeforeEach(func() { BeforeEach(func() {
handler = NewSentPacketHandler().(*sentPacketHandler) handler = NewSentPacketHandler().(*sentPacketHandler)
streamFrame = frames.StreamFrame{
StreamID: 5,
Data: []byte{0x13, 0x37},
}
}) })
Context("SentPacket", func() { Context("SentPacket", func() {
It("accepts two consecutive packets", func() { It("accepts two consecutive packets", func() {
entropy := EntropyAccumulator(0) entropy := EntropyAccumulator(0)
packet1 := Packet{PacketNumber: 1, Plaintext: []byte{0x13, 0x37}, EntropyBit: true} packet1 := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, EntropyBit: true}
packet2 := Packet{PacketNumber: 2, Plaintext: []byte{0xBE, 0xEF}, EntropyBit: true} packet2 := Packet{PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, EntropyBit: true}
err := handler.SentPacket(&packet1) err := handler.SentPacket(&packet1)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.SentPacket(&packet2) err = handler.SentPacket(&packet2)
@ -34,8 +39,8 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("rejects packets with the same packet number", func() { It("rejects packets with the same packet number", func() {
packet1 := Packet{PacketNumber: 1, Plaintext: []byte{0x13, 0x37}, EntropyBit: true} packet1 := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, EntropyBit: true}
packet2 := Packet{PacketNumber: 1, Plaintext: []byte{0xBE, 0xEF}, EntropyBit: false} packet2 := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, EntropyBit: false}
err := handler.SentPacket(&packet1) err := handler.SentPacket(&packet1)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.SentPacket(&packet2) err = handler.SentPacket(&packet2)
@ -45,8 +50,8 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("rejects non-consecutive packets", func() { It("rejects non-consecutive packets", func() {
packet1 := Packet{PacketNumber: 1, Plaintext: []byte{0x13, 0x37}, EntropyBit: true} packet1 := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, EntropyBit: true}
packet2 := Packet{PacketNumber: 3, Plaintext: []byte{0xBE, 0xEF}, EntropyBit: false} packet2 := Packet{PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, EntropyBit: false}
err := handler.SentPacket(&packet1) err := handler.SentPacket(&packet1)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
err = handler.SentPacket(&packet2) err = handler.SentPacket(&packet2)
@ -58,8 +63,8 @@ var _ = Describe("SentPacketHandler", func() {
}) })
It("correctly calculates the entropy, even if the last packet has already been ACKed", func() { It("correctly calculates the entropy, even if the last packet has already been ACKed", func() {
packet1 := Packet{PacketNumber: 1, Plaintext: []byte{0x13, 0x37}, EntropyBit: true} packet1 := Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, EntropyBit: true}
packet2 := Packet{PacketNumber: 2, Plaintext: []byte{0xBE, 0xEF}, EntropyBit: true} packet2 := Packet{PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, EntropyBit: true}
err := handler.SentPacket(&packet1) err := handler.SentPacket(&packet1)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
entropy := EntropyAccumulator(0) entropy := EntropyAccumulator(0)
@ -85,12 +90,12 @@ var _ = Describe("SentPacketHandler", func() {
BeforeEach(func() { BeforeEach(func() {
entropy = EntropyAccumulator(0) entropy = EntropyAccumulator(0)
packets = []*Packet{ packets = []*Packet{
&Packet{PacketNumber: 1, Plaintext: []byte{0x13, 0x37}, EntropyBit: true}, &Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, EntropyBit: true},
&Packet{PacketNumber: 2, Plaintext: []byte{0xBE, 0xEF}, EntropyBit: true}, &Packet{PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, EntropyBit: true},
&Packet{PacketNumber: 3, Plaintext: []byte{0xCA, 0xFE}, EntropyBit: true}, &Packet{PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, EntropyBit: true},
&Packet{PacketNumber: 4, Plaintext: []byte{0x54, 0x32}, EntropyBit: true}, &Packet{PacketNumber: 4, Frames: []frames.Frame{&streamFrame}, EntropyBit: true},
&Packet{PacketNumber: 5, Plaintext: []byte{0x12, 0x42}, EntropyBit: true}, &Packet{PacketNumber: 5, Frames: []frames.Frame{&streamFrame}, EntropyBit: true},
&Packet{PacketNumber: 6, Plaintext: []byte{0xCA, 0xFE}, EntropyBit: true}, &Packet{PacketNumber: 6, Frames: []frames.Frame{&streamFrame}, EntropyBit: true},
} }
for _, packet := range packets { for _, packet := range packets {
handler.SentPacket(packet) handler.SentPacket(packet)
@ -227,12 +232,12 @@ var _ = Describe("SentPacketHandler", func() {
BeforeEach(func() { BeforeEach(func() {
packets = []*Packet{ packets = []*Packet{
&Packet{PacketNumber: 1, Plaintext: []byte{0x13, 0x37}, EntropyBit: false}, &Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
&Packet{PacketNumber: 2, Plaintext: []byte{0xBE, 0xEF}, EntropyBit: false}, &Packet{PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
&Packet{PacketNumber: 3, Plaintext: []byte{0xCA, 0xFE}, EntropyBit: false}, &Packet{PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
&Packet{PacketNumber: 4, Plaintext: []byte{0x54, 0x32}, EntropyBit: false}, &Packet{PacketNumber: 4, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
&Packet{PacketNumber: 5, Plaintext: []byte{0x54, 0x32}, EntropyBit: false}, &Packet{PacketNumber: 5, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
&Packet{PacketNumber: 6, Plaintext: []byte{0x54, 0x32}, EntropyBit: false}, &Packet{PacketNumber: 6, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
} }
for _, packet := range packets { for _, packet := range packets {
handler.SentPacket(packet) handler.SentPacket(packet)
@ -285,12 +290,12 @@ var _ = Describe("SentPacketHandler", func() {
retransmissionThreshold = 1 retransmissionThreshold = 1
packets = []*Packet{ packets = []*Packet{
&Packet{PacketNumber: 1, Plaintext: []byte{0x13, 0x37}, EntropyBit: false}, &Packet{PacketNumber: 1, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
&Packet{PacketNumber: 2, Plaintext: []byte{0xBE, 0xEF}, EntropyBit: false}, &Packet{PacketNumber: 2, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
&Packet{PacketNumber: 3, Plaintext: []byte{0xCA, 0xFE}, EntropyBit: false}, &Packet{PacketNumber: 3, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
&Packet{PacketNumber: 4, Plaintext: []byte{0x54, 0x32}, EntropyBit: false}, &Packet{PacketNumber: 4, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
&Packet{PacketNumber: 5, Plaintext: []byte{0x12, 0x42}, EntropyBit: false}, &Packet{PacketNumber: 5, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
&Packet{PacketNumber: 6, Plaintext: []byte{0xCA, 0xFE}, EntropyBit: false}, &Packet{PacketNumber: 6, Frames: []frames.Frame{&streamFrame}, EntropyBit: false},
} }
for _, packet := range packets { for _, packet := range packets {
handler.SentPacket(packet) handler.SentPacket(packet)

View file

@ -15,7 +15,7 @@ type packedPacket struct {
number protocol.PacketNumber number protocol.PacketNumber
entropyBit bool entropyBit bool
raw []byte raw []byte
payload []byte frames []frames.Frame
} }
type packetPacker struct { type packetPacker struct {
@ -47,12 +47,12 @@ func (p *packetPacker) PackPacket() (*packedPacket, error) {
1, 1,
)) ))
frames, err := p.composeNextPacket() payloadFrames, err := p.composeNextPacket()
if err != nil { if err != nil {
return nil, err return nil, err
} }
payload, err := p.getPayload(frames, currentPacketNumber) payload, err := p.getPayload(payloadFrames, currentPacketNumber)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -85,7 +85,7 @@ func (p *packetPacker) PackPacket() (*packedPacket, error) {
number: currentPacketNumber, number: currentPacketNumber,
entropyBit: entropyBit, entropyBit: entropyBit,
raw: raw.Bytes(), raw: raw.Bytes(),
payload: payload[1:], frames: payloadFrames,
}, nil }, nil
} }

View file

@ -34,7 +34,7 @@ var _ = Describe("Packet packer", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
b := &bytes.Buffer{} b := &bytes.Buffer{}
f.Write(b, 1, 6) f.Write(b, 1, 6)
Expect(p.payload).To(Equal(b.Bytes())) Expect(len(p.frames)).To(Equal(1))
Expect(p.raw).To(ContainSubstring(string(b.Bytes()))) Expect(p.raw).To(ContainSubstring(string(b.Bytes())))
}) })
@ -49,7 +49,7 @@ var _ = Describe("Packet packer", func() {
b := &bytes.Buffer{} b := &bytes.Buffer{}
f1.Write(b, 2, 6) f1.Write(b, 2, 6)
f2.Write(b, 2, 6) f2.Write(b, 2, 6)
Expect(p.payload).To(Equal(b.Bytes())) Expect(len(p.frames)).To(Equal(2))
Expect(p.raw).To(ContainSubstring(string(b.Bytes()))) Expect(p.raw).To(ContainSubstring(string(b.Bytes())))
}) })

View file

@ -233,7 +233,7 @@ func (s *Session) sendPacket() error {
} }
s.sentPacketHandler.SentPacket(&ackhandler.Packet{ s.sentPacketHandler.SentPacket(&ackhandler.Packet{
PacketNumber: packet.number, PacketNumber: packet.number,
Plaintext: packet.payload, Frames: packet.frames,
EntropyBit: packet.entropyBit, EntropyBit: packet.entropyBit,
}) })
fmt.Printf("-> Sending packet %d (%d bytes)\n", packet.number, len(packet.raw)) fmt.Printf("-> Sending packet %d (%d bytes)\n", packet.number, len(packet.raw))