Skip to main content

Overview

The ServerConnection class manages all network connections on the server side. It handles incoming connections, player management, and distributes packets to connected clients.

Class Definition

Location: Minecraft.Client/ServerConnection.h

Constructor

Parameters

  • server - Pointer to the MinecraftServer instance that owns this connection manager
Example:

Connection Management

Accepting Connections

NewIncomingSocket()

Accepts a new incoming client connection. Creates a PendingConnection to handle the login process. Example:
Implementation Details:
  • Creates a PendingConnection for the handshake phase
  • Assigns a unique connection ID
  • Adds to pending connections list for processing
See ServerConnection.cpp:27 for implementation.

Player Connection Management

addPlayerConnection()

Promotes a pending connection to a full player connection after successful login. Example:

Connection Lifecycle

tick()

Processes all connections each game tick. This method:
  1. Ticks all pending connections (login phase)
  2. Removes completed pending connections
  3. Ticks all active player connections
  4. Handles chunk sending for players
  5. Removes disconnected players
  6. Flushes network buffers
Example:
See ServerConnection.cpp:62 for implementation.

stop()

Disconnects all players and closes all connections gracefully. Example:

Player Connection List

Connection Storage

Both lists are protected by critical sections for thread safety.

Thread Safety

The 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:
  1. Socket Accepted - Network connection established
  2. Pending Connection - Handshake and login phase
  3. Player Connection - Authenticated and in-game
  4. Disconnected - Connection closed

Connection Flow

Texture Synchronization

The server handles custom texture/skin requests from clients:

addPendingTextureRequest()

Queues a texture request. Returns true if the request should be sent. Example:

handleTextureReceived()

Processes a received texture and distributes it to players who need it. Example:
See ServerConnection.cpp:132 for implementation.

handleTextureAndGeometryReceived()

Handles combined texture and geometry data (for custom player models).

Server Settings

handleServerSettingsChanged()

Broadcasts server setting changes to all connected clients. Settings Types:
  • HOST_DIFFICULTY - Game difficulty level
  • HOST_IN_GAME_SETTINGS - Various gameplay options
Example:
See ServerConnection.cpp:166 for implementation.

Properties

Server State

Pending Requests

Tracks which custom textures are currently being requested to avoid duplicates.

Spam Protection

removeSpamProtection()

Removes rate limiting for a trusted connection. Currently not implemented in Minecraft Community Edition.

Best Practices

1. Regular Ticking

Call tick() consistently every server frame:

2. Clean Shutdown

Always call stop() 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