Фикс error parsing: 1 frame = 1 packet и safe handshake fallback
This commit is contained in:
@@ -6,7 +6,8 @@ enum class HandshakeState(val value: Int) {
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun fromValue(value: Int): HandshakeState {
|
fun fromValue(value: Int): HandshakeState {
|
||||||
return entries.firstOrNull { it.value == value } ?: COMPLETED
|
// Fail-safe: unknown value must not auto-authenticate.
|
||||||
|
return entries.firstOrNull { it.value == value } ?: NEED_DEVICE_VERIFICATION
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -535,52 +535,40 @@ class Protocol(
|
|||||||
log("📥 Received ${data.size} bytes: $hexDump${if (data.size > 50) "..." else ""}")
|
log("📥 Received ${data.size} bytes: $hexDump${if (data.size > 50) "..." else ""}")
|
||||||
|
|
||||||
val stream = Stream(data)
|
val stream = Stream(data)
|
||||||
var parsedPackets = 0
|
if (stream.getRemainingBits() < MIN_PACKET_ID_BITS) {
|
||||||
|
log("⚠️ Frame too short to contain packet ID (${stream.getRemainingBits()} bits)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Desktop/server parity: one WebSocket frame contains one packet.
|
||||||
|
val packetId = stream.readInt16()
|
||||||
|
log("📥 Packet ID: $packetId")
|
||||||
|
|
||||||
while (stream.getRemainingBits() >= MIN_PACKET_ID_BITS) {
|
val packetFactory = supportedPackets[packetId]
|
||||||
val packetStartBits = stream.getReadPointerBits()
|
if (packetFactory == null) {
|
||||||
val packetId = stream.readInt16()
|
log("⚠️ Unknown packet ID: $packetId")
|
||||||
|
return
|
||||||
log("📥 Packet ID: $packetId")
|
|
||||||
|
|
||||||
val packetFactory = supportedPackets[packetId]
|
|
||||||
if (packetFactory == null) {
|
|
||||||
log("⚠️ Unknown packet ID: $packetId, stopping frame parse")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
val packet = packetFactory()
|
|
||||||
try {
|
|
||||||
packet.receive(stream)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
log("❌ Error parsing packet $packetId: ${e.message}")
|
|
||||||
e.printStackTrace()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// Notify waiters
|
|
||||||
val waitersCount = packetWaiters[packetId]?.size ?: 0
|
|
||||||
log("📥 Notifying $waitersCount waiter(s) for packet $packetId")
|
|
||||||
|
|
||||||
packetWaiters[packetId]?.forEach { callback ->
|
|
||||||
try {
|
|
||||||
callback(packet)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
log("❌ Error in packet handler: ${e.message}")
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
parsedPackets++
|
|
||||||
val consumedBits = stream.getReadPointerBits() - packetStartBits
|
|
||||||
if (consumedBits <= 0) {
|
|
||||||
log("⚠️ Packet parser made no progress for packet $packetId, stopping frame parse")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parsedPackets > 1) {
|
val packet = packetFactory()
|
||||||
log("📦 Parsed $parsedPackets packets from single WebSocket frame")
|
try {
|
||||||
|
packet.receive(stream)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
log("❌ Error parsing packet $packetId: ${e.message}")
|
||||||
|
e.printStackTrace()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify waiters
|
||||||
|
val waitersCount = packetWaiters[packetId]?.size ?: 0
|
||||||
|
log("📥 Notifying $waitersCount waiter(s) for packet $packetId")
|
||||||
|
|
||||||
|
packetWaiters[packetId]?.forEach { callback ->
|
||||||
|
try {
|
||||||
|
callback(packet)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
log("❌ Error in packet handler: ${e.message}")
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
log("❌ Error parsing packet: ${e.message}")
|
log("❌ Error parsing packet: ${e.message}")
|
||||||
|
|||||||
Reference in New Issue
Block a user