Overview
TheServerConnection class manages all network connections on the server side. It handles incoming connections, player management, and distributes packets to connected clients.
Class Definition
Minecraft.Client/ServerConnection.h
Constructor
Parameters
server- Pointer to the MinecraftServer instance that owns this connection manager
Connection Management
Accepting Connections
NewIncomingSocket()
PendingConnection to handle the login process.
Example:
- Creates a
PendingConnectionfor the handshake phase - Assigns a unique connection ID
- Adds to pending connections list for processing
ServerConnection.cpp:27 for implementation.
Player Connection Management
addPlayerConnection()
Connection Lifecycle
tick()
- Ticks all pending connections (login phase)
- Removes completed pending connections
- Ticks all active player connections
- Handles chunk sending for players
- Removes disconnected players
- Flushes network buffers
ServerConnection.cpp:62 for implementation.
stop()
Player Connection List
Connection Storage
Thread Safety
pending_cs critical section ensures thread-safe access when accepting new connections from the network thread.
Network Player Interface
Player States
Connections progress through these states:- Socket Accepted - Network connection established
- Pending Connection - Handshake and login phase
- Player Connection - Authenticated and in-game
- Disconnected - Connection closed
Connection Flow
Texture Synchronization
The server handles custom texture/skin requests from clients:addPendingTextureRequest()
true if the request should be sent.
Example:
handleTextureReceived()
ServerConnection.cpp:132 for implementation.
handleTextureAndGeometryReceived()
Server Settings
handleServerSettingsChanged()
HOST_DIFFICULTY- Game difficulty levelHOST_IN_GAME_SETTINGS- Various gameplay options
ServerConnection.cpp:166 for implementation.
Properties
Server State
Pending Requests
Spam Protection
removeSpamProtection()
Best Practices
1. Regular Ticking
Calltick() consistently every server frame:
2. Clean Shutdown
Always callstop() before destroying the server:
3. Connection Validation
Validate connections in the pending phase before promoting to player:4. Thread Safety
Always use the critical section when accessing pending connections from network threads:Performance Considerations
Connection Limits
The server supports multiple simultaneous connections. Monitor the connection count:Bandwidth Management
- Use
PlayerConnection::queueSend()for non-critical packets - Batch multiple tile updates into
ChunkTilesUpdatePacket - Limit texture synchronization frequency
Error Handling
The connection manager handles various error conditions:- Connection timeouts - Automatically disconnects inactive clients
- Malformed packets - Validates and rejects invalid data
- Resource limits - Can reject connections if server is full
See Also
- ClientConnection API - Client-side connection management
- PlayerConnection API - Individual player connection handling
- Packet System - Network packet reference
- MinecraftServer - Server implementation