DEFLATEPacket - randomblink/PocketMine-MP GitHub Wiki
DEFLATEPacket mixed protocol encapsulation for Minecraft: Pocket Edition
This protocol aims to use less bandwidth sending big packets / chunks of smaller packets compressed using DEFLATE. It works encapsulating raw packets into this protocol, without modifying them. It follows a mixed approach, letting normal packets work and handling compressed packet seamlessly. Both parties must support this protocol, so a Handshake system exists.
All numbers are Big-endian
Packet Structure
Packet ID | Field Name | Type | Notes |
---|---|---|---|
0x99 | Action | ubyte | 0 start, 1 stop, 2 send, 3 ACK |
Message ID | ushort | sent if Action >= 2 | |
Message Index | ushort | starting from 0, sent if Action >= 2 | |
Message Count | ushort | sent if Action == 2 | |
Data Length | ushort | sent if Action == 2 | |
Data | byte array | DEFLATE Data, sent if Action == 2 |
Handshake
The handshake is needed so servers and proxies are able to know if the other party supports the DEFLATEPacket. The handsake will be sent by the connecting party, the proxy or client. The packet can be sent anytime.
- C = client / proxy
- S = Server
[C-->S] DEFLATEPacket (Action = 0)
- Wait for
[S-->C] DEFLATEPacket (Action = 0)
- If not received, the server does not support DEFLATEPacket, then stop
[S-->C] DEFLATEPacket (Action = 0)
- Handshake completed
Stop
The server or the proxy/client can stop DEFLATEPacket support anytime.
[C<-->S] DEFLATEPacket (Action = 1)
- After that, the party replies with
DEFLATEPacket (Action = 1)
- DEFLATEPacket support finished. You can start again using the handshake anytime
Receiving/sending packets
Each data packet sent contains 3 additional fields, the Message ID, Message Count and Message Index. These fields are used to reorder the packet, notify receival and request lost packets.
Each time you receive a DEFLATEPacket with data (Action = 2), you've to reply with one that has Action = 3, the same Message ID and the same Message Index. If you don't receive that packet, the other party will resend it.
If you receive a packet that has been received, you can drop it. Keep in mind that the Message ID is a short, so it can rotate when the short space is filled.
Reordering packets
- After the specified number of packets have been received for a given Message ID, they have to bereordered and joined. The packet with the lower Message Index will be the first, then the next one...
- Then, you join all the data and INFLATE it (raw DEFLATE). Then, parse it following the DEFLATE Data structure, and send packets back to the party / handle them. You don't have to worry about lost packets, they will be handled seamlessly using the normal protocol.
DEFLATE Data structure
Max length 524280
- Length short: (if length == 0xFFFF, stop)
- sent if length != 0xFFFF:
- RAW Raknet Packet