Network Stack Overview
Connection Class
TheConnection class (Connection.h:20) provides the core networking functionality:
The Connection class uses separate read and write threads to prevent blocking and ensure smooth gameplay even with network latency.
Read Thread
Write Thread
Packet System
Packets are defined innet.minecraft.network.packet.h with a base class:
Common Packet Types
Connection Packets
Connection Packets
PreLoginPacket: Initial handshake (UGC settings, texture pack info)LoginPacket: Player authentication and spawn dataDisconnectPacket: Connection termination with reasonKeepAlivePacket: Connection health check (every 15 seconds)
Player Movement
Player Movement
MovePlayerPacket: Player position and rotationTeleportEntityPacket: Instant position changeMoveEntityPacket: Entity position deltaRotateHeadPacket: Entity head rotation
World Data
World Data
ChunkVisibilityPacket: Send/remove chunk dataChunkVisibilityAreaPacket: Set render distanceTileUpdatePacket: Single block changeBlockRegionUpdatePacket: Multiple block changes
Entity Updates
Entity Updates
AddPlayerPacket: Spawn player entityAddMobPacket: Spawn mob entityAddEntityPacket: Spawn generic entityRemoveEntitiesPacket: Despawn entitiesSetEntityDataPacket: Entity metadata update
Game Actions
Game Actions
PlayerActionPacket: Mining, interact, use itemUseItemPacket: Right-click/place blockAnimatePacket: Swing arm, take damage, etc.InteractPacket: Entity interaction
Packet Example: MovePlayerPacket
Platform-Specific Networking
Socket Abstraction
P2P Connection Manager
Each platform has its own P2P implementation:Multiplayer Synchronization
Entity Synchronization
The server is authoritative for all entity state:Client-Side Prediction
For responsive gameplay, clients predict their own movement:When the server sends back the authoritative position, the client smoothly interpolates if there’s a mismatch, avoiding jarring teleportation.
Chunk Loading
Chunks are streamed based on player position:Block Updates
Block changes are broadcast to nearby players:Network Optimization
Slow Queue System
Low-priority packets use a rate-limited queue to prevent bandwidth saturation:- Non-critical entity updates
- Distant chunk data
- Particle effects
- Sound events
Packet Batching
Multiple small updates can be batched:Compression
Large data (chunks, textures) uses compression:Texture Synchronization
Custom skins and texture packs are synchronized:Texture packets use the slow queue to avoid overwhelming the network during multiplayer sessions.
Error Handling
Disconnect Reasons
Connection Timeout
Best Practices
Minimize Packet Size
Use compact data types (shorts instead of ints where possible) and avoid sending redundant data.
Batch Updates
Group multiple changes into region update packets instead of individual updates.
Use Slow Queue
Put non-critical updates in the slow queue to prioritize gameplay packets.
Validate Server-Side
Never trust client data. Always validate positions, inventory changes, etc.
Related Topics
Client-Server
Learn about the client-server architecture
Overview
High-level architecture overview