This commit is contained in:
callebtc
2025-10-21 13:25:39 +02:00
parent 6fb45f730a
commit 27c4d363b4
@@ -114,29 +114,15 @@ class BluetoothPacketBroadcaster(
private val transferJobs = ConcurrentHashMap<String, Job>() private val transferJobs = ConcurrentHashMap<String, Job>()
// Replace simple actor FIFO with a priority-aware channel + processor // Replace simple actor FIFO with a priority-aware channel + processor
@Volatile private val broadcasterChannel = Channel<BroadcastRequest>(capacity = Channel.UNLIMITED)
private var broadcasterChannel = Channel<BroadcastRequest>(capacity = Channel.UNLIMITED)
private val sequenceCounter = AtomicLong(0) private val sequenceCounter = AtomicLong(0)
// Track processor state for recovery
@Volatile
private var processorJob: Job? = null
private val processorLock = Any()
init { init {
startPriorityProcessor() startPriorityProcessor()
} }
private fun startPriorityProcessor() { private fun startPriorityProcessor() {
synchronized(processorLock) { broadcasterScope.launch {
// Cancel existing processor if running
processorJob?.cancel()
// Create new channel for fresh start
broadcasterChannel = Channel(capacity = Channel.UNLIMITED)
processorJob = broadcasterScope.launch {
var processorException: Exception? = null
Log.d(TAG, "🎭 Priority packet broadcaster processor started") Log.d(TAG, "🎭 Priority packet broadcaster processor started")
// Min-heap by (primaryPriority, secondaryPriority, sequence) // Min-heap by (primaryPriority, secondaryPriority, sequence)
@@ -148,6 +134,7 @@ class BluetoothPacketBroadcaster(
} }
} }
var processorException: Exception? = null
try { try {
while (isActive) { while (isActive) {
// If priority queue is empty, suspend to receive at least one item // If priority queue is empty, suspend to receive at least one item
@@ -167,32 +154,20 @@ class BluetoothPacketBroadcaster(
try { try {
broadcastSinglePacketInternal(next.request.routed, next.request.gattServer, next.request.characteristic) broadcastSinglePacketInternal(next.request.routed, next.request.gattServer, next.request.characteristic)
} catch (e: Exception) { } catch (e: Exception) {
// Log the broadcast failure - this is likely a transient BLE error // BLE error during broadcast - log and save for channel closure
Log.e(TAG, "❌ Broadcast failed, will attempt recovery: ${e.message}", e) Log.e(TAG, "❌ Broadcast failed: ${e.message}", e)
processorException = e processorException = e
// Don't throw - instead break and trigger recovery throw e // Re-throw to exit loop and close channel
break
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "❌ Priority processor loop terminated due to exception: ${e.message}", e) Log.e(TAG, "❌ Priority processor loop terminated: ${e.message}", e)
processorException = e processorException = e
} finally { } finally {
// Close the current channel // CRITICAL: Close channel so producers fail fast instead of accumulating packets
// This triggers the fallback path in broadcastSinglePacket()
broadcasterChannel.close(processorException) broadcasterChannel.close(processorException)
Log.w(TAG, "🎭 Priority packet broadcaster processor terminated, attempting recovery...") Log.d(TAG, "🎭 Priority packet broadcaster processor terminated, channel closed")
// Schedule automatic recovery after a short delay
if (processorException != null && broadcasterScope.isActive) {
broadcasterScope.launch {
delay(1000) // Wait 1 second before recovery
Log.d(TAG, "🔄 Attempting to restart priority processor...")
startPriorityProcessor()
}
} else {
Log.d(TAG, "🎭 Priority packet broadcaster processor shut down (no recovery scheduled)")
}
}
} }
} }
} }