Table of Contents

Scene Channel System

View this file on GitHub

Short description: SceneServer subsystem for open-world scene channel listing and switching, aggregating scene instances across scene servers via database queries and handling same-server or cross-server channel transitions with DoS protection and write-through TTL caching.

Table of Contents

Overview

The Scene Channel system is a scene-server subsystem that provides channel selection for open-world scenes. Channels are multiple instances of the same scene on the same world server, potentially hosted across different scene servers. The system aggregates all available instances via database queries (ISceneService.FetchAvailableAsync, ISceneServerService.FetchAsync) and exposes a channel list to the client. The scene-server lookup is a liveness probe only: its answer (the hosting process's port) never leaves the server, because a switch is never a direct dial and a port list would map which scene server hosts which instance. It also handles same-server or cross-server channel switching by updating the character's SceneHandle and disconnecting the client for world-server re-route.

The implementation uses a split execution model:

  • Main thread: request validation, ingress guard checks, cooldown enforcement, network broadcasts, and character state mutations.
  • Async worker: database reads via TryEnqueueIngressWork for scene-instance and scene-server-address queries.
  • Main-thread queue: marshalling async completion actions back to Unity/FishNet-safe context via ISceneChannelSystemMainThreadQueueData.

All database work is asynchronous. Main-thread mutations (FishNet broadcasts, character state changes, disconnects) are marshalled through an isolated main-thread queue container. Two write-through TTL caches reduce repeated database polling for scene-instance and scene-server-address queries.

Supported Platforms

Platform Supported Notes
Windows Yes
Linux Yes
WebGL N/A Server-only module
Unity 6.6 (6000.6.8f1) Yes Required engine version
IL2CPP Yes Supported scripting backend

Features

  • Channel listing aggregating all available open-world scene instances across scene servers on the same world server via database queries
  • Opt-in initial channel list dispatch when a character loads into an open-world scene (sendChannelListOnCharacterLoad, off by default; OnAfterLoadCharacter is only subscribed when it is on)
  • Explicit channel list refresh via RequestSceneChannelListBroadcast from the client
  • Channel switching with database validation of target handle existence, OpenWorld scene type, and capacity
  • Ordered hand-off through ICharacterSystem.BeginChannelTransfer: departure announced, then rebind, save, session release, disconnect
  • Client disconnect triggering standard save/despawn/session-release pipeline via CharacterSystem.OnRemoteConnectionStopped
  • Auto-reconnect through world server load balancing to the scene server hosting the target channel (SceneHandle-aware routing)
  • Per-connection, per-operation ingress guard with configurable debounce and in-flight gating (RequestList, SelectChannel)
  • Bounded ingress guard sweep with configurable TTL, interval, and max removals
  • Per-connection channel switch cooldown with configurable interval and bounded dictionary (DoS defense), timed on MonotonicClock so a stepped host clock cannot hold every entry inside its window
  • Scene-server liveness judged by SceneServerData.PulseAgeSeconds — the pulse's age as the database measured it when the row was read — with the world server's SceneServerPulseStaleSeconds threshold (60 s); never by subtracting LastPulse from this host's clock
  • Hard cap on cooldown dictionary size (maxCooldownEntries) rejecting new entries when saturated
  • Periodic cleanup of expired cooldown entries with bounded removal per sweep
  • Immediate cooldown entry removal on client disconnect
  • Write-through TTL cache for scene-instance query results (AvailableSceneCache, keyed by worldServerID|sceneName via BuildAvailableSceneCacheKey with an OrdinalIgnoreCase comparer — the scene name alone collided between world servers sharing a scene server)
  • Write-through TTL cache for scene-server ports (SceneServerAddressCache, TimedCache<long, ushort> keyed by scene server ID)
  • Cache invalidation on fetch failure to prevent routing to dead scene servers
  • Periodic expired-cache sweep piggybacking on the cooldown cleanup cycle
  • Async worker backpressure via TryEnqueueIngressWork (rejects when queue unavailable/full)
  • Per-system main-thread queue isolation with configurable drain cap per frame
  • Instanced scene rejection for both channel listing and switching; the listing answers an instanced character with an empty list from behind the ingress guard rather than an unlimited reliable reply per message
  • Graceful failure semantics: invalid requests fail closed with no mutation; capacity/type checks enforced before state changes; async failures logged without blocking main thread

Prerequisites

  • Unity 6.6 (6000.6.8f1)
  • FishNetworking — networking framework
  • FishMMO Server Core — provides ServerBehaviour, ISceneChannelSystem, ISceneChannelSystemRuntimeData, ISceneChannelSystemMainThreadQueueData, broadcast types (RequestSceneChannelListBroadcast, SceneChannelSelectBroadcast, SceneChannelListBroadcast, ChannelAddress), IngressGuard, TimedCache, AsyncWorkerData, ISceneInstanceMappingData, ICharacterMappingData<NetworkConnection>, ISceneServerSystem<NetworkConnection>, ICharacterSystem<NetworkConnection, Scene>, CharacterFlags, and SceneType
  • FishMMO Database — provides ISceneService, ISceneServerService, SceneData, and DatabaseResult<T>

Installation / Build

This is an integrated module within FishMMO. It is included as part of the server-side scene-server implementation and does not require separate installation. Ensure the FishMMO Server Core and its dependencies are properly configured in your Unity project.

Quick Start Guides

  1. Ensure SceneChannelSystem is present on the scene server GameObject (it inherits from ServerBehaviour and implements ISceneChannelSystem). The asset is created via Create > FishMMO > Server > SceneServer > Scene Channel System.
  2. Verify that the following data containers are registered in DataContainerRegistry:
    • SceneChannelSystemRuntimeData → ISceneChannelSystemRuntimeData
    • SceneChannelSystemMainThreadQueueData → ISceneChannelSystemMainThreadQueueData
    • AsyncWorkerData (shared async work queue)
  3. Verify that the following dependencies resolve from DataContainerRegistry:
    • ISceneInstanceMappingData
    • ICharacterMappingData<NetworkConnection>
  4. Verify that ISceneServerSystem<NetworkConnection> is registered in BehaviourRegistry (used for WorldSceneDetailsCache.MaxClients lookups).
  5. Verify that ICharacterSystem<NetworkConnection, Scene> is registered in BehaviourRegistry for character load event subscriptions.
  6. Verify that ISceneService and ISceneServerService are available in Database.ServiceRegistry.
  7. On initialize, SceneChannelSystem registers broadcast handlers for RequestSceneChannelListBroadcast and SceneChannelSelectBroadcast (both require authentication), subscribes to OnAfterLoadCharacter only when sendChannelListOnCharacterLoad is enabled, subscribes to connection events for cooldown cleanup, and clamps all serialized fields.
  8. On deinitialize, it drains the remaining main-thread queue, clears both caches, unregisters broadcast handlers, unsubscribes character and connection callbacks.
  9. Clients send RequestSceneChannelListBroadcast to refresh the channel list or SceneChannelSelectBroadcast to switch channels; the server validates, queries the database, and replies with a SceneChannelListBroadcast or disconnects the client for world-server re-route.

Configuration

Inspector Parameters

Parameter Type Default Description
maxMainThreadActionsPerFrame int 50 Max queued channel-system actions drained from main-thread queue per frame
ingressDebounceMilliseconds int 500 Per-connection, per-operation debounce window in milliseconds
ingressSweepIntervalSeconds float 10.0 Seconds between ingress guard stale-entry sweep passes
ingressEntryTtlSeconds float 60.0 Maximum age in seconds for ingress entries before sweep eligibility
ingressSweepMaxRemovals int 128 Maximum ingress entries removed per sweep pass
channelSwitchCooldownSeconds float 10.0 Minimum seconds between channel switch attempts per connection
cooldownCleanupIntervalSeconds float 30.0 Seconds between stale cooldown dictionary cleanup sweeps
cooldownCleanupMaxRemovals int 128 Maximum cooldown entries removed per cleanup sweep
maxCooldownEntries int 5000 Hard cap on cooldown dictionary size (DoS defense)
sceneInstanceCacheTtlSeconds float 5.0 TTL for cached scene-instance query results; 0 disables caching
sceneServerCacheTtlSeconds float 10.0 TTL for cached scene-server ports; 0 disables caching
sendChannelListOnCharacterLoad bool false Push an unsolicited channel list to every client as its character loads; off because the client asks when the picker opens and the list is stale within seconds

Clamped Minimums

All serialized fields are clamped on initialization to prevent invalid configuration:

Parameter Minimum
maxMainThreadActionsPerFrame 1
ingressDebounceMilliseconds 50
ingressSweepIntervalSeconds 1.0
ingressEntryTtlSeconds 5.0
ingressSweepMaxRemovals 1
channelSwitchCooldownSeconds 1.0
cooldownCleanupIntervalSeconds 5.0
cooldownCleanupMaxRemovals 1
maxCooldownEntries 100
sceneInstanceCacheTtlSeconds 0.0
sceneServerCacheTtlSeconds 0.0

Ingress Operations

Operation Byte Value Used By
RequestList 1 OnCharacterLoaded, OnRequestChannelList
SelectChannel 2 OnChannelSelect

Threading Model

Thread Work
Main thread Request validation, ingress guards, cooldown enforcement, broadcast dispatch, character state mutations (SceneHandle, CharacterFlags), disconnect, queue drain, ingress sweep, cooldown cleanup, cache sweep
Async worker Database reads (FetchAvailableScenesAsync, FetchSceneServerAddressAsync) via TryEnqueueIngressWork with automatic guard release

Scene Instance Caches

Two TimedCache instances in SceneChannelSystemRuntimeData reduce database polling:

Cache Key Value Default TTL Comparer
AvailableSceneCache worldServerID\|sceneName (string) IReadOnlyList<SceneData> 5 s OrdinalIgnoreCase
SceneServerAddressCache scene server ID (long) ushort (port, server-side only) 10 s default

Cache behaviour:

  • On hit (within TTL): returns cached value, skips database query.
  • On miss or TTL expired: queries database, stores result in cache.
  • On fetch failure (SceneServerAddressCache): invalidates the stale entry so subsequent calls re-fetch immediately, and the channel is left out of the list — a scene row outlives the process that served it, so a failed lookup is what keeps a dead instance out.
  • Expired entries are swept periodically during the cooldown cleanup cycle (max 64 scanned, 32 removed per sweep per cache).

Usage Examples

Broadcast Protocol

Broadcast Direction Purpose
RequestSceneChannelListBroadcast Client → Server Client requests the channel list (empty payload)
SceneChannelSelectBroadcast Client → Server Client selects a target channel (contains ChannelAddress with target SceneHandle)
SceneChannelListBroadcast Server → Client Server sends the list of available channels (contains List<ChannelAddress>)

Both inbound broadcast handlers require authentication (requireAuthentication = true).

Channel List (Opt-in on Load)

OnCharacterLoaded(conn, character) — fired by ICharacterSystem.OnAfterLoadCharacter, and only subscribed when sendChannelListOnCharacterLoad is enabled:

  1. Returns immediately if the connection is null/inactive (a lingering combat-logout body is reattached through the same load path and has no connection) or the character is in an instanced scene.
  2. Acquires ingress guard (RequestList).
  3. Captures sceneName, worldServerID, characterID, and SceneHandle.
  4. Validates sceneName is not null/empty.
  5. Resolves maxClients from WorldSceneDetailsCache (fallback: WorldSceneSettings.MaximumClientsPerScene, 200).
  6. Enqueues FetchAndSendChannelListAsync via TryEnqueueIngressWork; a rejected enqueue releases the guard and answers SendServerBusy.

Channel List (Explicit Request)

OnRequestChannelList(conn, msg, channel) — handles RequestSceneChannelListBroadcast:

  1. Validates connection is active and has a spawned object.
  2. Looks up the character from ICharacterMappingData<NetworkConnection>.
  3. Acquires ingress guard (RequestList) — before the instanced-scene branch, which is otherwise an unbounded reliable reply per message.
  4. If the character is in an instanced scene: release the guard and answer with an empty list carrying InstanceSceneHandle.
  5. Captures sceneName, worldServerID, characterID, and SceneHandle.
  6. Validates sceneName is not null/empty.
  7. Resolves maxClients from WorldSceneDetailsCache (fallback: WorldSceneSettings.MaximumClientsPerScene, 200).
  8. Enqueues FetchAndSendChannelListAsync via TryEnqueueIngressWork; a rejected enqueue releases the guard and answers SendServerBusy.

FetchAndSendChannelListAsync

  1. Validates Database.ServiceRegistry, ISceneService, ISceneServerService, and SceneChannelSystemRuntimeData.
  2. Fetches available scene instances (cache-aware via FetchAvailableScenesAsync).
  3. Filters to SceneType.OpenWorld only.
  4. For each scene instance, resolves the hosting scene server's port (cache-aware via FetchSceneServerAddressAsync) purely to prove the process is alive; an instance whose lookup fails is dropped from the list.
  5. Builds List<ChannelAddress> with SceneHandle (the scenes.id row), SceneName, and CharacterCount. ChannelAddress carries no address or port field: the resolved port stays server-side.
  6. Marshals SceneChannelListBroadcast (Addresses array plus CurrentSceneHandle) to the main thread for transmission — from a finally, so every failure path above still answers, with an empty list.

Channel Switch

OnChannelSelect(conn, msg, channel) — handles SceneChannelSelectBroadcast:

  1. Validates connection is active with a spawned object.
  2. Looks up the character from ICharacterMappingData<NetworkConnection>.
  3. Rejects if character is in an instanced scene.
  4. Rejects if currentHandle == targetHandle (already on target channel).
  5. Enforces per-connection cooldown:
    • Rejects if within cooldown window.
    • Rejects if dictionary is saturated (>= maxCooldownEntries) and client has no existing entry.
  6. Acquires ingress guard (SelectChannel).
  7. Records cooldown timestamp immediately to prevent rapid re-entry during async window.
  8. Resolves maxClients from WorldSceneDetailsCache.
  9. Enqueues ValidateAndSwitchChannelAsync via TryEnqueueIngressWork.

ValidateAndSwitchChannelAsync

  1. Validates Database.ServiceRegistry and ISceneService.
  2. Fetches available instances (cache-aware) and verifies target handle exists, is OpenWorld, and has capacity (CharacterCount < maxClients).
  3. Claims the switch cooldown against the character row (ICharacterService.TryBeginChannelSwitchAsync). This is the cooldown that actually holds: the per-connection dictionary is wiped by the very disconnect the switch performs. Claimed only after the destination has been validated, so a player is not put on cooldown for asking about a channel that turned out to be full.
  4. Marshals to main thread:
    • Re-validates the character is still mapped to the connection and still passes CanActOrMove — the database round trip above gives combat or death time to intervene.
    • Hands the whole transfer to ICharacterSystem.BeginChannelTransfer(conn, targetHandle).
  5. BeginChannelTransfer raises OnDisconnect while SceneHandle still names the instance being left (that event is what debits scene population), then rebinds SceneHandle, saves, releases the session claim, and drops the connection. Doing it in that order is the point of the method: debiting after the rebind charged the destination instance — often one this server does not host — and left the source permanently over-populated.
  6. BuildCharacterData captures the updated SceneHandle so the character is persisted with the target channel.
  7. Any failure after the claim is taken — inactive connection, character no longer mapped, CanActOrMove refusal, BeginChannelTransfer returning false, or a rejected main-thread enqueue — puts the claim back via ReleaseChannelSwitchClaim, so a player is never charged the cooldown for a switch they did not get.
  8. The client auto-reconnects to the world server, which routes through ProcessOpenWorldQueueAsync to the scene server hosting the target channel.

SceneHandle is a scenes.id, not a Unity scene handle. A scene-manager handle is allocated from a per-process counter, so two scene servers running the same build routinely produce the same value for different scenes — it cannot identify an instance across processes. The row id is unique by construction. ChannelAddress.SceneHandle carries the same row id.

Failure Semantics

  • Invalid requests fail closed with no mutation.
  • Every request is answered. A channel-list request always produces a SceneChannelListBroadcast, even when the list is empty (instanced scene, no other instances, database unavailable) — the picker opens on the player's click and only fills in or closes when a list arrives, so a silent failure left an empty window over the game. A refused switch always produces a SceneTransferRefusedBroadcast naming the reason, including AlreadyAtDestination.
  • The list carries CurrentSceneHandle so the client can mark the channel the player is already on. It cannot work this out for itself: IPlayerCharacter.SceneHandle is server-side state and is never replicated.
  • Capacity and scene-type checks are enforced before state changes.
  • Async failures are caught, logged, and do not block the main thread.
  • Main-thread completion paths revalidate connection activity and character mapping before mutating or broadcasting.
  • Ingress guards are always released via TryEnqueueIngressWork deferred release (on completion or failure).
  • Cache invalidation on fetch failure prevents stale routing.

Operational Checks

Check How to Verify
Initialization success Confirm SceneChannelSystem logs "Initialized (Debounce=500ms, SwitchCooldown=10s)" without errors on server startup
Data containers available Verify ISceneChannelSystemRuntimeData, ISceneChannelSystemMainThreadQueueData, and AsyncWorkerData all resolve from DataContainerRegistry
Dependencies available Verify ISceneInstanceMappingData, ICharacterMappingData<NetworkConnection>, and ISceneServerSystem<NetworkConnection> resolve from their respective registries
Database services available Verify ISceneService and ISceneServerService resolve from Database.ServiceRegistry
Initial channel list on load Enable sendChannelListOnCharacterLoad, load a character into an open-world scene, and confirm SceneChannelListBroadcast is sent unprompted; with the flag off (the default) confirm nothing is sent until the client asks
Instanced scene rejection (load) Load a character into an instanced scene; confirm no channel list is sent
Channel list carries no ports Inspect a SceneChannelListBroadcast; confirm each ChannelAddress has only SceneHandle, SceneName, and CharacterCount
Explicit channel list request Send RequestSceneChannelListBroadcast from an authenticated client in an open-world scene; confirm SceneChannelListBroadcast reply with List<ChannelAddress>
Instanced scene request Send RequestSceneChannelListBroadcast from a character in an instanced scene; confirm an empty SceneChannelListBroadcast is returned so the client can close its picker
Channel switch Send SceneChannelSelectBroadcast with a valid target handle; confirm OnDisconnect fires before the rebind, SceneHandle is updated, the session is released, and the client is disconnected
Same-channel rejection Send SceneChannelSelectBroadcast with targetHandle == currentHandle; confirm a SceneTransferRefusedBroadcast with AlreadyAtDestination is returned
Combat gate Send SceneChannelSelectBroadcast while in combat; confirm SceneTransferRefusedBroadcast with CharacterStateChanged and that no combat-logout body is left behind
Target validation Send SceneChannelSelectBroadcast with a non-existent or full target handle; confirm request is rejected after async validation
Channel switch cooldown Send rapid consecutive SceneChannelSelectBroadcast requests; confirm excess requests are dropped within channelSwitchCooldownSeconds
Cooldown dictionary saturation Saturate the cooldown dictionary to maxCooldownEntries; confirm new entries from unknown clients are rejected
Ingress debounce Send rapid consecutive RequestSceneChannelListBroadcast from the same connection; confirm excess requests are dropped
Ingress in-flight guard Send overlapping async requests; confirm only one is processed at a time per operation type
Ingress sweep Wait for ingressSweepIntervalSeconds; confirm stale guard entries are cleaned up
Cooldown cleanup Wait for cooldownCleanupIntervalSeconds; confirm expired cooldown entries are removed
Disconnect cleanup Disconnect a client; confirm cooldown entry is removed immediately via OnRemoteConnectionStopped
Scene instance cache hit Request channel list twice within sceneInstanceCacheTtlSeconds; confirm second request uses cached data (no DB query)
Scene instance cache expiry Wait beyond sceneInstanceCacheTtlSeconds; confirm next request queries the database
Scene server cache invalidation Simulate a scene server fetch failure; confirm cached address is invalidated
Cache sweep Wait for cooldownCleanupIntervalSeconds; confirm expired cache entries are swept
Main-thread queue drain Confirm queued async results are dispatched on the main thread within maxMainThreadActionsPerFrame per frame
World scene details fallback Remove scene from WorldSceneDetailsCache; confirm maxClients falls back to WorldSceneSettings.MaximumClientsPerScene (200), the same ceiling the world server routes against
Deinitialize cleanup Trigger deinitialize; confirm broadcast handlers unregistered, character and connection callbacks unsubscribed, caches cleared, and main-thread queue drained

Flow Diagram

High-Level Overview

flowchart LR
    Client[Unity Client] -->|RequestSceneChannelListBroadcast| SCS[SceneChannelSystem]
    SCS -->|FetchAvailableAsync / FetchAsync| DB[(PostgreSQL)]
    SCS -->|SceneChannelListBroadcast| Client
    Client -->|SceneChannelSelectBroadcast| SCS
    SCS -->|BeginChannelTransfer| CS[CharacterSystem]
    CS -->|save + release + disconnect| Client
    Client -->|reconnect| World[WorldServer]
    World -->|route to hosting scene server| Client

Channel List (Load / Request)

OnCharacterLoaded(conn, character) / OnRequestChannelList(conn, msg, channel)
│
├─ 1. Validate connection + character
├─ 2. Acquire ingress guard (RequestList)
├─ 3. Instanced scene → release guard, send EMPTY list (InstanceSceneHandle)
├─ 4. Capture sceneName, worldServerID, characterID, currentSceneHandle
├─ 5. Resolve maxClients from WorldSceneDetailsCache (fallback: 200)
└─ 6. TryEnqueueIngressWork → FetchAndSendChannelListAsync
       │
       ├─ FetchAvailableScenesAsync(worldServerID, sceneName, maxClients)
       │    └─ Cache hit → return cached; miss → ISceneService.FetchAvailableAsync → cache result
       ├─ Filter to SceneType.OpenWorld
       ├─ For each instance:
       │    └─ FetchSceneServerAddressAsync(sceneServerID)   # liveness probe; port stays server-side
       │         └─ Cache hit → return cached; miss → ISceneServerService.FetchAsync → cache result
       │              └─ Failure → invalidate cache entry, skip this channel
       ├─ Build List<ChannelAddress> (SceneHandle, SceneName, CharacterCount)
       └─ finally: TryEnqueueMainThread
              └─ Validate conn still active → Broadcast SceneChannelListBroadcast
                 (Addresses + CurrentSceneHandle; empty list on any failure above)

Channel Switch

OnChannelSelect(conn, msg, channel)
│
├─ 1. Validate connection + character
├─ 2. Reject if instanced scene
├─ 3. Reject if currentHandle == targetHandle
├─ 4. Enforce per-connection cooldown
│      ├─ Reject if within cooldown window
│      └─ Reject if dictionary saturated (>= maxCooldownEntries)
├─ 5. Acquire ingress guard (SelectChannel)
├─ 6. Record cooldown timestamp immediately
├─ 7. Capture sceneName, worldServerID, characterID, maxClients
└─ 8. TryEnqueueIngressWork → ValidateAndSwitchChannelAsync
       │
       ├─ FetchAvailableScenesAsync (cache-aware)
       ├─ Verify targetHandle exists, is OpenWorld, has capacity
       │    └─ Fails → ResolveUnavailableReasonAsync → SendTransferRefused
       ├─ ICharacterService.TryBeginChannelSwitchAsync (the cooldown that survives the disconnect)
       │    └─ Not granted → SendTransferRefused(OnCooldown)
       └─ TryEnqueueMainThread
              ├─ Re-validate character still mapped to connection
              ├─ Re-check CharacterStateValidation.CanActOrMove
              └─ ICharacterSystem.BeginChannelTransfer(conn, targetHandle)
                     ├─ OnDisconnect raised while SceneHandle still names the source instance
                     ├─ Rebind SceneHandle → save → release session claim → disconnect
                     └─ Client auto-reconnects → world server re-routes to target channel
              (any failure on this path → ReleaseChannelSwitchClaim + SendTransferRefused)

OnUpdate Sweep

OnUpdate(deltaTime)
│
├─ 1. DrainMainThreadQueue (up to maxMainThreadActionsPerFrame)
├─ 2. SweepIngressGuard()
│      └── IngressGuard.Sweep(interval, ttl, maxRemovals)
└─ 3. Decrement NextCooldownCleanup timer
       └── When elapsed:
              ├─ Reset timer to cooldownCleanupIntervalSeconds
              ├─ CleanupExpiredCooldownEntries()
              │    └── Remove entries where (MonotonicClock.NowSeconds - lastSwitch) > cooldownSeconds
              │         (bounded by cooldownCleanupMaxRemovals)
              └─ SweepSceneCaches()
                     ├─ AvailableSceneCache.SweepExpired(ttl, maxScan=64, maxRemove=32)
                     └─ SceneServerAddressCache.SweepExpired(ttl, maxScan=64, maxRemove=32)

Project Structure

Directory Structure

SceneChannel/
├── SceneChannelSystem.cs                    # Channel listing, switching, DoS protection, cache
├── SceneChannelSystemRuntimeData.cs         # Runtime state (ingress guard, cooldowns, caches)
├── SceneChannelSystemMainThreadQueueData.cs # Per-system main-thread action queue container
└── README.md                                # System documentation
  • Server/Core/World/SceneServer/SceneChannel/ISceneChannelSystem.cs
  • Server/Core/World/SceneServer/SceneChannel/ISceneChannelSystemRuntimeData.cs
  • Server/Core/World/SceneServer/SceneChannel/ISceneChannelSystemMainThreadQueueData.cs
  • Server/Core/RuntimeData/IAsyncWorkerData.cs
  • Server/Core/RuntimeData/IMainThreadQueueData.cs
  • Server/Core/Collections/TimedCache.cs
  • Server/Core/Collections/IngressGuard.cs

Inheritance Hierarchy

ServerBehaviour
└── SceneChannelSystem : ISceneChannelSystem

RuntimeDataContainer
├── SceneChannelSystemRuntimeData : ISceneChannelSystemRuntimeData
└── MainThreadQueueData (abstract)
    └── SystemMainThreadQueueData (abstract)
        └── SceneChannelSystemMainThreadQueueData : ISceneChannelSystemMainThreadQueueData

Runtime Data Container Details

SceneChannelSystemRuntimeData — mutable runtime state for ingress protection, cooldowns, and caching. Implements ISceneChannelSystemRuntimeData.

Property Type Purpose
IngressGuard IngressGuard Per-connection, per-operation debounce and in-flight gating
ChannelSwitchCooldownByClientId Dictionary<int, double> Tracks each client's last channel switch attempt, in MonotonicClock seconds, for cooldown enforcement
NextCooldownCleanup float Countdown until next stale cooldown dictionary cleanup sweep
AvailableSceneCache TimedCache<string, IReadOnlyList<SceneData>> Write-through TTL cache of FetchAvailableAsync results keyed by worldServerID\|sceneName
SceneServerAddressCache TimedCache<long, ushort> Write-through TTL cache of scene server ports keyed by scene server ID; used only to prove the host process is alive

Lifecycle:

  • InitializeOnce() — creates IngressGuard, empty cooldown dictionary, and both TimedCache instances (scene cache uses OrdinalIgnoreCase comparer).
  • Clear() — clears guard, cooldowns, resets timer, and clears both caches.
  • OnDeinitialize() — clears and nulls all references.

SceneChannelSystemMainThreadQueueData — per-system main-thread action queue. Inherits from SystemMainThreadQueueData (which inherits from MainThreadQueueData). Implements ISceneChannelSystemMainThreadQueueData. Provides Enqueue(Action) and Drain(int) methods for marshalling async worker responses back to the Unity main thread. The DataContainerRegistry creates independent instances per concrete type, ensuring each system gets its own isolated main-thread queue separate from other subsystems.

Required Data Container Attributes

SceneChannelSystem declares three required containers:

  • [RequiresDataContainer(typeof(SceneChannelSystemMainThreadQueueData))]
  • [RequiresDataContainer(typeof(SceneChannelSystemRuntimeData))]
  • [RequiresDataContainer(typeof(AsyncWorkerData))]

License

This project is subject to the FishMMO project license.

Eligibility

OnChannelSelect gates on CharacterStateValidation.CanActOrMove, the same predicate every other state-changing handler uses. It rejects a character that is dead, teleporting, frozen, not finished loading, or in combat, and the check is repeated on the main thread after the database validation returns, because the character's state can change while that runs.

Combat matters for the same reason CharacterSystem.IPlayerCharacter_OnTeleport refuses a teleport mid-fight: a channel switch is a voluntary move to another scene instance, so allowing it in combat makes it a cleaner escape than a teleporter — instant, and it lands the player on an instance their attacker is not in.

It is also actively corrupting rather than merely unfair. The switch works by rewriting SceneHandle and dropping the connection, which lands in CharacterSystem.OnRemoteConnectionStopped — the one path that passes allowCombatLinger: true. In combat that starts a combat-logout linger: the body stays on the old instance holding the character's session claim, while the row now says the character belongs to the target handle. The world server then routes the reconnect to the target scene server, which has no body to reattach, tries a fresh claim, loses to the still-held one, and kicks the player — repeatedly, until the linger expires.

OnRequestChannelList is deliberately left ungated: it is a read-only query, treated the same way as the chat and name-lookup handlers. The authoritative gate is on the action.