Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions Sources/SwiftNetwork/QUIC/SendItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3110,19 +3110,78 @@ struct TransmittedItems: ~Copyable {
var sentCrypto = NetworkUniqueArray<SentCrypto>()

// Minimal information about a send on a stream
struct SentStream: ~Copyable {
struct SentStream {
let flowID: MultiplexedFlowIdentifier
let streamID: QUICStreamID
let offset: UInt64
let length: UInt64
let isFinal: Bool

func matches(_ other: borrowing SentStream) -> Bool {
func matches(_ other: SentStream) -> Bool {
flowID == other.flowID && streamID == other.streamID && offset == other.offset && length == other.length
&& isFinal == other.isFinal
}
}
var sentStreams = NetworkUniqueArray<SentStream>()

/// The stream sends a packet carried.
///
/// A packet almost always carries a single stream, and every sent packet keeps its record until it is
/// acknowledged or lost, so one send is held inline and only a second one allocates storage.
struct SentStreams: ~Copyable {
private enum Storage: ~Copyable {
case empty
case one(SentStream)
case many(NetworkUniqueArray<SentStream>)
}

private var storage = Storage.empty

var isEmpty: Bool {
switch storage {
case .empty: return true
case .one: return false
case .many(let sends): return sends.isEmpty
}
}

var count: Int {
switch storage {
case .empty: return 0
case .one: return 1
case .many(let sends): return sends.count
}
}

subscript(index: Int) -> SentStream {
switch storage {
case .empty:
preconditionFailure("Index out of range")
case .one(let send):
precondition(index == 0, "Index out of range")
return send
case .many(let sends):
return sends[index]
}
}

mutating func append(_ sentStream: SentStream) {
var taken = Storage.empty
swap(&taken, &storage)
switch consume taken {
case .empty:
storage = .one(sentStream)
case .one(let first):
var sends = NetworkUniqueArray<SentStream>(minimumCapacity: 2)
sends.append(first)
sends.append(sentStream)
storage = .many(sends)
case .many(var sends):
sends.append(sentStream)
storage = .many(sends)
}
}
}
var sentStreams = SentStreams()

var maxStreamDataFlows = Deque<MultiplexedFlowIdentifier>()
var streamDataBlockedFlows = Deque<MultiplexedFlowIdentifier>()
Expand Down
2 changes: 1 addition & 1 deletion Tests/QUICTests/QUICLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class QUICLayoutTests: XCTestCase {

func testLayoutPacket() {
let packetSize = 156
let packetRecordSize = 193
let packetRecordSize = 209
XCTAssertEqual(packetSize, MemoryLayout<Packet>.size)
XCTAssertEqual(packetRecordSize, MemoryLayout<SentPacketRecord>.size)
}
Expand Down
Loading