Protocol Implementation
The Bedrock Edition protocol is implemented inpumpkin-protocol/src/bedrock/ with the following components:
- RakNet Protocol - Reliable UDP packet delivery system
- Packet Encoder - Encodes game packets for Bedrock clients
- Packet Decoder - Decodes game packets from Bedrock clients
- Frame Set - Groups packets into frames for transmission
- ACK/NACK - Acknowledgment system for reliable delivery
RakNet Constants
Defined inpumpkin-protocol/src/bedrock/mod.rs:9-23
Magic Bytes
RakNet uses a 16-byte magic sequence to identify RakNet packets. This magic must be present in connection packets.MTU (Maximum Transmission Unit)
The default MTU is 1400 bytes, accounting for UDP and IP headers to prevent fragmentation.Reliability System
RakNet implements multiple reliability levels for packet delivery.RakReliability Enum
Defined inpumpkin-protocol/src/bedrock/mod.rs:24-102
Reliability Features
- is_reliable() - Packet requires acknowledgment
- is_sequenced() - Newer packets replace older ones
- is_ordered() - Packets must arrive in order
- is_order_exclusive() - Only ordered, not sequenced
Reliability ID Mapping
Sub-Clients
Bedrock Edition supports multiple sub-clients for split-screen gameplay. Defined inpumpkin-protocol/src/bedrock/mod.rs:104-110
Network Encoder
TheUDPNetworkEncoder handles encoding game packets for Bedrock Edition clients.
Implementation
Defined inpumpkin-protocol/src/bedrock/packet_encoder.rs:81-176
Game Packet Structure
Game packets use a 14-bit header encoding:- Bits 0-9: Game Packet ID (10 bits, 0-1023)
- Bits 10-11: Sub-Client Sender ID (2 bits)
- Bits 12-13: Sub-Client Target ID (2 bits)
Write Game Packet
Defined inpumpkin-protocol/src/bedrock/packet_encoder.rs:114-166
Encoding Process
- Write game packet ID (0xFE)
- Write compression method (if compression enabled)
- Calculate 14-bit header:
- Calculate total packet length
- Write packet length as VarUInt
- Write 14-bit header as VarUInt
- Write payload
Network Decoder
TheUDPNetworkDecoder handles decoding game packets from Bedrock Edition clients.
Implementation
Defined inpumpkin-protocol/src/bedrock/packet_decoder.rs:81-171
Decoding Process
Defined inpumpkin-protocol/src/bedrock/packet_decoder.rs:124-170
- Read compression method (if compression enabled)
- Read packet length (VarUInt)
- Read 14-bit header (VarUInt)
- Extract packet ID from header:
- Read payload bytes
- Return RawPacket
VarUInt Codec
Bedrock Edition uses VarUInt (unsigned variable-length integers) for packet lengths and headers.Implementation
Defined inpumpkin-protocol/src/codec/var_uint.rs
Encoding Algorithm
Similar to VarInt but uses unsigned values:- While value > 0x7F:
- Write (value & 0x7F) | 0x80
- Right shift value by 7 bits
- Write final byte (value & 0x7F)
Packet Components
ACK Packets
Acknowledgment packets confirm receipt of reliable packets. Implemented inpumpkin-protocol/src/bedrock/ack.rs
NACK Packets
Negative acknowledgment packets request retransmission of missing packets.Frame Sets
Groups multiple packets into frames for efficient transmission. Implemented inpumpkin-protocol/src/bedrock/frame_set.rs
Network Items
Individual packets within a frame set. Implemented inpumpkin-protocol/src/bedrock/network_item.rs
Packet Organization
Serverbound Packets
Located inpumpkin-protocol/src/bedrock/server/:
- Connection request packets
- Game packets from client
- ACK/NACK responses
Clientbound Packets
Located inpumpkin-protocol/src/bedrock/client/:
- Connection reply packets
- Game packets to client
- ACK/NACK requests
Encryption and Compression
Current Status
As of the latest implementation:- Encryption: Not yet implemented for Bedrock (stubbed in encoder/decoder)
- Compression: Framework in place but not fully implemented
pumpkin-protocol/src/bedrock/packet_encoder.rs:106-112pumpkin-protocol/src/bedrock/packet_decoder.rs:102-108
Planned Support
Differences from Java Edition
| Feature | Java Edition | Bedrock Edition |
|---|---|---|
| Transport | TCP | UDP (RakNet) |
| Reliability | TCP guarantees | RakNet reliability system |
| Packet Header | VarInt length + ID | 0xFE + VarUInt length + 14-bit header |
| Compression | Zlib (implemented) | Zlib (planned) |
| Encryption | AES-128 CFB8 | Not implemented |
| MTU | N/A (TCP) | 1400 bytes |
| Sub-Clients | No | Yes (split-screen) |
Packet Traits
BClientPacket (Serverbound)
pumpkin-protocol/src/lib.rs:326-328
BServerPacket (Clientbound)
pumpkin-protocol/src/lib.rs:330-332
Packet Trait
pumpkin-protocol/src/packet.rs:13-15
RakNet Protocol Flow
Connection Establishment
- Client sends Open Connection Request 1 with MTU size
- Server responds with Open Connection Reply 1
- Client sends Open Connection Request 2 with client GUID
- Server responds with Open Connection Reply 2 with server GUID
- Client sends Connection Request
- Server responds with Connection Request Accepted
- Client sends New Incoming Connection
- Connection established, game packets can be sent
Reliable Packet Flow
- Sender transmits packet with sequence number
- Receiver stores packet and sends ACK
- If ACK not received within timeout, sender retransmits
- If packet missing, receiver sends NACK to request retransmission
Next Steps
- Java Edition Protocol - Compare with Java Edition
- Encryption - Learn about encryption (Java only currently)
- Compression - Learn about compression support
