Network System
This page describes the current network stack used by Moongate v2.
Core Components
MoongateTCPServer(Moongate.Network)- Listens on endpoints and raises connect/disconnect/data events.
MoongateTCPClient(Moongate.Network)- Per-connection transport abstraction with send/receive and middleware support.
NetworkService(Moongate.Server.Services.Network)- Owns protocol framing/parsing and session lifecycle.
GameNetworkSessionService- Stores active
GameSessioninstances keyed by session id.
- Stores active
PacketRegistry/PacketTable- Resolve opcode metadata and packet factories.
Inbound Flow
OnDataReceivedreceives bytes fromMoongateTCPClient.- Bytes are appended to per-session pending buffer (
GameNetworkSession.WithPendingBytesLock(...)). NetworkServiceresolves packet length:- fixed length from
PacketDescriptor.Length - variable length from bytes
[1..2](big-endian, includes header)
- fixed length from
- Raw packet bytes are parsed with
packet.TryParse(rawPacket). - Parsed packet is published to
IMessageBusService.
Protection currently implemented:
- max pending buffer size
- max declared packet length
- unknown opcode handling with protocol-violation counter
- disconnect after repeated violations
Outbound Flow
- Handlers/listeners enqueue packets into
IOutgoingPacketQueue. GameLoopServicedrains queue each loop iteration.IOutboundPacketSenderserializes packet withSpanWriterand sends throughMoongateTCPClient.SendAsync(...).- If packet logging is enabled, hex dump is written with
PacketDatacontext.
Compression and Middleware
GameNetworkSession can toggle transport middleware:
EnableCompression()/DisableCompression()EnableEncryption()/DisableEncryption()
Compression middleware is attached/removed on MoongateTCPClient pipeline.
Session Lifecycle
- connect: create or refresh
GameSession+GameNetworkSession - disconnect: remove session from
GameNetworkSessionService - protocol states:
AwaitingSeed,Authenticated,InGame, etc.
Useful Runtime Diagnostics
- Registered packets are logged at startup (
NetworkService.ShowRegisteredPackets()). - Unknown opcode warnings include descriptor description when available.
- Metrics exposed via
INetworkMetricsSource.
Previous: Architecture Overview | Next: Game Loop