Skip to content

Commit

Permalink
Add more compact bundle protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
9il committed Apr 21, 2024
1 parent fde8ccd commit a15a52d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class FirmwareFeatures {
enum class ServerFeatureFlags {
/** Server can parse bundle packets: `PACKET_BUNDLE` = 100 (0x64). */
PROTOCOL_BUNDLE_SUPPORT,
PROTOCOL_BUNDLE_V2_SUPPORT,

// Add new flags here

Expand All @@ -51,6 +52,7 @@ enum class ServerFeatureFlags {
companion object {
val flagsEnabled: Set<ServerFeatureFlags> = setOf(
PROTOCOL_BUNDLE_SUPPORT,
PROTOCOL_BUNDLE_V2_SUPPORT,

// Add enabled flags here
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ class UDPProtocolParser {
buf.position(bundlePacketStart + bundlePacketLen)
}
return bundlePackets.toTypedArray()
} else if (packetId == PACKET_BUNDLE_V2) {
bundlePackets.clear()
while (buf.hasRemaining()) {
val bundlePacketLen = Math.min(buf.get().toUByte().toInt(), buf.remaining()) // 1 byte
if (bundlePacketLen == 0) continue

val bundlePacketStart = buf.position()
val bundleBuf = buf.slice()
bundleBuf.limit(bundlePacketLen)
val bundlePacketId = bundleBuf.get().toUByte().toInt() // 1 byte
val newPacket = getNewPacket(bundlePacketId)
newPacket?.let {
newPacket.readData(bundleBuf)
bundlePackets.add(newPacket)
}

buf.position(bundlePacketStart + bundlePacketLen)
}
return bundlePackets.toTypedArray()
}

val newPacket = getNewPacket(packetId)
Expand Down Expand Up @@ -128,6 +147,7 @@ class UDPProtocolParser {
const val PACKET_USER_ACTION = 21
const val PACKET_FEATURE_FLAGS = 22
const val PACKET_BUNDLE = 100
const val PACKET_BUNDLE_V2 = 101
const val PACKET_PROTOCOL_CHANGE = 200
private val HANDSHAKE_BUFFER = ByteArray(64)
private val bundlePackets = ArrayList<UDPPacket>(128)
Expand Down

0 comments on commit a15a52d

Please sign in to comment.