mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 04:45:20 +00:00
# Conflicts: # bitchat/Localizable.xcstrings # bitchat/Services/BLE/BLEOutboundPacketPolicy.swift # bitchat/Services/BLE/BLEService.swift # bitchat/Sync/SyncTypeFlags.swift # localPackages/BitFoundation/Sources/BitFoundation/MessageType.swift
44 lines
1.5 KiB
Swift
44 lines
1.5 KiB
Swift
import BitFoundation
|
|
import Foundation
|
|
|
|
enum BLEOutboundPacketPolicy {
|
|
private static let fragmentFrameOverhead = 13 + 8 + 8 + 13
|
|
|
|
static func messageID(for packet: BitchatPacket) -> String {
|
|
BLEIngressLinkRegistry.messageID(for: packet)
|
|
}
|
|
|
|
static func padsBLEFrame(for packetType: UInt8) -> Bool {
|
|
switch MessageType(rawValue: packetType) {
|
|
case .noiseEncrypted, .noiseHandshake:
|
|
return true
|
|
case .none, .announce, .message, .leave, .requestSync, .fragment, .fileTransfer, .courierEnvelope, .prekeyBundle, .groupMessage, .nostrCarrier:
|
|
return false
|
|
}
|
|
}
|
|
|
|
static func priority(for packet: BitchatPacket, data: Data) -> BLEOutboundWritePriority {
|
|
guard let messageType = MessageType(rawValue: packet.type) else { return .low }
|
|
switch messageType {
|
|
case .fragment:
|
|
return .fragment(totalFragments: fragmentTotalCount(from: packet.payload))
|
|
case .fileTransfer:
|
|
return .fileTransfer
|
|
default:
|
|
return .high
|
|
}
|
|
}
|
|
|
|
static func fragmentChunkSize(forLinkLimit limit: Int) -> Int {
|
|
max(64, limit - fragmentFrameOverhead)
|
|
}
|
|
|
|
private static func fragmentTotalCount(from payload: Data) -> Int {
|
|
guard payload.count >= 12 else { return Int(UInt16.max) }
|
|
let totalHigh = Int(payload[10])
|
|
let totalLow = Int(payload[11])
|
|
let total = (totalHigh << 8) | totalLow
|
|
return max(total, 1)
|
|
}
|
|
}
|