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.
Current gameplay-facing outbound examples:
PlayerStatusHandleremitsPlayerStatusPacket(0x11) for basic status requestsPlayerStatusHandleremitsSkillListPacket(0x3A) for skill window requestsChatSystemServiceemitsChatCommandPacket(0xB2) for classic conference chat UI, channel membership, PMs, moderator/voice changes, and system responsesItemHandlerresponds to classic book traffic:0xD4saves writabletitle/author0x66serves page requests and writable page saves
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.
Current Request Flows
Important current request/response pairs:
0x34 Get Player StatusBasicStatus->0x11 Player StatusRequestSkills->0x3A Skill List
0xB5 Open Chat Window- client open request -> server creates/refreshes runtime chat user and returns
0xB2chat-window command
- client open request -> server creates/refreshes runtime chat user and returns
0xB3 Chat Text- action dispatch in
IChatSystemService - supports conference message/join/create/rename/password, PM, ignore, ops/voice, whois, kick, emote
- action dispatch in
0x66 Book Pages- page request -> server book page response
- writable page save -> server persists updated page content
0xD4 Book Header New- writable header save -> server persists
title/author
- writable header save -> server persists
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