File size: 6,432 Bytes
b450976 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | import Foundation
final class MessageChunkReassembler {
private static let chunkTimeoutMs: TimeInterval = 30_000
private static let maxConcurrentSessions = 10
private var activeSessions: [String: ChunkSession] = [:]
private var activeBinarySessions: [UInt16: BinarySession] = [:]
func addBinaryFragment(msgId: UInt16, fragIdx: Int, fragCount: Int, data: Data) -> Data? {
cleanupTimedOutSessions()
guard fragCount > 0, fragIdx >= 0, fragIdx < fragCount else {
Bridge.log("MessageChunkReassembler: Dropping invalid binary fragment for msgId \(msgId)")
return nil
}
if let existing = activeBinarySessions[msgId], existing.fragCount != fragCount {
activeBinarySessions.removeValue(forKey: msgId)
}
if activeBinarySessions.count >= Self.maxConcurrentSessions,
activeBinarySessions[msgId] == nil
{
removeOldestBinarySession()
}
let isNewSession = activeBinarySessions[msgId] == nil
let session = activeBinarySessions[msgId] ?? BinarySession(msgId: msgId, fragCount: fragCount)
guard session.addFragment(index: fragIdx, data: data) else {
return nil
}
if isNewSession {
activeBinarySessions[msgId] = session
}
guard session.isComplete else {
return nil
}
let reassembled = session.reassemble()
activeBinarySessions.removeValue(forKey: msgId)
Bridge.log("MessageChunkReassembler: Reassembled \(reassembled.count) bytes from \(fragCount) binary fragments")
return reassembled
}
func addChunk(_ info: MessageChunker.ChunkInfo) -> String? {
cleanupTimedOutSessions()
guard !info.chunkId.isEmpty,
info.totalChunks > 0,
info.chunkIndex >= 0,
info.chunkIndex < info.totalChunks
else {
Bridge.log("MessageChunkReassembler: Dropping invalid chunk metadata for \(info.chunkId)")
return nil
}
if let existing = activeSessions[info.chunkId], existing.totalChunks != info.totalChunks {
Bridge.log(
"MessageChunkReassembler: totalChunks mismatch for \(info.chunkId) (expected \(existing.totalChunks), got \(info.totalChunks)); resetting session"
)
activeSessions.removeValue(forKey: info.chunkId)
}
if activeSessions.count >= Self.maxConcurrentSessions,
activeSessions[info.chunkId] == nil
{
removeOldestSession()
}
let isNewSession = activeSessions[info.chunkId] == nil
let session = activeSessions[info.chunkId] ?? ChunkSession(
chunkId: info.chunkId,
totalChunks: info.totalChunks
)
guard session.addChunk(index: info.chunkIndex, data: info.data) else {
Bridge.log("MessageChunkReassembler: Failed to add chunk \(info.chunkIndex) for \(info.chunkId)")
return nil
}
if isNewSession {
activeSessions[info.chunkId] = session
}
guard session.isComplete else {
return nil
}
let reassembled = session.reassemble()
activeSessions.removeValue(forKey: info.chunkId)
Bridge.log("MessageChunkReassembler: Reassembled \(reassembled.count) bytes from \(info.totalChunks) chunks")
return reassembled
}
func clear() {
activeSessions.removeAll()
activeBinarySessions.removeAll()
}
private func cleanupTimedOutSessions() {
let now = Date().timeIntervalSince1970 * 1000
activeSessions = activeSessions.filter { entry in
now - entry.value.createdAtMs <= Self.chunkTimeoutMs
}
activeBinarySessions = activeBinarySessions.filter { entry in
now - entry.value.createdAtMs <= Self.chunkTimeoutMs
}
}
private func removeOldestBinarySession() {
guard let oldest = activeBinarySessions.min(by: { $0.value.createdAtMs < $1.value.createdAtMs }) else {
return
}
activeBinarySessions.removeValue(forKey: oldest.key)
}
private func removeOldestSession() {
guard let oldest = activeSessions.min(by: { $0.value.createdAtMs < $1.value.createdAtMs }) else {
return
}
activeSessions.removeValue(forKey: oldest.key)
}
private final class ChunkSession {
let chunkId: String
let totalChunks: Int
let createdAtMs: TimeInterval
private var chunks: [Int: String] = [:]
init(chunkId: String, totalChunks: Int) {
self.chunkId = chunkId
self.totalChunks = totalChunks
self.createdAtMs = Date().timeIntervalSince1970 * 1000
}
func addChunk(index: Int, data: String) -> Bool {
guard index >= 0, index < totalChunks else {
return false
}
chunks[index] = data
return true
}
var isComplete: Bool {
chunks.count == totalChunks
}
func reassemble() -> String {
var result = ""
for index in 0 ..< totalChunks {
result += chunks[index] ?? ""
}
return result
}
}
private final class BinarySession {
let msgId: UInt16
let fragCount: Int
let createdAtMs: TimeInterval
private var fragments: [Int: Data] = [:]
init(msgId: UInt16, fragCount: Int) {
self.msgId = msgId
self.fragCount = fragCount
self.createdAtMs = Date().timeIntervalSince1970 * 1000
}
func addFragment(index: Int, data: Data) -> Bool {
guard index >= 0, index < fragCount else { return false }
fragments[index] = data
return true
}
var isComplete: Bool {
fragments.count == fragCount
}
func reassemble() -> Data {
var result = Data()
for index in 0 ..< fragCount {
result.append(fragments[index] ?? Data())
}
return result
}
}
}
|