|
| 1 | +# CSM TM:PE Sync - Manual Traffic Lights / Manuelle Ampeln |
| 2 | + |
| 3 | +This document is bilingual. German (DE) first, English (EN) follows. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## DE - Deutsch |
| 8 | + |
| 9 | +### 1) Kurzbeschreibung |
| 10 | +- Synchronisiert TM:PE Manual Traffic Lights pro Knoten zwischen Host und Clients in CSM-Sitzungen. |
| 11 | +- Verwendet ein Node-Snapshot-Modell: pro betroffenem Knoten wird der komplette manuelle Ampelzustand uebertragen. |
| 12 | +- Host ist autoritativ: Clients senden Aenderungswuensche, der Host wendet den Zustand in TM:PE an und broadcastet den effektiv angewandten Snapshot. |
| 13 | +- Event-getriebener Versand: lokale TM:PE-Events werden knotenweise gebuendelt und als Simulation-Flush verarbeitet. |
| 14 | +- Retry-/Backoff-Apply sorgt fuer robuste Anwendung bei temporaer nicht bereiten TM:PE-Komponenten. |
| 15 | + |
| 16 | +### 2) Dateistruktur |
| 17 | +- `src/CSM.TmpeSync.ManualTrafficLights/` |
| 18 | + - `ManualTrafficLightsSyncFeature.cs` - Feature-Bootstrap (aktiviert Listener). |
| 19 | + - `Handlers/` - CSM-Command-Handler (Server/Client-Verarbeitung). |
| 20 | + - `Messages/` - Netzwerkbefehle (ProtoBuf-Vertraege) fuer Node-Snapshots. |
| 21 | + - `Services/` - Harmony-Listener, Synchronisationslogik, TM:PE-Adapter, Apply-Koordinator, State-Cache. |
| 22 | + |
| 23 | +### 3) Dateiuebersicht |
| 24 | +| Datei | Zweck | |
| 25 | +| --- | --- | |
| 26 | +| `ManualTrafficLightsSyncFeature.cs` | Registriert/aktiviert das Feature und den TM:PE-Listener. | |
| 27 | +| `Handlers/ManualTrafficLightsUpdateRequestHandler.cs` | Server: validiert Requests, fuehrt Apply unter Node-Lock aus und triggert Host-Broadcast. | |
| 28 | +| `Handlers/ManualTrafficLightsAppliedCommandHandler.cs` | Client: verarbeitet Host-Broadcasts und wendet den Snapshot lokal an; `OnClientConnect` triggert Host-Resync. | |
| 29 | +| `Messages/ManualTrafficLightsUpdateRequest.cs` | Client -> Server: Aenderungswunsch (`NodeId`, `State`). | |
| 30 | +| `Messages/ManualTrafficLightsAppliedCommand.cs` | Server -> Alle: final angewandter Snapshot (`NodeId`, `State`). | |
| 31 | +| `Messages/ManualTrafficLightsNodeState.cs` | Wire-State fuer manuelle Ampeln: Node, Segmente, Fussgaengerzustand, Vehicle-Lights. | |
| 32 | +| `Services/ManualTrafficLightsEventListener.cs` | Harmony-Hooks auf relevante TM:PE-Methoden; sammelt lokale Aenderungen pro Knoten und flush't gebuendelt. | |
| 33 | +| `Services/ManualTrafficLightsSynchronization.cs` | Gemeinsamer Versandweg, Host/Client-Dispatch, Retry/Backoff-Apply, Reconnect-Resync. | |
| 34 | +| `Services/ManualTrafficLightsTmpeAdapter.cs` | Lesen/Anwenden des Snapshot-Zustands in TM:PE (SetUp/Remove Manual-Simulation, Segment-/Vehicle-Lights). | |
| 35 | +| `Services/ManualTrafficLightsStateCache.cs` | Host-Cache fuer zuletzt angewandte Snapshots inkl. Hashing, Clone und Prune. | |
| 36 | + |
| 37 | +### 4) Workflow (Server/Host und Client) |
| 38 | +- **Host aendert manuelle Ampeln in TM:PE** |
| 39 | + - Harmony-Postfix erkennt den betroffenen Knoten (z. B. `ChangeMainLight`, `SetLightMode`, `set_CurrentMode`). |
| 40 | + - Listener queued den Knoten und fuehrt genau einen Flush auf der Simulation aus. |
| 41 | + - Der Host liest den kompletten Node-Snapshot und broadcastet `ManualTrafficLightsAppliedCommand`. |
| 42 | + |
| 43 | +- **Client aendert manuelle Ampeln in TM:PE** |
| 44 | + - Harmony-Postfix queued den betroffenen Knoten und erzeugt beim Flush einen Node-Snapshot. |
| 45 | + - Client sendet `ManualTrafficLightsUpdateRequest` an den Host. |
| 46 | + - Host validiert den Knoten, lockt ihn via `EntityLocks.AcquireNode`, wendet den Snapshot an und broadcastet danach den effektiv angewandten Zustand. |
| 47 | + |
| 48 | +- **Apply-Verhalten** |
| 49 | + - Apply laeuft ueber einen Koordinator mit bis zu 6 Versuchen und Frame-Backoff `5/15/30/60/120/240`. |
| 50 | + - Bei temporaeren TM:PE-Problemen (z. B. uninitialisierte Manager, `NullReference`) wird automatisch erneut versucht. |
| 51 | + - Bei finalem Fehler wird server-/clientseitig geloggt. |
| 52 | + |
| 53 | +- **Reconnect-Resync** |
| 54 | + - Beim Client-Reconnect sendet der Host alle gueltigen gecachten `ManualTrafficLightsAppliedCommand`-Eintraege. |
| 55 | + - Vor dem Versand wird der Cache per `Prune` auf gueltige Knoten bereinigt. |
| 56 | + |
| 57 | +- **Ablehnungen** |
| 58 | + - Dieses Feature verwendet keinen dedizierten `RequestRejected`-Kanal; fehlerhafte Requests werden serverseitig verworfen und geloggt. |
| 59 | + |
| 60 | +### 5) Datenaustausch (Nachrichten/Felder) |
| 61 | +| Nachricht | Richtung | Feld | Typ | Beschreibung | |
| 62 | +| --- | --- | --- | --- | --- | |
| 63 | +| `ManualTrafficLightsUpdateRequest` | Client -> Server | `NodeId` | `ushort` | Knoten-ID der manuellen Ampel. | |
| 64 | +| | | `State` | `ManualTrafficLightsNodeState` | Gewuenschter kompletter Node-Snapshot. | |
| 65 | +| `ManualTrafficLightsAppliedCommand` | Server -> Alle | `NodeId` | `ushort` | Knoten-ID der manuellen Ampel. | |
| 66 | +| | | `State` | `ManualTrafficLightsNodeState` | Effektiv angewandter kompletter Node-Snapshot. | |
| 67 | +| `ManualTrafficLightsNodeState` | (payload) | `NodeId` | `ushort` | Zielknoten. | |
| 68 | +| | | `IsManualEnabled` | `bool` | Manual-Simulation aktiv/inaktiv. | |
| 69 | +| | | `Segments[]` | `List<SegmentState>` | Segmentende-Snapshots am Knoten. | |
| 70 | +| `SegmentState` | (payload) | `SegmentId` | `ushort` | Betroffenes Segment. | |
| 71 | +| | | `StartNode` | `bool` | `true` = Startknoten-Ende, `false` = Endknoten-Ende. | |
| 72 | +| | | `ManualPedestrianMode` | `bool` | Manueller Fussgaenger-Modus aktiv/inaktiv. | |
| 73 | +| | | `HasPedestrianLightState` | `bool` | Kennzeichnet expliziten Fussgaenger-Lichtzustand. | |
| 74 | +| | | `PedestrianLightState` | `int` | Serialisierter TM:PE-Lichtzustand fuer Fussgaenger. | |
| 75 | +| | | `VehicleLights[]` | `List<VehicleLightState>` | Fahrzeug-Lichtzustaende pro VehicleType. | |
| 76 | +| `VehicleLightState` | (payload) | `VehicleType` | `int` | Serialisierter TM:PE-VehicleType. | |
| 77 | +| | | `LightMode` | `int` | Serialisierter TM:PE-LightMode. | |
| 78 | +| | | `MainLightState` | `int` | Hauptsignal-Zustand. | |
| 79 | +| | | `LeftLightState` | `int` | Links-Signal-Zustand. | |
| 80 | +| | | `RightLightState` | `int` | Rechts-Signal-Zustand. | |
| 81 | + |
| 82 | +Hinweise |
| 83 | +- Enum-Werte werden als `int` uebertragen, um API-Drift zwischen TM:PE-Versionen robust zu behandeln. |
| 84 | +- Client-Dispatch verwendet pro Knoten Hash-Dedupe, damit identische Snapshots nicht erneut gesendet werden. |
| 85 | +- Host-Cache verwendet Hash-Dedupe fuer Broadcast/Resync und haelt nur den letzten effektiven Snapshot je Knoten. |
| 86 | +- `NetworkUtil.IsSynchronizationReady()` gate't Queue/Flush waehrend der Ladephase. |
| 87 | +- Ignore- und Local-Apply-Scopes verhindern Echo-Schleifen zwischen lokalem Apply und Harmony-Listener. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## EN - English |
| 92 | + |
| 93 | +### 1) Summary |
| 94 | +- Synchronizes TM:PE manual traffic lights per node between host and clients in CSM sessions. |
| 95 | +- Uses a node snapshot model: the full manual light state is transferred for each affected node. |
| 96 | +- Host is authoritative: clients send update requests, the host applies in TM:PE, then broadcasts the effective snapshot. |
| 97 | +- Event-driven dispatch: local TM:PE events are queued per node and processed in a single simulation flush. |
| 98 | +- Retry/backoff apply keeps synchronization stable when TM:PE components are temporarily unavailable. |
| 99 | + |
| 100 | +### 2) Directory Layout |
| 101 | +- `src/CSM.TmpeSync.ManualTrafficLights/` |
| 102 | + - `ManualTrafficLightsSyncFeature.cs` - feature bootstrap (enables listener). |
| 103 | + - `Handlers/` - CSM command handlers (server/client processing). |
| 104 | + - `Messages/` - network commands (ProtoBuf contracts) for node snapshots. |
| 105 | + - `Services/` - Harmony listener, synchronization logic, TM:PE adapter, apply coordinator, state cache. |
| 106 | + |
| 107 | +### 3) File Overview |
| 108 | +| File | Purpose | |
| 109 | +| --- | --- | |
| 110 | +| `ManualTrafficLightsSyncFeature.cs` | Registers/enables the feature and TM:PE listener. | |
| 111 | +| `Handlers/ManualTrafficLightsUpdateRequestHandler.cs` | Server: validates requests, applies under node lock, and triggers host broadcast. | |
| 112 | +| `Handlers/ManualTrafficLightsAppliedCommandHandler.cs` | Client: processes host broadcasts and applies snapshots locally; `OnClientConnect` triggers host resync. | |
| 113 | +| `Messages/ManualTrafficLightsUpdateRequest.cs` | Client -> Server: change request (`NodeId`, `State`). | |
| 114 | +| `Messages/ManualTrafficLightsAppliedCommand.cs` | Server -> All: final applied snapshot (`NodeId`, `State`). | |
| 115 | +| `Messages/ManualTrafficLightsNodeState.cs` | Wire state for manual lights: node, segments, pedestrian state, vehicle lights. | |
| 116 | +| `Services/ManualTrafficLightsEventListener.cs` | Harmony hooks on relevant TM:PE methods; queues local changes per node and flushes in batch. | |
| 117 | +| `Services/ManualTrafficLightsSynchronization.cs` | Common dispatch path, host/client routing, retry/backoff apply, reconnect resync. | |
| 118 | +| `Services/ManualTrafficLightsTmpeAdapter.cs` | Reads/applies snapshot state in TM:PE (set up/remove manual simulation, segment/vehicle lights). | |
| 119 | +| `Services/ManualTrafficLightsStateCache.cs` | Host cache for latest applied snapshots with hashing, cloning, and prune support. | |
| 120 | + |
| 121 | +### 4) Workflow (Server/Host and Client) |
| 122 | +- **Host edits manual lights in TM:PE** |
| 123 | + - Harmony postfix detects the affected node (for example `ChangeMainLight`, `SetLightMode`, `set_CurrentMode`). |
| 124 | + - Listener queues the node and executes exactly one simulation flush. |
| 125 | + - Host reads the full node snapshot and broadcasts `ManualTrafficLightsAppliedCommand`. |
| 126 | + |
| 127 | +- **Client edits manual lights in TM:PE** |
| 128 | + - Harmony postfix queues the affected node and builds a node snapshot during flush. |
| 129 | + - Client sends `ManualTrafficLightsUpdateRequest` to the host. |
| 130 | + - Host validates the node, acquires `EntityLocks.AcquireNode`, applies the snapshot, and then broadcasts the effective state. |
| 131 | + |
| 132 | +- **Apply behavior** |
| 133 | + - Apply runs through a coordinator with up to 6 attempts and frame backoff `5/15/30/60/120/240`. |
| 134 | + - Temporary TM:PE failures (for example uninitialized managers, `NullReference`) automatically trigger retries. |
| 135 | + - Final failures are logged on host/client side. |
| 136 | + |
| 137 | +- **Reconnect resync** |
| 138 | + - On reconnect, the host sends all valid cached `ManualTrafficLightsAppliedCommand` entries to the client. |
| 139 | + - The cache is pruned against current node validity before sending. |
| 140 | + |
| 141 | +- **Rejections** |
| 142 | + - This feature does not use a dedicated `RequestRejected` channel; invalid requests are dropped and logged on the server. |
| 143 | + |
| 144 | +### 5) Data Exchange (messages/fields) |
| 145 | +| Message | Direction | Field | Type | Description | |
| 146 | +| --- | --- | --- | --- | --- | |
| 147 | +| `ManualTrafficLightsUpdateRequest` | Client -> Server | `NodeId` | `ushort` | Node ID of the manual traffic light. | |
| 148 | +| | | `State` | `ManualTrafficLightsNodeState` | Desired full node snapshot. | |
| 149 | +| `ManualTrafficLightsAppliedCommand` | Server -> All | `NodeId` | `ushort` | Node ID of the manual traffic light. | |
| 150 | +| | | `State` | `ManualTrafficLightsNodeState` | Effective full node snapshot after host apply. | |
| 151 | +| `ManualTrafficLightsNodeState` | (payload) | `NodeId` | `ushort` | Target node. | |
| 152 | +| | | `IsManualEnabled` | `bool` | Manual simulation enabled/disabled. | |
| 153 | +| | | `Segments[]` | `List<SegmentState>` | Segment-end snapshots at the node. | |
| 154 | +| `SegmentState` | (payload) | `SegmentId` | `ushort` | Target segment. | |
| 155 | +| | | `StartNode` | `bool` | `true` = start-node end, `false` = end-node end. | |
| 156 | +| | | `ManualPedestrianMode` | `bool` | Manual pedestrian mode enabled/disabled. | |
| 157 | +| | | `HasPedestrianLightState` | `bool` | Indicates explicit pedestrian light state presence. | |
| 158 | +| | | `PedestrianLightState` | `int` | Serialized TM:PE pedestrian light state. | |
| 159 | +| | | `VehicleLights[]` | `List<VehicleLightState>` | Vehicle light states per vehicle type. | |
| 160 | +| `VehicleLightState` | (payload) | `VehicleType` | `int` | Serialized TM:PE vehicle type. | |
| 161 | +| | | `LightMode` | `int` | Serialized TM:PE light mode. | |
| 162 | +| | | `MainLightState` | `int` | Main light state. | |
| 163 | +| | | `LeftLightState` | `int` | Left light state. | |
| 164 | +| | | `RightLightState` | `int` | Right light state. | |
| 165 | + |
| 166 | +Notes |
| 167 | +- Enum values are transferred as `int` to keep compatibility resilient across TM:PE enum drift. |
| 168 | +- Client dispatch uses per-node hash deduplication to suppress identical snapshots. |
| 169 | +- Host cache uses hash deduplication for broadcast/resync and stores only the latest effective snapshot per node. |
| 170 | +- `NetworkUtil.IsSynchronizationReady()` gates queue/flush during loading. |
| 171 | +- Ignore scopes and local-apply scopes prevent feedback loops between local apply and Harmony listeners. |
0 commit comments