Фикс error parsing: 1 frame = 1 packet и safe handshake fallback

This commit is contained in:
2026-03-26 02:53:21 +05:00
parent 03282eb478
commit 9202204094
2 changed files with 33 additions and 44 deletions

View File

@@ -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
} }
} }
} }

View File

@@ -535,18 +535,18 @@ 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)")
while (stream.getRemainingBits() >= MIN_PACKET_ID_BITS) { return
val packetStartBits = stream.getReadPointerBits() }
// Desktop/server parity: one WebSocket frame contains one packet.
val packetId = stream.readInt16() val packetId = stream.readInt16()
log("📥 Packet ID: $packetId") log("📥 Packet ID: $packetId")
val packetFactory = supportedPackets[packetId] val packetFactory = supportedPackets[packetId]
if (packetFactory == null) { if (packetFactory == null) {
log("⚠️ Unknown packet ID: $packetId, stopping frame parse") log("⚠️ Unknown packet ID: $packetId")
break return
} }
val packet = packetFactory() val packet = packetFactory()
@@ -555,7 +555,7 @@ class Protocol(
} catch (e: Exception) { } catch (e: Exception) {
log("❌ Error parsing packet $packetId: ${e.message}") log("❌ Error parsing packet $packetId: ${e.message}")
e.printStackTrace() e.printStackTrace()
break return
} }
// Notify waiters // Notify waiters
@@ -570,18 +570,6 @@ class Protocol(
e.printStackTrace() 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) {
log("📦 Parsed $parsedPackets packets from single WebSocket frame")
}
} catch (e: Exception) { } catch (e: Exception) {
log("❌ Error parsing packet: ${e.message}") log("❌ Error parsing packet: ${e.message}")
e.printStackTrace() e.printStackTrace()