mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-03 20:27:35 +03:00
simplify content storage in packed packets
It's not necessary to store both the packetBuffer and the slice containing the raw data in the packet.
This commit is contained in:
parent
077504f557
commit
d642bf9098
10 changed files with 112 additions and 103 deletions
|
@ -7,9 +7,9 @@ import (
|
|||
)
|
||||
|
||||
type packetBuffer struct {
|
||||
Slice []byte
|
||||
Data []byte
|
||||
|
||||
// refCount counts how many packets the Slice is used in.
|
||||
// refCount counts how many packets Data is used in.
|
||||
// It doesn't support concurrent use.
|
||||
// It is > 1 when used for coalesced packet.
|
||||
refCount int
|
||||
|
@ -50,8 +50,13 @@ func (b *packetBuffer) Release() {
|
|||
b.putBack()
|
||||
}
|
||||
|
||||
// Len returns the length of Data
|
||||
func (b *packetBuffer) Len() protocol.ByteCount {
|
||||
return protocol.ByteCount(len(b.Data))
|
||||
}
|
||||
|
||||
func (b *packetBuffer) putBack() {
|
||||
if cap(b.Slice) != int(protocol.MaxReceivePacketSize) {
|
||||
if cap(b.Data) != int(protocol.MaxReceivePacketSize) {
|
||||
panic("putPacketBuffer called with packet of wrong size!")
|
||||
}
|
||||
bufferPool.Put(b)
|
||||
|
@ -62,14 +67,14 @@ var bufferPool sync.Pool
|
|||
func getPacketBuffer() *packetBuffer {
|
||||
buf := bufferPool.Get().(*packetBuffer)
|
||||
buf.refCount = 1
|
||||
buf.Slice = buf.Slice[:protocol.MaxReceivePacketSize]
|
||||
buf.Data = buf.Data[:0]
|
||||
return buf
|
||||
}
|
||||
|
||||
func init() {
|
||||
bufferPool.New = func() interface{} {
|
||||
return &packetBuffer{
|
||||
Slice: make([]byte, 0, protocol.MaxReceivePacketSize),
|
||||
Data: make([]byte, 0, protocol.MaxReceivePacketSize),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
var _ = Describe("Buffer Pool", func() {
|
||||
It("returns buffers of cap", func() {
|
||||
buf := getPacketBuffer()
|
||||
Expect(buf.Slice).To(HaveCap(int(protocol.MaxReceivePacketSize)))
|
||||
Expect(buf.Data).To(HaveCap(int(protocol.MaxReceivePacketSize)))
|
||||
})
|
||||
|
||||
It("releases buffers", func() {
|
||||
|
@ -18,9 +18,15 @@ var _ = Describe("Buffer Pool", func() {
|
|||
buf.Release()
|
||||
})
|
||||
|
||||
It("gets the length", func() {
|
||||
buf := getPacketBuffer()
|
||||
buf.Data = append(buf.Data, []byte("foobar")...)
|
||||
Expect(buf.Len()).To(BeEquivalentTo(6))
|
||||
})
|
||||
|
||||
It("panics if wrong-sized buffers are passed", func() {
|
||||
buf := getPacketBuffer()
|
||||
buf.Slice = make([]byte, 10)
|
||||
buf.Data = make([]byte, 10)
|
||||
Expect(func() { buf.Release() }).To(Panic())
|
||||
})
|
||||
|
||||
|
|
|
@ -218,7 +218,7 @@ func (h *packetHandlerMap) listen() {
|
|||
defer close(h.listening)
|
||||
for {
|
||||
buffer := getPacketBuffer()
|
||||
data := buffer.Slice
|
||||
data := buffer.Data[:protocol.MaxReceivePacketSize]
|
||||
// The packet size should not exceed protocol.MaxReceivePacketSize bytes
|
||||
// If it does, we only read a truncated packet, which will then end up undecryptable
|
||||
n, addr, err := h.conn.ReadFrom(data)
|
||||
|
|
|
@ -37,7 +37,6 @@ type payload struct {
|
|||
|
||||
type packedPacket struct {
|
||||
header *wire.ExtendedHeader
|
||||
raw []byte
|
||||
ack *wire.AckFrame
|
||||
frames []ackhandler.Frame
|
||||
|
||||
|
@ -87,7 +86,7 @@ func (p *packedPacket) ToAckHandlerPacket(now time.Time, q *retransmissionQueue)
|
|||
PacketNumber: p.header.PacketNumber,
|
||||
LargestAcked: largestAcked,
|
||||
Frames: p.frames,
|
||||
Length: protocol.ByteCount(len(p.raw)),
|
||||
Length: p.buffer.Len(),
|
||||
EncryptionLevel: encLevel,
|
||||
SendTime: now,
|
||||
}
|
||||
|
@ -536,28 +535,25 @@ func (p *packetPacker) writeSinglePacket(
|
|||
encLevel protocol.EncryptionLevel,
|
||||
sealer sealer,
|
||||
) (*packedPacket, error) {
|
||||
packetBuffer := getPacketBuffer()
|
||||
|
||||
n, err := p.appendPacket(packetBuffer.Slice[:0], header, payload, encLevel, sealer)
|
||||
if err != nil {
|
||||
buffer := getPacketBuffer()
|
||||
if err := p.appendPacket(buffer, header, payload, encLevel, sealer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &packedPacket{
|
||||
buffer: buffer,
|
||||
header: header,
|
||||
raw: packetBuffer.Slice[:n],
|
||||
ack: payload.ack,
|
||||
frames: payload.frames,
|
||||
buffer: packetBuffer,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *packetPacker) appendPacket(
|
||||
raw []byte,
|
||||
buffer *packetBuffer,
|
||||
header *wire.ExtendedHeader,
|
||||
payload payload,
|
||||
encLevel protocol.EncryptionLevel,
|
||||
sealer sealer,
|
||||
) (int, error) {
|
||||
) error {
|
||||
var paddingLen protocol.ByteCount
|
||||
pnLen := protocol.ByteCount(header.PacketNumberLen)
|
||||
if encLevel != protocol.Encryption1RTT {
|
||||
|
@ -572,46 +568,49 @@ func (p *packetPacker) appendPacket(
|
|||
paddingLen = 4 - pnLen - payload.length
|
||||
}
|
||||
|
||||
hdrOffset := len(raw)
|
||||
buffer := bytes.NewBuffer(raw)
|
||||
if err := header.Write(buffer, p.version); err != nil {
|
||||
return 0, err
|
||||
hdrOffset := buffer.Len()
|
||||
buf := bytes.NewBuffer(buffer.Data)
|
||||
if err := header.Write(buf, p.version); err != nil {
|
||||
return err
|
||||
}
|
||||
payloadOffset := buffer.Len()
|
||||
payloadOffset := buf.Len()
|
||||
|
||||
if payload.ack != nil {
|
||||
if err := payload.ack.Write(buffer, p.version); err != nil {
|
||||
return 0, err
|
||||
if err := payload.ack.Write(buf, p.version); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if paddingLen > 0 {
|
||||
buffer.Write(bytes.Repeat([]byte{0}, int(paddingLen)))
|
||||
buf.Write(bytes.Repeat([]byte{0}, int(paddingLen)))
|
||||
}
|
||||
for _, frame := range payload.frames {
|
||||
if err := frame.Write(buffer, p.version); err != nil {
|
||||
return 0, err
|
||||
if err := frame.Write(buf, p.version); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if payloadSize := protocol.ByteCount(buffer.Len()-payloadOffset) - paddingLen; payloadSize != payload.length {
|
||||
return 0, fmt.Errorf("PacketPacker BUG: payload size inconsistent (expected %d, got %d bytes)", payload.length, payloadSize)
|
||||
if payloadSize := protocol.ByteCount(buf.Len()-payloadOffset) - paddingLen; payloadSize != payload.length {
|
||||
return fmt.Errorf("PacketPacker BUG: payload size inconsistent (expected %d, got %d bytes)", payload.length, payloadSize)
|
||||
}
|
||||
if size := protocol.ByteCount(buffer.Len() + sealer.Overhead()); size > p.maxPacketSize {
|
||||
return 0, fmt.Errorf("PacketPacker BUG: packet too large (%d bytes, allowed %d bytes)", size, p.maxPacketSize)
|
||||
if size := protocol.ByteCount(buf.Len() + sealer.Overhead()); size > p.maxPacketSize {
|
||||
return fmt.Errorf("PacketPacker BUG: packet too large (%d bytes, allowed %d bytes)", size, p.maxPacketSize)
|
||||
}
|
||||
|
||||
raw = raw[:buffer.Len()]
|
||||
raw := buffer.Data
|
||||
// encrypt the packet
|
||||
raw = raw[:buf.Len()]
|
||||
_ = sealer.Seal(raw[payloadOffset:payloadOffset], raw[payloadOffset:], header.PacketNumber, raw[hdrOffset:payloadOffset])
|
||||
raw = raw[0 : buffer.Len()+sealer.Overhead()]
|
||||
|
||||
raw = raw[0 : buf.Len()+sealer.Overhead()]
|
||||
// apply header protection
|
||||
pnOffset := payloadOffset - int(header.PacketNumberLen)
|
||||
sealer.EncryptHeader(raw[pnOffset+4:pnOffset+4+16], &raw[0], raw[pnOffset:payloadOffset])
|
||||
buffer.Data = raw
|
||||
|
||||
num := p.pnManager.PopPacketNumber(encLevel)
|
||||
if num != header.PacketNumber {
|
||||
return 0, errors.New("packetPacker BUG: Peeked and Popped packet numbers do not match")
|
||||
return errors.New("packetPacker BUG: Peeked and Popped packet numbers do not match")
|
||||
}
|
||||
return len(raw) - hdrOffset, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *packetPacker) SetToken(token []byte) {
|
||||
|
|
|
@ -183,8 +183,8 @@ var _ = Describe("Packet packer", func() {
|
|||
hdrRawEncrypted[0] ^= 0xff
|
||||
hdrRawEncrypted[len(hdrRaw)-2] ^= 0xff
|
||||
hdrRawEncrypted[len(hdrRaw)-1] ^= 0xff
|
||||
Expect(p.raw[0:len(hdrRaw)]).To(Equal(hdrRawEncrypted))
|
||||
Expect(p.raw[len(p.raw)-4:]).To(Equal([]byte{0xde, 0xca, 0xfb, 0xad}))
|
||||
Expect(p.buffer.Data[0:len(hdrRaw)]).To(Equal(hdrRawEncrypted))
|
||||
Expect(p.buffer.Data[p.buffer.Len()-4:]).To(Equal([]byte{0xde, 0xca, 0xfb, 0xad}))
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -309,7 +309,7 @@ var _ = Describe("Packet packer", func() {
|
|||
b := &bytes.Buffer{}
|
||||
f.Write(b, packer.version)
|
||||
Expect(p.frames).To(Equal([]ackhandler.Frame{{Frame: f}}))
|
||||
Expect(p.raw).To(ContainSubstring(b.String()))
|
||||
Expect(p.buffer.Data).To(ContainSubstring(b.String()))
|
||||
})
|
||||
|
||||
It("stores the encryption level a packet was sealed with", func() {
|
||||
|
@ -371,7 +371,7 @@ var _ = Describe("Packet packer", func() {
|
|||
Expect(p).ToNot(BeNil())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(p.frames).To(Equal(frames))
|
||||
Expect(p.raw).NotTo(BeEmpty())
|
||||
Expect(p.buffer.Len()).ToNot(BeZero())
|
||||
})
|
||||
|
||||
It("accounts for the space consumed by control frames", func() {
|
||||
|
@ -408,10 +408,10 @@ var _ = Describe("Packet packer", func() {
|
|||
packet, err := packer.PackAppDataPacket()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
// cut off the tag that the mock sealer added
|
||||
packet.raw = packet.raw[:len(packet.raw)-sealer.Overhead()]
|
||||
hdr, _, _, err := wire.ParsePacket(packet.raw, len(packer.getDestConnID()))
|
||||
packet.buffer.Data = packet.buffer.Data[:packet.buffer.Len()-protocol.ByteCount(sealer.Overhead())]
|
||||
hdr, _, _, err := wire.ParsePacket(packet.buffer.Data, len(packer.getDestConnID()))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
r := bytes.NewReader(packet.raw)
|
||||
r := bytes.NewReader(packet.buffer.Data)
|
||||
extHdr, err := hdr.ParseExtended(r, packer.version)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(extHdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen1))
|
||||
|
@ -613,7 +613,7 @@ var _ = Describe("Packet packer", func() {
|
|||
sealingManager.EXPECT().GetInitialSealer().Return(sealer, nil)
|
||||
p, err := packer.PackPacket()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
checkLength(p.raw)
|
||||
checkLength(p.buffer.Data)
|
||||
})
|
||||
|
||||
It("packs a maximum size Handshake packet", func() {
|
||||
|
@ -635,9 +635,9 @@ var _ = Describe("Packet packer", func() {
|
|||
p, err := packer.PackPacket()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(p.frames).To(HaveLen(1))
|
||||
Expect(p.raw).To(HaveLen(int(packer.maxPacketSize)))
|
||||
Expect(p.buffer.Len()).To(BeEquivalentTo(packer.maxPacketSize))
|
||||
Expect(p.header.IsLongHeader).To(BeTrue())
|
||||
checkLength(p.raw)
|
||||
checkLength(p.buffer.Data)
|
||||
})
|
||||
|
||||
It("adds retransmissions", func() {
|
||||
|
@ -654,7 +654,7 @@ var _ = Describe("Packet packer", func() {
|
|||
Expect(p.EncryptionLevel()).To(Equal(protocol.EncryptionInitial))
|
||||
Expect(p.frames).To(Equal([]ackhandler.Frame{{Frame: f}}))
|
||||
Expect(p.header.IsLongHeader).To(BeTrue())
|
||||
checkLength(p.raw)
|
||||
checkLength(p.buffer.Data)
|
||||
})
|
||||
|
||||
It("sends an Initial packet containing only an ACK", func() {
|
||||
|
@ -709,11 +709,11 @@ var _ = Describe("Packet packer", func() {
|
|||
packet, err := packer.PackPacket()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.header.Token).To(Equal(token))
|
||||
Expect(packet.raw).To(HaveLen(protocol.MinInitialPacketSize))
|
||||
Expect(packet.buffer.Len()).To(BeEquivalentTo(protocol.MinInitialPacketSize))
|
||||
Expect(packet.frames).To(HaveLen(1))
|
||||
cf := packet.frames[0].Frame.(*wire.CryptoFrame)
|
||||
Expect(cf.Data).To(Equal([]byte("foobar")))
|
||||
checkLength(packet.raw)
|
||||
checkLength(packet.buffer.Data)
|
||||
})
|
||||
|
||||
It("adds an ACK frame", func() {
|
||||
|
@ -729,7 +729,7 @@ var _ = Describe("Packet packer", func() {
|
|||
packer.perspective = protocol.PerspectiveClient
|
||||
packet, err := packer.PackPacket()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(packet.raw).To(HaveLen(protocol.MinInitialPacketSize))
|
||||
Expect(packet.buffer.Len()).To(BeEquivalentTo(protocol.MinInitialPacketSize))
|
||||
Expect(packet.ack).To(Equal(ack))
|
||||
Expect(packet.frames).To(HaveLen(1))
|
||||
})
|
||||
|
@ -751,7 +751,7 @@ var _ = Describe("Packet packer", func() {
|
|||
Expect(packet.EncryptionLevel()).To(Equal(protocol.EncryptionInitial))
|
||||
Expect(packet.frames).To(HaveLen(1))
|
||||
Expect(packet.frames[0].Frame).To(Equal(f))
|
||||
checkLength(packet.raw)
|
||||
checkLength(packet.buffer.Data)
|
||||
})
|
||||
|
||||
It("packs a Handshake probe packet", func() {
|
||||
|
@ -769,7 +769,7 @@ var _ = Describe("Packet packer", func() {
|
|||
Expect(packet.EncryptionLevel()).To(Equal(protocol.EncryptionHandshake))
|
||||
Expect(packet.frames).To(HaveLen(1))
|
||||
Expect(packet.frames[0].Frame).To(Equal(f))
|
||||
checkLength(packet.raw)
|
||||
checkLength(packet.buffer.Data)
|
||||
})
|
||||
|
||||
It("packs a 1-RTT probe packet", func() {
|
||||
|
@ -795,11 +795,13 @@ var _ = Describe("Packet packer", func() {
|
|||
|
||||
var _ = Describe("Converting to AckHandler packets", func() {
|
||||
It("convert a packet", func() {
|
||||
buffer := getPacketBuffer()
|
||||
buffer.Data = append(buffer.Data, []byte("foobar")...)
|
||||
packet := &packedPacket{
|
||||
buffer: buffer,
|
||||
header: &wire.ExtendedHeader{Header: wire.Header{}},
|
||||
frames: []ackhandler.Frame{{Frame: &wire.MaxDataFrame{}}, {Frame: &wire.PingFrame{}}},
|
||||
ack: &wire.AckFrame{AckRanges: []wire.AckRange{{Largest: 100, Smallest: 80}}},
|
||||
raw: []byte("foobar"),
|
||||
}
|
||||
t := time.Now()
|
||||
p := packet.ToAckHandlerPacket(t, nil)
|
||||
|
@ -811,9 +813,9 @@ var _ = Describe("Converting to AckHandler packets", func() {
|
|||
|
||||
It("sets the LargestAcked to invalid, if the packet doesn't have an ACK frame", func() {
|
||||
packet := &packedPacket{
|
||||
buffer: getPacketBuffer(),
|
||||
header: &wire.ExtendedHeader{Header: wire.Header{}},
|
||||
frames: []ackhandler.Frame{{Frame: &wire.MaxDataFrame{}}, {Frame: &wire.PingFrame{}}},
|
||||
raw: []byte("foobar"),
|
||||
}
|
||||
p := packet.ToAckHandlerPacket(time.Now(), nil)
|
||||
Expect(p.LargestAcked).To(Equal(protocol.InvalidPacketNumber))
|
||||
|
@ -822,12 +824,12 @@ var _ = Describe("Converting to AckHandler packets", func() {
|
|||
It("doesn't overwrite the OnLost callback, if it is set", func() {
|
||||
var pingLost bool
|
||||
packet := &packedPacket{
|
||||
buffer: getPacketBuffer(),
|
||||
header: &wire.ExtendedHeader{Header: wire.Header{Type: protocol.PacketTypeHandshake}},
|
||||
frames: []ackhandler.Frame{
|
||||
{Frame: &wire.MaxDataFrame{}},
|
||||
{Frame: &wire.PingFrame{}, OnLost: func(wire.Frame) { pingLost = true }},
|
||||
},
|
||||
raw: []byte("foobar"),
|
||||
}
|
||||
p := packet.ToAckHandlerPacket(time.Now(), newRetransmissionQueue(protocol.VersionTLS))
|
||||
Expect(p.Frames).To(HaveLen(2))
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package quic
|
||||
|
||||
type sendQueue struct {
|
||||
queue chan *packedPacket
|
||||
queue chan *packetBuffer
|
||||
closeCalled chan struct{} // runStopped when Close() is called
|
||||
runStopped chan struct{} // runStopped when the run loop returns
|
||||
conn connection
|
||||
|
@ -12,12 +12,12 @@ func newSendQueue(conn connection) *sendQueue {
|
|||
conn: conn,
|
||||
runStopped: make(chan struct{}),
|
||||
closeCalled: make(chan struct{}),
|
||||
queue: make(chan *packedPacket, 1),
|
||||
queue: make(chan *packetBuffer, 1),
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (h *sendQueue) Send(p *packedPacket) {
|
||||
func (h *sendQueue) Send(p *packetBuffer) {
|
||||
h.queue <- p
|
||||
}
|
||||
|
||||
|
@ -34,10 +34,10 @@ func (h *sendQueue) Run() error {
|
|||
// make sure that all queued packets are actually sent out
|
||||
shouldClose = true
|
||||
case p := <-h.queue:
|
||||
if err := h.conn.Write(p.raw); err != nil {
|
||||
if err := h.conn.Write(p.Data); err != nil {
|
||||
return err
|
||||
}
|
||||
p.buffer.Release()
|
||||
p.Release()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,14 +15,11 @@ var _ = Describe("Send Queue", func() {
|
|||
q = newSendQueue(c)
|
||||
})
|
||||
|
||||
getPacket := func(b []byte) *packedPacket {
|
||||
getPacket := func(b []byte) *packetBuffer {
|
||||
buf := getPacketBuffer()
|
||||
buf.Slice = buf.Slice[:len(b)]
|
||||
copy(buf.Slice, b)
|
||||
return &packedPacket{
|
||||
buffer: buf,
|
||||
raw: buf.Slice,
|
||||
}
|
||||
buf.Data = buf.Data[:len(b)]
|
||||
copy(buf.Data, b)
|
||||
return buf
|
||||
}
|
||||
|
||||
It("sends a packet", func() {
|
||||
|
@ -30,7 +27,7 @@ var _ = Describe("Send Queue", func() {
|
|||
q.Send(p)
|
||||
|
||||
written := make(chan struct{})
|
||||
c.EXPECT().Write(p.raw).Do(func([]byte) { close(written) })
|
||||
c.EXPECT().Write([]byte("foobar")).Do(func([]byte) { close(written) })
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer GinkgoRecover()
|
||||
|
|
|
@ -494,7 +494,7 @@ func (s *baseServer) sendServerBusy(remoteAddr net.Addr, hdr *wire.Header) error
|
|||
sealer, _ := handshake.NewInitialAEAD(hdr.DestConnectionID, protocol.PerspectiveServer)
|
||||
packetBuffer := getPacketBuffer()
|
||||
defer packetBuffer.Release()
|
||||
buf := bytes.NewBuffer(packetBuffer.Slice[:0])
|
||||
buf := bytes.NewBuffer(packetBuffer.Data)
|
||||
|
||||
ccf := &wire.ConnectionCloseFrame{ErrorCode: qerr.ServerBusy}
|
||||
|
||||
|
|
10
session.go
10
session.go
|
@ -1340,7 +1340,7 @@ func (s *session) sendPackedPacket(packet *packedPacket) {
|
|||
TransportState: s.sentPacketHandler.GetStats(),
|
||||
EncryptionLevel: packet.EncryptionLevel(),
|
||||
PacketNumber: packet.header.PacketNumber,
|
||||
PacketSize: protocol.ByteCount(len(packet.raw)),
|
||||
PacketSize: packet.buffer.Len(),
|
||||
Frames: frames,
|
||||
})
|
||||
}
|
||||
|
@ -1349,11 +1349,11 @@ func (s *session) sendPackedPacket(packet *packedPacket) {
|
|||
for _, f := range packet.frames {
|
||||
frames = append(frames, f.Frame)
|
||||
}
|
||||
s.qlogger.SentPacket(now, packet.header, protocol.ByteCount(len(packet.raw)), packet.ack, frames)
|
||||
s.qlogger.SentPacket(now, packet.header, packet.buffer.Len(), packet.ack, frames)
|
||||
}
|
||||
s.logPacket(packet)
|
||||
s.connIDManager.SentPacket()
|
||||
s.sendQueue.Send(packet)
|
||||
s.sendQueue.Send(packet.buffer)
|
||||
}
|
||||
|
||||
func (s *session) sendConnectionClose(quicErr *qerr.QuicError) ([]byte, error) {
|
||||
|
@ -1376,7 +1376,7 @@ func (s *session) sendConnectionClose(quicErr *qerr.QuicError) ([]byte, error) {
|
|||
return nil, err
|
||||
}
|
||||
s.logPacket(packet)
|
||||
return packet.raw, s.conn.Write(packet.raw)
|
||||
return packet.buffer.Data, s.conn.Write(packet.buffer.Data)
|
||||
}
|
||||
|
||||
func (s *session) logPacket(packet *packedPacket) {
|
||||
|
@ -1384,7 +1384,7 @@ func (s *session) logPacket(packet *packedPacket) {
|
|||
// We don't need to allocate the slices for calling the format functions
|
||||
return
|
||||
}
|
||||
s.logger.Debugf("-> Sending packet 0x%x (%d bytes) for connection %s, %s", packet.header.PacketNumber, len(packet.raw), s.logID, packet.EncryptionLevel())
|
||||
s.logger.Debugf("-> Sending packet 0x%x (%d bytes) for connection %s, %s", packet.header.PacketNumber, packet.buffer.Len(), s.logID, packet.EncryptionLevel())
|
||||
packet.header.Log(s.logger)
|
||||
if packet.ack != nil {
|
||||
wire.LogFrame(s.logger, packet.ack, true)
|
||||
|
|
|
@ -54,10 +54,8 @@ var _ = Describe("Session", func() {
|
|||
|
||||
getPacket := func(pn protocol.PacketNumber) *packedPacket {
|
||||
buffer := getPacketBuffer()
|
||||
data := buffer.Slice[:0]
|
||||
data = append(data, []byte("foobar")...)
|
||||
buffer.Data = append(buffer.Data, []byte("foobar")...)
|
||||
return &packedPacket{
|
||||
raw: data,
|
||||
buffer: buffer,
|
||||
header: &wire.ExtendedHeader{PacketNumber: pn},
|
||||
}
|
||||
|
@ -417,12 +415,14 @@ var _ = Describe("Session", func() {
|
|||
streamManager.EXPECT().CloseWithError(qerr.ApplicationError(0, ""))
|
||||
expectReplaceWithClosed()
|
||||
cryptoSetup.EXPECT().Close()
|
||||
buffer := getPacketBuffer()
|
||||
buffer.Data = append(buffer.Data, []byte("connection close")...)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).DoAndReturn(func(f *wire.ConnectionCloseFrame) (*packedPacket, error) {
|
||||
Expect(f.IsApplicationError).To(BeTrue())
|
||||
Expect(f.ErrorCode).To(Equal(qerr.NoError))
|
||||
Expect(f.FrameType).To(BeZero())
|
||||
Expect(f.ReasonPhrase).To(BeEmpty())
|
||||
return &packedPacket{raw: []byte("connection close")}, nil
|
||||
return &packedPacket{buffer: buffer}, nil
|
||||
})
|
||||
mconn.EXPECT().Write([]byte("connection close"))
|
||||
sess.shutdown()
|
||||
|
@ -434,7 +434,7 @@ var _ = Describe("Session", func() {
|
|||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
expectReplaceWithClosed()
|
||||
cryptoSetup.EXPECT().Close()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
sess.shutdown()
|
||||
sess.shutdown()
|
||||
|
@ -450,7 +450,7 @@ var _ = Describe("Session", func() {
|
|||
Expect(f.IsApplicationError).To(BeTrue())
|
||||
Expect(f.ErrorCode).To(BeEquivalentTo(0x1337))
|
||||
Expect(f.ReasonPhrase).To(Equal("test error"))
|
||||
return &packedPacket{}, nil
|
||||
return &packedPacket{buffer: getPacketBuffer()}, nil
|
||||
})
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
sess.CloseWithError(0x1337, "test error")
|
||||
|
@ -468,7 +468,7 @@ var _ = Describe("Session", func() {
|
|||
Expect(f.FrameType).To(BeEquivalentTo(0x42))
|
||||
Expect(f.ErrorCode).To(BeEquivalentTo(0x1337))
|
||||
Expect(f.ReasonPhrase).To(Equal("test error"))
|
||||
return &packedPacket{}, nil
|
||||
return &packedPacket{buffer: getPacketBuffer()}, nil
|
||||
})
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
sess.closeLocal(testErr)
|
||||
|
@ -485,7 +485,7 @@ var _ = Describe("Session", func() {
|
|||
Expect(f.IsApplicationError).To(BeFalse())
|
||||
Expect(f.ErrorCode).To(BeEquivalentTo(0x15a))
|
||||
Expect(f.ReasonPhrase).To(BeEmpty())
|
||||
return &packedPacket{}, nil
|
||||
return &packedPacket{buffer: getPacketBuffer()}, nil
|
||||
})
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
sess.CloseWithError(0x1337, "test error")
|
||||
|
@ -518,7 +518,7 @@ var _ = Describe("Session", func() {
|
|||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
expectReplaceWithClosed()
|
||||
cryptoSetup.EXPECT().Close()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
returned := make(chan struct{})
|
||||
go func() {
|
||||
defer GinkgoRecover()
|
||||
|
@ -607,7 +607,7 @@ var _ = Describe("Session", func() {
|
|||
unpacker.EXPECT().Unpack(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, handshake.ErrDecryptionFailed)
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
cryptoSetup.EXPECT().Close()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
go func() {
|
||||
defer GinkgoRecover()
|
||||
cryptoSetup.EXPECT().RunHandshake().MaxTimes(1)
|
||||
|
@ -629,7 +629,7 @@ var _ = Describe("Session", func() {
|
|||
unpacker.EXPECT().Unpack(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, wire.ErrInvalidReservedBits)
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
cryptoSetup.EXPECT().Close()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer GinkgoRecover()
|
||||
|
@ -653,7 +653,7 @@ var _ = Describe("Session", func() {
|
|||
unpacker.EXPECT().Unpack(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, testErr)
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
cryptoSetup.EXPECT().Close()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
runErr := make(chan error)
|
||||
go func() {
|
||||
defer GinkgoRecover()
|
||||
|
@ -679,7 +679,7 @@ var _ = Describe("Session", func() {
|
|||
}, nil)
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
cryptoSetup.EXPECT().Close()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer GinkgoRecover()
|
||||
|
@ -875,7 +875,7 @@ var _ = Describe("Session", func() {
|
|||
|
||||
AfterEach(func() {
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
expectReplaceWithClosed()
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
|
@ -1002,7 +1002,7 @@ var _ = Describe("Session", func() {
|
|||
|
||||
AfterEach(func() {
|
||||
// make the go routine return
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
expectReplaceWithClosed()
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
|
@ -1117,7 +1117,7 @@ var _ = Describe("Session", func() {
|
|||
// make the go routine return
|
||||
expectReplaceWithClosed()
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
sess.shutdown()
|
||||
|
@ -1206,7 +1206,7 @@ var _ = Describe("Session", func() {
|
|||
// make sure the go routine returns
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
expectReplaceWithClosed()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
sess.shutdown()
|
||||
|
@ -1247,7 +1247,7 @@ var _ = Describe("Session", func() {
|
|||
// make sure the go routine returns
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
expectReplaceWithClosed()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
sess.shutdown()
|
||||
|
@ -1258,7 +1258,7 @@ var _ = Describe("Session", func() {
|
|||
packer.EXPECT().PackPacket().AnyTimes()
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
expectReplaceWithClosed()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
cryptoSetup.EXPECT().Close()
|
||||
go func() {
|
||||
defer GinkgoRecover()
|
||||
|
@ -1301,7 +1301,7 @@ var _ = Describe("Session", func() {
|
|||
// make sure the go routine returns
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
expectReplaceWithClosed()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
sess.shutdown()
|
||||
|
@ -1318,7 +1318,7 @@ var _ = Describe("Session", func() {
|
|||
}()
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
expectReplaceWithClosed()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
sess.shutdown()
|
||||
|
@ -1337,7 +1337,7 @@ var _ = Describe("Session", func() {
|
|||
}()
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
expectReplaceWithClosed()
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
Expect(sess.CloseWithError(0x1337, testErr.Error())).To(Succeed())
|
||||
|
@ -1374,7 +1374,7 @@ var _ = Describe("Session", func() {
|
|||
Expect(s).To(BeAssignableToTypeOf(&closedLocalSession{}))
|
||||
s.shutdown()
|
||||
}).Times(4) // initial connection ID + initial client dest conn ID + 2 newly issued conn IDs
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
sess.shutdown()
|
||||
|
@ -1406,7 +1406,7 @@ var _ = Describe("Session", func() {
|
|||
// make the go routine return
|
||||
expectReplaceWithClosed()
|
||||
streamManager.EXPECT().CloseWithError(gomock.Any())
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
sess.shutdown()
|
||||
|
@ -1506,7 +1506,7 @@ var _ = Describe("Session", func() {
|
|||
sess.lastPacketReceivedTime = time.Now().Add(-time.Minute)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).DoAndReturn(func(f *wire.ConnectionCloseFrame) (*packedPacket, error) {
|
||||
Expect(f.ErrorCode).To(Equal(qerr.NoError))
|
||||
return &packedPacket{}, nil
|
||||
return &packedPacket{buffer: getPacketBuffer()}, nil
|
||||
})
|
||||
// the handshake timeout is irrelevant here, since it depends on the time the session was created,
|
||||
// and not on the last network activity
|
||||
|
@ -1562,7 +1562,7 @@ var _ = Describe("Session", func() {
|
|||
}()
|
||||
Consistently(sess.Context().Done()).ShouldNot(BeClosed())
|
||||
// make the go routine return
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
expectReplaceWithClosed()
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
|
@ -1738,7 +1738,7 @@ var _ = Describe("Client Session", func() {
|
|||
PacketNumberLen: protocol.PacketNumberLen2,
|
||||
}, []byte{0}))).To(BeTrue())
|
||||
// make sure the go routine returns
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil)
|
||||
expectReplaceWithClosed()
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
|
@ -1863,7 +1863,7 @@ var _ = Describe("Client Session", func() {
|
|||
Expect(s).To(BeAssignableToTypeOf(&closedLocalSession{}))
|
||||
s.shutdown()
|
||||
})
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{}, nil).MaxTimes(1)
|
||||
packer.EXPECT().PackConnectionClose(gomock.Any()).Return(&packedPacket{buffer: getPacketBuffer()}, nil).MaxTimes(1)
|
||||
cryptoSetup.EXPECT().Close()
|
||||
mconn.EXPECT().Write(gomock.Any())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue