mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 01:45:20 +00:00
chore: centralize more BLE/Nostr delays; tighten NostrRelayManager logs to concise summaries; adopt config for location/geohash/relays
This commit is contained in:
@@ -289,7 +289,7 @@ class NostrRelayManager: ObservableObject {
|
||||
|
||||
// Only log non-gift-wrap events to reduce noise
|
||||
if event.kind != 1059 {
|
||||
SecureLogger.log("📥 Received Nostr event (kind: \(event.kind)) from relay: \(relayUrl)",
|
||||
SecureLogger.log("📥 Event kind=\(event.kind) id=\(event.id.prefix(16))… relay=\(relayUrl)",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
}
|
||||
|
||||
@@ -321,11 +321,11 @@ class NostrRelayManager: ObservableObject {
|
||||
let reason = array.count >= 4 ? (array[3] as? String ?? "no reason given") : "no reason given"
|
||||
if success {
|
||||
_ = Self.pendingGiftWrapIDs.remove(eventId)
|
||||
SecureLogger.log("✅ Event accepted id=\(eventId.prefix(16))... by relay: \(relayUrl)",
|
||||
SecureLogger.log("✅ Accepted id=\(eventId.prefix(16))… relay=\(relayUrl)",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
} else {
|
||||
let isGiftWrap = Self.pendingGiftWrapIDs.remove(eventId) != nil
|
||||
SecureLogger.log("📮 Event \(eventId.prefix(16))... rejected by relay: \(reason)",
|
||||
SecureLogger.log("📮 Rejected id=\(eventId.prefix(16))… reason=\(reason)",
|
||||
category: SecureLogger.session, level: isGiftWrap ? .warning : .error)
|
||||
}
|
||||
}
|
||||
@@ -353,7 +353,7 @@ class NostrRelayManager: ObservableObject {
|
||||
let data = try encoder.encode(req)
|
||||
let message = String(data: data, encoding: .utf8) ?? ""
|
||||
|
||||
SecureLogger.log("📤 Sending Nostr event (kind: \(event.kind)) to relay: \(relayUrl)",
|
||||
SecureLogger.log("📤 Send kind=\(event.kind) id=\(event.id.prefix(16))… relay=\(relayUrl)",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
|
||||
connection.send(.string(message)) { [weak self] error in
|
||||
|
||||
@@ -430,7 +430,7 @@ final class BLEService: NSObject {
|
||||
|
||||
// Send initial announce after services are ready
|
||||
// Use longer delay to avoid conflicts with other announces
|
||||
messageQueue.asyncAfter(deadline: .now() + 2.0) { [weak self] in
|
||||
messageQueue.asyncAfter(deadline: .now() + TransportConfig.bleInitialAnnounceDelaySeconds) { [weak self] in
|
||||
self?.sendAnnounce(forceSend: true)
|
||||
}
|
||||
}
|
||||
@@ -2077,7 +2077,7 @@ extension BLEService: CBCentralManagerDelegate {
|
||||
|
||||
// Set a timeout for the connection attempt (slightly longer for reliability)
|
||||
// Use BLE queue to mutate BLE-related state consistently
|
||||
bleQueue.asyncAfter(deadline: .now() + 8.0) { [weak self] in
|
||||
bleQueue.asyncAfter(deadline: .now() + TransportConfig.bleConnectTimeoutSeconds) { [weak self] in
|
||||
guard let self = self,
|
||||
let state = self.peripherals[peripheralID],
|
||||
state.isConnecting && !state.isConnected else { return }
|
||||
@@ -2149,7 +2149,7 @@ func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeriph
|
||||
if centralManager?.state == .poweredOn {
|
||||
// Stop and restart scanning to ensure we get fresh discovery events
|
||||
centralManager?.stopScan()
|
||||
bleQueue.asyncAfter(deadline: .now() + 0.1) { [weak self] in
|
||||
bleQueue.asyncAfter(deadline: .now() + TransportConfig.bleRestartScanDelaySeconds) { [weak self] in
|
||||
self?.startScanning()
|
||||
}
|
||||
}
|
||||
@@ -2347,7 +2347,7 @@ extension BLEService: CBPeripheralDelegate {
|
||||
SecureLogger.log("🔔 Subscribed to notifications from \(peripheral.name ?? "Unknown")", category: SecureLogger.session, level: .debug)
|
||||
|
||||
// Send announce after subscription is confirmed (force send for new connection)
|
||||
messageQueue.asyncAfter(deadline: .now() + 0.1) { [weak self] in
|
||||
messageQueue.asyncAfter(deadline: .now() + TransportConfig.blePostSubscribeAnnounceDelaySeconds) { [weak self] in
|
||||
self?.sendAnnounce(forceSend: true)
|
||||
}
|
||||
} else {
|
||||
@@ -2509,7 +2509,7 @@ extension BLEService: CBPeripheralManagerDelegate {
|
||||
SecureLogger.log("📥 Central subscribed: \(central.identifier.uuidString)", category: SecureLogger.session, level: .debug)
|
||||
subscribedCentrals.append(central)
|
||||
// Send announce to the newly subscribed central after a small delay to avoid overwhelming
|
||||
messageQueue.asyncAfter(deadline: .now() + 0.4) { [weak self] in
|
||||
messageQueue.asyncAfter(deadline: .now() + TransportConfig.blePostAnnounceDelaySeconds) { [weak self] in
|
||||
self?.sendAnnounce(forceSend: true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,4 +77,11 @@ enum TransportConfig {
|
||||
|
||||
// Geo relay directory
|
||||
static let geoRelayFetchIntervalSeconds: TimeInterval = 60 * 60 * 24
|
||||
|
||||
// BLE operational delays
|
||||
static let bleInitialAnnounceDelaySeconds: TimeInterval = 2.0
|
||||
static let bleConnectTimeoutSeconds: TimeInterval = 8.0
|
||||
static let bleRestartScanDelaySeconds: TimeInterval = 0.1
|
||||
static let blePostSubscribeAnnounceDelaySeconds: TimeInterval = 0.1
|
||||
static let blePostAnnounceDelaySeconds: TimeInterval = 0.4
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user