From 931166bb8e0dee5183a3b43726cfe7f285529e9b Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sat, 3 Aug 2024 17:16:02 -0700 Subject: [PATCH] remove unneeded cryptoStream interface (#4617) --- connection.go | 6 +++--- crypto_stream.go | 30 +++++++++--------------------- crypto_stream_manager.go | 16 ++++++++-------- crypto_stream_manager_test.go | 6 +++--- crypto_stream_test.go | 6 ++---- packet_packer.go | 8 ++++---- packet_packer_test.go | 4 ++-- 7 files changed, 31 insertions(+), 45 deletions(-) diff --git a/connection.go b/connection.go index 371a93453..81d602c23 100644 --- a/connection.go +++ b/connection.go @@ -152,9 +152,9 @@ type connection struct { maxPayloadSizeEstimate atomic.Uint32 - initialStream cryptoStream - handshakeStream cryptoStream - oneRTTStream cryptoStream // only set for the server + initialStream *cryptoStream + handshakeStream *cryptoStream + oneRTTStream *cryptoStream // only set for the server cryptoStreamHandler cryptoStreamHandler receivedPackets chan receivedPacket diff --git a/crypto_stream.go b/crypto_stream.go index 55700eaa8..9a387baa4 100644 --- a/crypto_stream.go +++ b/crypto_stream.go @@ -2,25 +2,13 @@ package quic import ( "fmt" - "io" "github.com/quic-go/quic-go/internal/protocol" "github.com/quic-go/quic-go/internal/qerr" "github.com/quic-go/quic-go/internal/wire" ) -type cryptoStream interface { - // for receiving data - HandleCryptoFrame(*wire.CryptoFrame) error - GetCryptoData() []byte - Finish() error - // for sending data - io.Writer - HasData() bool - PopCryptoFrame(protocol.ByteCount) *wire.CryptoFrame -} - -type cryptoStreamImpl struct { +type cryptoStream struct { queue frameSorter highestOffset protocol.ByteCount @@ -30,11 +18,11 @@ type cryptoStreamImpl struct { writeBuf []byte } -func newCryptoStream() cryptoStream { - return &cryptoStreamImpl{queue: *newFrameSorter()} +func newCryptoStream() *cryptoStream { + return &cryptoStream{queue: *newFrameSorter()} } -func (s *cryptoStreamImpl) HandleCryptoFrame(f *wire.CryptoFrame) error { +func (s *cryptoStream) HandleCryptoFrame(f *wire.CryptoFrame) error { highestOffset := f.Offset + protocol.ByteCount(len(f.Data)) if maxOffset := highestOffset; maxOffset > protocol.MaxCryptoStreamOffset { return &qerr.TransportError{ @@ -59,12 +47,12 @@ func (s *cryptoStreamImpl) HandleCryptoFrame(f *wire.CryptoFrame) error { } // GetCryptoData retrieves data that was received in CRYPTO frames -func (s *cryptoStreamImpl) GetCryptoData() []byte { +func (s *cryptoStream) GetCryptoData() []byte { _, data, _ := s.queue.Pop() return data } -func (s *cryptoStreamImpl) Finish() error { +func (s *cryptoStream) Finish() error { if s.queue.HasMoreData() { return &qerr.TransportError{ ErrorCode: qerr.ProtocolViolation, @@ -76,16 +64,16 @@ func (s *cryptoStreamImpl) Finish() error { } // Writes writes data that should be sent out in CRYPTO frames -func (s *cryptoStreamImpl) Write(p []byte) (int, error) { +func (s *cryptoStream) Write(p []byte) (int, error) { s.writeBuf = append(s.writeBuf, p...) return len(p), nil } -func (s *cryptoStreamImpl) HasData() bool { +func (s *cryptoStream) HasData() bool { return len(s.writeBuf) > 0 } -func (s *cryptoStreamImpl) PopCryptoFrame(maxLen protocol.ByteCount) *wire.CryptoFrame { +func (s *cryptoStream) PopCryptoFrame(maxLen protocol.ByteCount) *wire.CryptoFrame { f := &wire.CryptoFrame{Offset: s.writeOffset} n := min(f.MaxDataLen(maxLen), protocol.ByteCount(len(s.writeBuf))) f.Data = s.writeBuf[:n] diff --git a/crypto_stream_manager.go b/crypto_stream_manager.go index b29eb4082..d70b9b007 100644 --- a/crypto_stream_manager.go +++ b/crypto_stream_manager.go @@ -8,15 +8,15 @@ import ( ) type cryptoStreamManager struct { - initialStream cryptoStream - handshakeStream cryptoStream - oneRTTStream cryptoStream + initialStream *cryptoStream + handshakeStream *cryptoStream + oneRTTStream *cryptoStream } func newCryptoStreamManager( - initialStream cryptoStream, - handshakeStream cryptoStream, - oneRTTStream cryptoStream, + initialStream *cryptoStream, + handshakeStream *cryptoStream, + oneRTTStream *cryptoStream, ) *cryptoStreamManager { return &cryptoStreamManager{ initialStream: initialStream, @@ -26,7 +26,7 @@ func newCryptoStreamManager( } func (m *cryptoStreamManager) HandleCryptoFrame(frame *wire.CryptoFrame, encLevel protocol.EncryptionLevel) error { - var str cryptoStream + var str *cryptoStream //nolint:exhaustive // CRYPTO frames cannot be sent in 0-RTT packets. switch encLevel { case protocol.EncryptionInitial: @@ -42,7 +42,7 @@ func (m *cryptoStreamManager) HandleCryptoFrame(frame *wire.CryptoFrame, encLeve } func (m *cryptoStreamManager) GetCryptoData(encLevel protocol.EncryptionLevel) []byte { - var str cryptoStream + var str *cryptoStream //nolint:exhaustive // CRYPTO frames cannot be sent in 0-RTT packets. switch encLevel { case protocol.EncryptionInitial: diff --git a/crypto_stream_manager_test.go b/crypto_stream_manager_test.go index 0fba7e8a0..1f09c13dd 100644 --- a/crypto_stream_manager_test.go +++ b/crypto_stream_manager_test.go @@ -12,9 +12,9 @@ var _ = Describe("Crypto Stream Manager", func() { var ( csm *cryptoStreamManager - initialStream cryptoStream - handshakeStream cryptoStream - oneRTTStream cryptoStream + initialStream *cryptoStream + handshakeStream *cryptoStream + oneRTTStream *cryptoStream ) BeforeEach(func() { diff --git a/crypto_stream_test.go b/crypto_stream_test.go index e5ea9186f..7a5eb9dfc 100644 --- a/crypto_stream_test.go +++ b/crypto_stream_test.go @@ -12,11 +12,9 @@ import ( ) var _ = Describe("Crypto Stream", func() { - var str cryptoStream + var str *cryptoStream - BeforeEach(func() { - str = newCryptoStream() - }) + BeforeEach(func() { str = newCryptoStream() }) Context("handling incoming data", func() { It("handles in-order CRYPTO frames", func() { diff --git a/packet_packer.go b/packet_packer.go index e707734fb..f505f16aa 100644 --- a/packet_packer.go +++ b/packet_packer.go @@ -121,8 +121,8 @@ type packetPacker struct { perspective protocol.Perspective cryptoSetup sealingManager - initialStream cryptoStream - handshakeStream cryptoStream + initialStream *cryptoStream + handshakeStream *cryptoStream token []byte @@ -141,7 +141,7 @@ var _ packer = &packetPacker{} func newPacketPacker( srcConnID protocol.ConnectionID, getDestConnID func() protocol.ConnectionID, - initialStream, handshakeStream cryptoStream, + initialStream, handshakeStream *cryptoStream, packetNumberManager packetNumberManager, retransmissionQueue *retransmissionQueue, cryptoSetup sealingManager, @@ -482,7 +482,7 @@ func (p *packetPacker) maybeGetCryptoPacket(maxPacketSize protocol.ByteCount, en return nil, payload{} } - var s cryptoStream + var s *cryptoStream var handler ackhandler.FrameHandler var hasRetransmission bool //nolint:exhaustive // Initial and Handshake are the only two encryption levels here. diff --git a/packet_packer_test.go b/packet_packer_test.go index 54c4b3b28..c676b2b47 100644 --- a/packet_packer_test.go +++ b/packet_packer_test.go @@ -31,8 +31,8 @@ var _ = Describe("Packet packer", func() { datagramQueue *datagramQueue framer *MockFrameSource ackFramer *MockAckFrameSource - initialStream cryptoStream - handshakeStream cryptoStream + initialStream *cryptoStream + handshakeStream *cryptoStream sealingManager *MockSealingManager pnManager *mockackhandler.MockSentPacketHandler )