Connection
The client establishes a TCP connection to the server on startup:network/network_manager.go:18-25
localhost:4422
Packet Channel
Packets are communicated between the network reader goroutine and the game loop via a buffered channel:network/network_manager.go:13-16
Fields
Buf: Binary buffer containing packet payload dataPacketData: Metadata about the packet (opcode, length, handler)
Reading Packets
Packets are read in a separate goroutine that runs for the lifetime of the connection:network/network_manager.go:27-75
Packet Format
All packets follow this structure:- Opcode (1 byte): Identifies the packet type
- Length (2 bytes, optional): For variable-length packets (when
PacketData.Length == -1) - Payload (variable): Packet-specific data
PacketData.Length.
Server-to-Client Packets
All S2C packet handlers implement thePacket interface:
network/s2c/packet.go:8-10
Packet Registry
network/s2c/packet.go:18-40
Example: Login Accepted
network/s2c/login_accepted.go:11-16
Example: Players Update
network/s2c/players_update.go:12-57
- Updates the local player’s position
- Updates or creates remote players
- Removes disconnected players (those not in the update)
Client-to-Server Packets
All C2S packet handlers implement a simpler interface:network/c2s/packet.go:5-8
Sending Packets
Packets are sent using theSendPacket helper:
shared/game.go:49-57
Handle method serializes its data into the buffer, then the entire buffer is sent over the connection.
Example: Move Packet
The local player sends move packets when attempting to move:shared/player.go:30-53
Example: Interact Packet
Interacting with objects or NPCs:shared/player.go:56-79
Packet Processing
Packets are processed each tick in the main game loop:main.go:117-126
Error Handling
- Unknown opcode: Logged, packet skipped
- Read errors: Connection closed, goroutine exits
- Channel full: Connection closed (client can’t keep up)
- Malformed data: Logged in packet handler, processing continues