Skip to content

Instantly share code, notes, and snippets.

@shoghicp
Last active December 18, 2015 04:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoghicp/5729825 to your computer and use it in GitHub Desktop.
Save shoghicp/5729825 to your computer and use it in GitHub Desktop.

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 byte 0 start, 1 stop, 2 send, 3 ACK
Message ID short sent if Action >= 2
Message Index short starting from 0, sent if Action >= 2
Message Count short sent if Action == 2
Data Length short 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
  1. [C-->S] DEFLATEPacket (Action = 0)
  2. Wait for [S-->C] DEFLATEPacket (Action = 0)
  • If not received, the server does not support DEFLATEPacket, then stop
  1. [S-->C] DEFLATEPacket (Action = 0)
  2. Handshake completed

Stop

The server or the proxy/client can stop DEFLATEPacket support anytime.

  1. [C<-->S] DEFLATEPacket (Action = 1)
  2. After that, the party replies with DEFLATEPacket (Action = 1)
  3. 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 a packet with the same Message Index + ID is received, it is a duplicated packet. You can safely drop it.

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 (gzip 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

  • Length short: (if length == 0xFFFF, stop)
  • sent if length != 0xFFFF:
  • RAW Raknet Packet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment