Ss14 netcode

From space-sunrise/project-fire · View interactive version

Ss14 netcode is an AI agent skill from the GitHub repository space-sunrise/project-fire. Architecture guide for networking in Space Station 14 — Lidgren integration, NetManager abstraction, message system, game state synchronization, PVS, network events, and component networking

How to use this skill

Skill file

SS14 network architecture

Stack overview

The SS14 networking stack consists of several layers of abstraction, from low-level transport to game state synchronization logic:

┌─────────────────────────────────────────────┐
│ Content (game systems) │
│  RaiseNetworkEvent / ComponentState / Dirty  │
├─────────────────────────────────────────────┤
│         ServerGameStateManager              │
│         ClientGameStateManager              │
│         GameStateProcessor                  │
├─────────────────────────────────────────────┤
│              PVS System                     │
│ (filtering the visibility of entities) │
├─────────────────────────────────────────────┤
│       NetMessage / MsgState / MsgGroups     │
│ (typed messages with serialization)│
├─────────────────────────────────────────────┤
│            NetManager                       │
│ (wrapper over Lidgren, control │
│ channels, packet dispatch) │
├─────────────────────────────────────────────┤
│     Lidgren (NetPeer/NetServer/NetClient)   │
│          UDP + reliability layer            │
└─────────────────────────────────────────────┘

Lidgren: transport layer

SS14 uses the Lidgren library for UDP transport. The engine does not use Lidgren directly in the game code - all work is done through the INetManager and NetMessage abstractions.

Lidgren configuration

The engine configures Lidgren via CVars:

Time synchronization

When initialized, the engine synchronizes NetTime Lidgren with its own RealTime:

NetTime.SetNow(_timing.RealTime.TotalSeconds);

This is critical for packet timings to work correctly.

NetMessage: typed message system

All network messages are inherited from the abstract class NetMessage.

Message groups (MsgGroups)

Each message belongs to a group that determines the default delivery method:

| Group | Delivery | Destination |

|--------|----------|------------|

| Core | ReliableUnordered | Connections, disconnections, ticks |

| Entity | Unreliable Game state synchronization |

| String | ReliableOrdered | Chat, text messages |

| Command | ReliableUnordered | Commands client → server |

| EntityEvent | ReliableOrdered | ECS events between server and client |

Lidgren delivery methods

Sequence Channels

Lidgren supports up to 32 channels for ordered messages. Channels 16+ are reserved for internal engine needs. Messages in different channels are ordered independently.

Registration and processing

// Registering a message with a handler
_networkManager.RegisterNetMessage<MsgStateAck>(HandleStateAck);

// Registration without handler (for sending only)
_networkManager.RegisterNetMessage<MsgState>();

NetManager: network management

NetManager is the central point of the network subsystem. It implements both interfaces: IClientNetManager and IServerNetManager.

Main processing cycle

The ProcessPackets() method is called every frame and does:

  1. Reads all incoming messages from Lidgren peers
  2. Classifies by NetIncomingMessageType:
  1. Updates Prometheus metrics (sent/received packets, bytes, resends)
  2. Recycles used Lidgren buffers

StringTable

To save traffic, message type names are passed as numeric IDs via StringTable. When connecting, the server and client synchronize the mapping table.

Channels (NetChannel)

Each connection is represented by NetChannel, which stores:

Synchronizing the game state (GameState)

GameState structure

GameState — a snapshot of the game state at a specific tick. Contains:

Delta states

The server sends the delta between two ticks, rather than the full state each time. FromSequence = 0 means full status (after connection or on error).

Compression and reliability (MsgState)

The MsgState class implements smart dispatch logic:

Flow server → client

  1. Server: ServerGameStateManager.SendGameStateUpdate() → PvsSystem.SendGameStates(players)
  2. PVS calculates visible entities for each player
  3. An individual GameState is formed for each player
  4. State is serialized, compressed, sent as MsgState
  5. Client: receives MsgState, deserializes, places in GameStateProcessor
  6. The client responds with MsgStateAck confirmation

Client state processing

GameStateProcessor buffers received states and releases them for use:

Query full status

If there is an error (missing entity metadata, desynchronization), the client requests the full state via MsgStateRequestFull. The server responds with a state of FromSequence = 0.

Potentially Visible Set (PVS)

PVS is a server-side optimization system that determines which entities each client sees.

Operating principle

Visibility Lifecycle

  1. Entity enters PVS - the client receives the full state, the Detached flag is cleared
  2. Entity in PVS - client receives delta updates
  3. The entity leaves PVS - the server sends MsgStateLeavePvs, the client sets the flag MetaDataFlags.Detached

Detached entities

When an entity exits PVS:

PVS Overrides

For entities that should be visible always or to specific players:

// Visible to all clients, regardless of distance
_pvs.AddGlobalOverride(entityUid);

// Visible to a specific session
_pvs.AddSessionOverride(entityUid, session);

Typical applications: player UI entities, global controllers, objects on the character’s hands.

Network Events

Two types of events

SS14 has local and online events. These are fundamentally different mechanisms:

// Local event - only in the current process
RaiseLocalEvent(uid, new MyLocalEvent());
SubscribeLocalEvent<MyComponent, MyLocalEvent>(OnMyEvent);

// Network event - sent over the network
RaiseNetworkEvent(new MyNetEvent());
SubscribeNetworkEvent<MyNetEvent>(OnNetEvent);

Server Validation

All events from the client to the server must be validated. The client can send any data:

// ❌ Dangerous - no validation
private void OnClientEvent(MyEvent ev, EntitySessionEventArgs args)
{
    DoAction(ev.TargetEntity); // The client could have spoofed the TargetEntity!
}

// ✅ Safe - with validation
private void OnClientEvent(MyEvent ev, EntitySessionEventArgs args)
{
    if (!HasComp<MyComponent>(ev.TargetEntity))
        return;
    if (!_interaction.InRangeUnobstructed(args.SenderSession, ev.TargetEntity))
        return;
    DoAction(ev.TargetEntity);
}

Validation must occur on the server after receiving the message and on the client before it is sent.

Validation code must be in a shared system/helper class to prevent duplication of logic!

Sending patterns

// Server → all clients
RaiseNetworkEvent(new MyEvent());

// Server → specific client
RaiseNetworkEvent(new MyEvent(), session);

// Server → everyone except one
var filter = Filter.Broadcast().RemovePlayerByAttachedEntity(uid);
RaiseNetworkEvent(new MyEvent(), filter);

EntityUid vs NetEntity

SS14 has two entity identification systems:

| | EntityUid | NetEntity |

|---|---|---|

| Where is it used | Locally in progress | When transmitted over a network |

| Stability | Different on client and server | Same everywhere |

| Storage | In components | For transmission only |

Conversion

// EntityUid → NetEntity (to be sent over the network)
var netEntity = GetNetEntity(uid);

// NetEntity → EntityUid (when received from the network)
var uid = GetEntity(netEntity);

Rules

Component Networking: network synchronization of components

Automatic synchronization

This is the basic and recommended method. Described in detail in the skill SS14 ECS Components:

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class MyComponent : Component
{
    [DataField, AutoNetworkedField]
    public float Value = 1f;
}

Key Point: Dirty()

After changing component data in code, it is required to call Dirty():

comp.Value = newValue;
Dirty(uid, comp); // Without this, the client will NOT receive the update!

Dirty() marks the component as changed. The PVS system will include it in the next GameState to send to clients.

⚠️ Common mistake: forgetting Dirty() after changing a component. The data will change on the server, but the client will not receive the update, which will lead to desynchronization.

SendOnlyToOwner

For data that only the "owner" of the entity should see (such as inventory):

[DataField, AutoNetworkedField]
[Access(Other = AccessPermissions.ReadWrite)]
public int SecretValue
{
    get => _secretValue;
    set => _secretValue = value;
}

SessionSpecific

For data that differs between clients (for example, the visibility of a masked character).

NetworkedComponent for Shared only

[NetworkedComponent] can be set only on components in a Shared project. If you put it on a component in a Client or Server project, it will not work silently - without compilation errors, but also without synchronization.

IRobustCloneable for reference types

If the network field contains a reference type (class, collection), the type must implement IRobustCloneable so that the prediction can correctly save and restore state:

[DataField, AutoNetworkedField]
public List<string> Items = new(); // List<T> already implements IRobustCloneable

Debugging network problems

###CVars for simulation

Use in the client or server console:

net.fakeloss 0.1       // 10% packet loss
net.fakelagmin 0.1     // Minimum 100ms latency
net.fakelagrand 0.05   // + random 0-50ms
net.fakeduplicates 0.05 // 5% doubles

net.predict

net.predict false  // Disable client prediction - you can see the real delay
net.predict true   // Turn back on

Prometheus Metrics

The engine exports metrics:

Connection with other skills

Synchronization optimization: `DirtyField` instead of the full `Dirty` (addition)

For large network components with multiple AutoNetworkedField and field deltas enabled, mark point changes with DirtyField.

[RegisterComponent, NetworkedComponent]
[AutoGenerateComponentState(fieldDeltas: true)]
public sealed partial class ProximityDetectorComponent : Component
{
    [AutoNetworkedField] public TimeSpan NextUpdate = TimeSpan.Zero;
    [AutoNetworkedField] public float Distance = float.PositiveInfinity;
    [AutoNetworkedField] public EntityUid? Target;
}

private void Tick(EntityUid uid, ProximityDetectorComponent comp)
{
    comp.NextUpdate += comp.UpdateCooldown;
    DirtyField(uid, comp, nameof(ProximityDetectorComponent.NextUpdate));
    // The delta will be sent only along the changed field.
}

When to do this

  1. One or more specific fields change.
  2. The component is “heavy” in terms of the number of network fields.
  3. Changes occur frequently.

Anti-pattern

// ❌ Completely dirty for every tick when changing one field:
comp.NextUpdate += comp.UpdateCooldown;
Dirty(uid, comp);

Leave the full Dirty for cases where a significant part of the state actually changes at the same time.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.