| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Assertions; |
| | 6 | | using DCL.Interface; |
| | 7 | | using DCL.Models; |
| | 8 | | using UnityEngine.SceneManagement; |
| | 9 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | public enum QueueMode |
| | 13 | | { |
| | 14 | | Reliable, |
| | 15 | | Lossy, |
| | 16 | | } |
| | 17 | |
|
| | 18 | | public class MessagingBus : IDisposable |
| | 19 | | { |
| | 20 | | public static bool VERBOSE = false; |
| | 21 | |
|
| | 22 | | public IMessageProcessHandler handler; |
| | 23 | |
|
| 1968 | 24 | | public LinkedList<QueuedSceneMessage> pendingMessages = new LinkedList<QueuedSceneMessage>(); |
| 0 | 25 | | public bool hasPendingMessages => pendingMessagesCount > 0; |
| | 26 | |
|
| | 27 | | //NOTE(Brian): This is handled manually. We aren't using pendingMessages.Count because is slow. Used heavily on |
| | 28 | | public int pendingMessagesCount; |
| 0 | 29 | | public long processedMessagesCount { get; set; } |
| | 30 | |
|
| 0 | 31 | | private static bool renderingIsDisabled => !CommonScriptableObjects.rendererState.Get(); |
| | 32 | | private float timeBudgetValue; |
| | 33 | |
|
| | 34 | | public CustomYieldInstruction msgYieldInstruction; |
| | 35 | |
|
| | 36 | | public MessagingBusType type; |
| | 37 | | public string debugTag; |
| | 38 | |
|
| | 39 | | public MessagingController owner; |
| | 40 | | private IMessagingControllersManager manager; |
| | 41 | |
|
| 1968 | 42 | | Dictionary<string, LinkedListNode<QueuedSceneMessage>> unreliableMessages = new Dictionary<string, LinkedListNod |
| | 43 | | public int unreliableMessagesReplaced = 0; |
| | 44 | |
|
| | 45 | | public bool enabled; |
| | 46 | |
|
| 0 | 47 | | public float timeBudget { get => renderingIsDisabled ? float.MaxValue : timeBudgetValue; set => timeBudgetValue |
| | 48 | |
|
| 1968 | 49 | | public MessagingBus(MessagingBusType type, IMessageProcessHandler handler, MessagingController owner) |
| | 50 | | { |
| 1968 | 51 | | Assert.IsNotNull(handler, "IMessageHandler can't be null!"); |
| 1968 | 52 | | this.handler = handler; |
| 1968 | 53 | | this.enabled = false; |
| 1968 | 54 | | this.type = type; |
| 1968 | 55 | | this.owner = owner; |
| 1968 | 56 | | this.pendingMessagesCount = 0; |
| 1968 | 57 | | manager = owner.messagingManager; |
| 1968 | 58 | | } |
| | 59 | |
|
| 0 | 60 | | public void Start() { enabled = true; } |
| | 61 | |
|
| | 62 | | public void Stop() |
| | 63 | | { |
| 5823 | 64 | | enabled = false; |
| | 65 | |
|
| 5823 | 66 | | if ( msgYieldInstruction is CleanableYieldInstruction cleanableYieldInstruction ) |
| 0 | 67 | | cleanableYieldInstruction.Cleanup(); |
| | 68 | |
|
| 5823 | 69 | | pendingMessagesCount = 0; |
| 5823 | 70 | | } |
| | 71 | |
|
| 3936 | 72 | | public void Dispose() { Stop(); } |
| | 73 | |
|
| | 74 | | public void Enqueue(QueuedSceneMessage message, QueueMode queueMode = QueueMode.Reliable) |
| | 75 | | { |
| 27 | 76 | | bool enqueued = true; |
| | 77 | |
|
| | 78 | | // When removing an entity we have to ensure that the enqueued lossy messages after it are processed and not |
| 27 | 79 | | if (message is QueuedSceneMessage_Scene queuedSceneMessage && queuedSceneMessage.payload is Protocol.RemoveE |
| | 80 | | { |
| 0 | 81 | | List<string> unreliableMessagesToRemove = new List<string>(); |
| 0 | 82 | | foreach (string key in unreliableMessages.Keys) |
| | 83 | | { |
| 0 | 84 | | if (key.Contains(removeEntityPayload.entityId)) //Key of unreliableMessages is a mixture of entityId |
| | 85 | | { |
| 0 | 86 | | unreliableMessagesToRemove.Add(key); |
| | 87 | | } |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | for (int index = 0; index < unreliableMessagesToRemove.Count; index++) |
| | 91 | | { |
| 0 | 92 | | string key = unreliableMessagesToRemove[index]; |
| 0 | 93 | | if (unreliableMessages.ContainsKey(key)) |
| | 94 | | { |
| 0 | 95 | | unreliableMessages.Remove(key); |
| | 96 | | } |
| | 97 | | } |
| | 98 | | } |
| | 99 | |
|
| 27 | 100 | | if (queueMode == QueueMode.Reliable) |
| | 101 | | { |
| 27 | 102 | | message.isUnreliable = false; |
| 27 | 103 | | AddReliableMessage(message); |
| 27 | 104 | | } |
| | 105 | | else |
| | 106 | | { |
| 0 | 107 | | message.isUnreliable = true; |
| | 108 | |
|
| 0 | 109 | | LinkedListNode<QueuedSceneMessage> node = null; |
| | 110 | |
|
| 0 | 111 | | message.unreliableMessageKey = message.tag; |
| | 112 | |
|
| 0 | 113 | | if (unreliableMessages.ContainsKey(message.unreliableMessageKey)) |
| | 114 | | { |
| 0 | 115 | | node = unreliableMessages[message.unreliableMessageKey]; |
| | 116 | |
|
| 0 | 117 | | if (node.List != null) |
| | 118 | | { |
| 0 | 119 | | node.Value = message; |
| 0 | 120 | | enqueued = false; |
| 0 | 121 | | unreliableMessagesReplaced++; |
| | 122 | | } |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | if (enqueued) |
| | 126 | | { |
| 0 | 127 | | node = AddReliableMessage(message); |
| 0 | 128 | | unreliableMessages[message.unreliableMessageKey] = node; |
| | 129 | | } |
| | 130 | | } |
| | 131 | |
|
| 27 | 132 | | if (enqueued) |
| | 133 | | { |
| 27 | 134 | | if (message.type == QueuedSceneMessage.Type.SCENE_MESSAGE) |
| | 135 | | { |
| 0 | 136 | | QueuedSceneMessage_Scene sm = message as QueuedSceneMessage_Scene; |
| 0 | 137 | | ProfilingEvents.OnMessageWillQueue?.Invoke(sm.method); |
| | 138 | | } |
| | 139 | |
|
| 27 | 140 | | if (type == MessagingBusType.INIT) |
| | 141 | | { |
| 27 | 142 | | manager.pendingInitMessagesCount++; |
| | 143 | | } |
| | 144 | |
|
| 27 | 145 | | if (owner != null) |
| | 146 | | { |
| 27 | 147 | | owner.enabled = true; |
| 27 | 148 | | manager.MarkBusesDirty(); |
| | 149 | | } |
| | 150 | | } |
| 27 | 151 | | } |
| | 152 | |
|
| | 153 | | public bool ProcessQueue(float timeBudget, out IEnumerator yieldReturn) |
| | 154 | | { |
| 9 | 155 | | yieldReturn = null; |
| | 156 | |
|
| | 157 | | // Note (Zak): This check is to avoid calling Time.realtimeSinceStartup |
| | 158 | | // unnecessarily because it's pretty slow in JS |
| 9 | 159 | | if (timeBudget <= 0 || !enabled || pendingMessagesCount == 0) |
| 0 | 160 | | return false; |
| | 161 | |
|
| 9 | 162 | | float startTime = Time.realtimeSinceStartup; |
| | 163 | |
|
| 36 | 164 | | while (enabled && pendingMessagesCount > 0 && Time.realtimeSinceStartup - startTime < timeBudget) |
| | 165 | | { |
| 27 | 166 | | QueuedSceneMessage m = pendingMessages.First.Value; |
| | 167 | |
|
| 27 | 168 | | RemoveFirstReliableMessage(); |
| | 169 | |
|
| 27 | 170 | | if (m.isUnreliable) |
| 0 | 171 | | RemoveUnreliableMessage(m); |
| | 172 | |
|
| 27 | 173 | | bool shouldLogMessage = VERBOSE; |
| | 174 | |
|
| 27 | 175 | | switch (m.type) |
| | 176 | | { |
| | 177 | | case QueuedSceneMessage.Type.NONE: |
| | 178 | | break; |
| | 179 | | case QueuedSceneMessage.Type.SCENE_MESSAGE: |
| | 180 | |
|
| 0 | 181 | | if (!(m is QueuedSceneMessage_Scene sceneMessage)) |
| | 182 | | continue; |
| | 183 | |
|
| 0 | 184 | | if (handler.ProcessMessage(sceneMessage, out msgYieldInstruction)) |
| | 185 | | { |
| | 186 | | #if UNITY_EDITOR |
| 0 | 187 | | if (DataStore.i.debugConfig.msgStepByStep) |
| | 188 | | { |
| 0 | 189 | | if (VERBOSE) |
| | 190 | | { |
| 0 | 191 | | LogMessage(m, this, false); |
| 0 | 192 | | shouldLogMessage = false; |
| | 193 | | } |
| | 194 | |
|
| 0 | 195 | | return true; |
| | 196 | | } |
| | 197 | | #endif |
| | 198 | | } |
| | 199 | | else |
| | 200 | | { |
| 0 | 201 | | shouldLogMessage = false; |
| | 202 | | } |
| | 203 | |
|
| 0 | 204 | | OnMessageProcessed(); |
| 0 | 205 | | ProfilingEvents.OnMessageWillDequeue?.Invoke(sceneMessage.method); |
| | 206 | |
|
| 0 | 207 | | if (msgYieldInstruction != null) |
| | 208 | | { |
| 0 | 209 | | processedMessagesCount++; |
| | 210 | |
|
| 0 | 211 | | msgYieldInstruction = null; |
| | 212 | | } |
| | 213 | |
|
| 0 | 214 | | break; |
| | 215 | | case QueuedSceneMessage.Type.LOAD_PARCEL: |
| 26 | 216 | | handler.LoadParcelScenesExecute(m.message); |
| 26 | 217 | | ProfilingEvents.OnMessageWillDequeue?.Invoke("LoadScene"); |
| 0 | 218 | | break; |
| | 219 | | case QueuedSceneMessage.Type.UNLOAD_PARCEL: |
| 1 | 220 | | handler.UnloadParcelSceneExecute(m.message); |
| 1 | 221 | | ProfilingEvents.OnMessageWillDequeue?.Invoke("UnloadScene"); |
| 0 | 222 | | break; |
| | 223 | | case QueuedSceneMessage.Type.UPDATE_PARCEL: |
| 0 | 224 | | handler.UpdateParcelScenesExecute(m.message); |
| 0 | 225 | | ProfilingEvents.OnMessageWillDequeue?.Invoke("UpdateScene"); |
| 0 | 226 | | break; |
| | 227 | | case QueuedSceneMessage.Type.UNLOAD_SCENES: |
| 0 | 228 | | handler.UnloadAllScenes(); |
| 0 | 229 | | ProfilingEvents.OnMessageWillDequeue?.Invoke("UnloadAllScenes"); |
| | 230 | | break; |
| | 231 | | } |
| | 232 | |
|
| 27 | 233 | | OnMessageProcessed(); |
| | 234 | | #if UNITY_EDITOR |
| 27 | 235 | | if (shouldLogMessage) |
| | 236 | | { |
| 0 | 237 | | LogMessage(m, this); |
| | 238 | | } |
| | 239 | | #endif |
| | 240 | | } |
| | 241 | |
|
| 9 | 242 | | return false; |
| | 243 | | } |
| | 244 | |
|
| | 245 | | public void OnMessageProcessed() |
| | 246 | | { |
| 27 | 247 | | processedMessagesCount++; |
| | 248 | |
|
| 27 | 249 | | if (type == MessagingBusType.INIT) |
| | 250 | | { |
| 27 | 251 | | manager.pendingInitMessagesCount--; |
| 27 | 252 | | manager.processedInitMessagesCount++; |
| | 253 | | } |
| 27 | 254 | | } |
| | 255 | |
|
| | 256 | | private LinkedListNode<QueuedSceneMessage> AddReliableMessage(QueuedSceneMessage message) |
| | 257 | | { |
| 27 | 258 | | manager.pendingMessagesCount++; |
| 27 | 259 | | pendingMessagesCount++; |
| 27 | 260 | | return pendingMessages.AddLast(message); |
| | 261 | | } |
| | 262 | |
|
| | 263 | | private void RemoveFirstReliableMessage() |
| | 264 | | { |
| 27 | 265 | | if (pendingMessages.First != null) |
| | 266 | | { |
| 27 | 267 | | pendingMessages.RemoveFirst(); |
| 27 | 268 | | pendingMessagesCount--; |
| 27 | 269 | | manager.pendingMessagesCount--; |
| | 270 | | } |
| 27 | 271 | | } |
| | 272 | |
|
| | 273 | | private void RemoveUnreliableMessage(QueuedSceneMessage message) |
| | 274 | | { |
| 0 | 275 | | if (unreliableMessages.ContainsKey(message.unreliableMessageKey)) |
| 0 | 276 | | unreliableMessages.Remove(message.unreliableMessageKey); |
| 0 | 277 | | } |
| | 278 | |
|
| | 279 | | private void LogMessage(QueuedSceneMessage m, MessagingBus bus, bool logType = true) |
| | 280 | | { |
| 0 | 281 | | string finalTag = WorldStateUtils.TryToGetSceneCoordsID(bus.debugTag); |
| | 282 | |
|
| 0 | 283 | | if (logType) |
| | 284 | | { |
| 0 | 285 | | Debug.Log($"#{bus.processedMessagesCount} ... bus = {finalTag}, id = {bus.type}... processing msg... typ |
| 0 | 286 | | } |
| | 287 | | else |
| | 288 | | { |
| 0 | 289 | | Debug.Log($"#{bus.processedMessagesCount} ... Bus = {finalTag}, id = {bus.type}... processing msg... {m. |
| | 290 | | } |
| 0 | 291 | | } |
| | 292 | | } |
| | 293 | | } |