< Summary

Class:DCL.MessagingBus
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Scene/Messaging/MessagingBus.cs
Covered lines:59
Uncovered lines:61
Coverable lines:120
Total lines:293
Line coverage:49.1% (59 of 120)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MessagingBus(...)0%110100%
Start()0%2100%
Stop()0%2.032080%
Dispose()0%110100%
Enqueue(...)0%86.316035%
ProcessQueue(...)0%148.4224040%
OnMessageProcessed()0%220100%
AddReliableMessage(...)0%110100%
RemoveFirstReliableMessage()0%220100%
RemoveUnreliableMessage(...)0%6200%
LogMessage(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Scene/Messaging/MessagingBus.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5using UnityEngine.Assertions;
 6using DCL.Interface;
 7using DCL.Models;
 8using UnityEngine.SceneManagement;
 9
 10namespace 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
 196824        public LinkedList<QueuedSceneMessage> pendingMessages = new LinkedList<QueuedSceneMessage>();
 025        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;
 029        public long processedMessagesCount { get; set; }
 30
 031        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
 196842        Dictionary<string, LinkedListNode<QueuedSceneMessage>> unreliableMessages = new Dictionary<string, LinkedListNod
 43        public int unreliableMessagesReplaced = 0;
 44
 45        public bool enabled;
 46
 047        public float timeBudget { get => renderingIsDisabled ? float.MaxValue : timeBudgetValue; set => timeBudgetValue 
 48
 196849        public MessagingBus(MessagingBusType type, IMessageProcessHandler handler, MessagingController owner)
 50        {
 196851            Assert.IsNotNull(handler, "IMessageHandler can't be null!");
 196852            this.handler = handler;
 196853            this.enabled = false;
 196854            this.type = type;
 196855            this.owner = owner;
 196856            this.pendingMessagesCount = 0;
 196857            manager = owner.messagingManager;
 196858        }
 59
 060        public void Start() { enabled = true; }
 61
 62        public void Stop()
 63        {
 582364            enabled = false;
 65
 582366            if ( msgYieldInstruction is CleanableYieldInstruction cleanableYieldInstruction )
 067                cleanableYieldInstruction.Cleanup();
 68
 582369            pendingMessagesCount = 0;
 582370        }
 71
 393672        public void Dispose() { Stop(); }
 73
 74        public void Enqueue(QueuedSceneMessage message, QueueMode queueMode = QueueMode.Reliable)
 75        {
 2776            bool enqueued = true;
 77
 78            // When removing an entity we have to ensure that the enqueued lossy messages after it are processed and not
 2779            if (message is QueuedSceneMessage_Scene queuedSceneMessage && queuedSceneMessage.payload is Protocol.RemoveE
 80            {
 081                List<string> unreliableMessagesToRemove = new List<string>();
 082                foreach (string key in unreliableMessages.Keys)
 83                {
 084                    if (key.Contains(removeEntityPayload.entityId)) //Key of unreliableMessages is a mixture of entityId
 85                    {
 086                        unreliableMessagesToRemove.Add(key);
 87                    }
 88                }
 89
 090                for (int index = 0; index < unreliableMessagesToRemove.Count; index++)
 91                {
 092                    string key = unreliableMessagesToRemove[index];
 093                    if (unreliableMessages.ContainsKey(key))
 94                    {
 095                        unreliableMessages.Remove(key);
 96                    }
 97                }
 98            }
 99
 27100            if (queueMode == QueueMode.Reliable)
 101            {
 27102                message.isUnreliable = false;
 27103                AddReliableMessage(message);
 27104            }
 105            else
 106            {
 0107                message.isUnreliable = true;
 108
 0109                LinkedListNode<QueuedSceneMessage> node = null;
 110
 0111                message.unreliableMessageKey = message.tag;
 112
 0113                if (unreliableMessages.ContainsKey(message.unreliableMessageKey))
 114                {
 0115                    node = unreliableMessages[message.unreliableMessageKey];
 116
 0117                    if (node.List != null)
 118                    {
 0119                        node.Value = message;
 0120                        enqueued = false;
 0121                        unreliableMessagesReplaced++;
 122                    }
 123                }
 124
 0125                if (enqueued)
 126                {
 0127                    node = AddReliableMessage(message);
 0128                    unreliableMessages[message.unreliableMessageKey] = node;
 129                }
 130            }
 131
 27132            if (enqueued)
 133            {
 27134                if (message.type == QueuedSceneMessage.Type.SCENE_MESSAGE)
 135                {
 0136                    QueuedSceneMessage_Scene sm = message as QueuedSceneMessage_Scene;
 0137                    ProfilingEvents.OnMessageWillQueue?.Invoke(sm.method);
 138                }
 139
 27140                if (type == MessagingBusType.INIT)
 141                {
 27142                    manager.pendingInitMessagesCount++;
 143                }
 144
 27145                if (owner != null)
 146                {
 27147                    owner.enabled = true;
 27148                    manager.MarkBusesDirty();
 149                }
 150            }
 27151        }
 152
 153        public bool ProcessQueue(float timeBudget, out IEnumerator yieldReturn)
 154        {
 9155            yieldReturn = null;
 156
 157            // Note (Zak): This check is to avoid calling Time.realtimeSinceStartup
 158            // unnecessarily because it's pretty slow in JS
 9159            if (timeBudget <= 0 || !enabled || pendingMessagesCount == 0)
 0160                return false;
 161
 9162            float startTime = Time.realtimeSinceStartup;
 163
 36164            while (enabled && pendingMessagesCount > 0 && Time.realtimeSinceStartup - startTime < timeBudget)
 165            {
 27166                QueuedSceneMessage m = pendingMessages.First.Value;
 167
 27168                RemoveFirstReliableMessage();
 169
 27170                if (m.isUnreliable)
 0171                    RemoveUnreliableMessage(m);
 172
 27173                bool shouldLogMessage = VERBOSE;
 174
 27175                switch (m.type)
 176                {
 177                    case QueuedSceneMessage.Type.NONE:
 178                        break;
 179                    case QueuedSceneMessage.Type.SCENE_MESSAGE:
 180
 0181                        if (!(m is QueuedSceneMessage_Scene sceneMessage))
 182                            continue;
 183
 0184                        if (handler.ProcessMessage(sceneMessage, out msgYieldInstruction))
 185                        {
 186#if UNITY_EDITOR
 0187                            if (DataStore.i.debugConfig.msgStepByStep)
 188                            {
 0189                                if (VERBOSE)
 190                                {
 0191                                    LogMessage(m, this, false);
 0192                                    shouldLogMessage = false;
 193                                }
 194
 0195                                return true;
 196                            }
 197#endif
 198                        }
 199                        else
 200                        {
 0201                            shouldLogMessage = false;
 202                        }
 203
 0204                        OnMessageProcessed();
 0205                        ProfilingEvents.OnMessageWillDequeue?.Invoke(sceneMessage.method);
 206
 0207                        if (msgYieldInstruction != null)
 208                        {
 0209                            processedMessagesCount++;
 210
 0211                            msgYieldInstruction = null;
 212                        }
 213
 0214                        break;
 215                    case QueuedSceneMessage.Type.LOAD_PARCEL:
 26216                        handler.LoadParcelScenesExecute(m.message);
 26217                        ProfilingEvents.OnMessageWillDequeue?.Invoke("LoadScene");
 0218                        break;
 219                    case QueuedSceneMessage.Type.UNLOAD_PARCEL:
 1220                        handler.UnloadParcelSceneExecute(m.message);
 1221                        ProfilingEvents.OnMessageWillDequeue?.Invoke("UnloadScene");
 0222                        break;
 223                    case QueuedSceneMessage.Type.UPDATE_PARCEL:
 0224                        handler.UpdateParcelScenesExecute(m.message);
 0225                        ProfilingEvents.OnMessageWillDequeue?.Invoke("UpdateScene");
 0226                        break;
 227                    case QueuedSceneMessage.Type.UNLOAD_SCENES:
 0228                        handler.UnloadAllScenes();
 0229                        ProfilingEvents.OnMessageWillDequeue?.Invoke("UnloadAllScenes");
 230                        break;
 231                }
 232
 27233                OnMessageProcessed();
 234#if UNITY_EDITOR
 27235                if (shouldLogMessage)
 236                {
 0237                    LogMessage(m, this);
 238                }
 239#endif
 240            }
 241
 9242            return false;
 243        }
 244
 245        public void OnMessageProcessed()
 246        {
 27247            processedMessagesCount++;
 248
 27249            if (type == MessagingBusType.INIT)
 250            {
 27251                manager.pendingInitMessagesCount--;
 27252                manager.processedInitMessagesCount++;
 253            }
 27254        }
 255
 256        private LinkedListNode<QueuedSceneMessage> AddReliableMessage(QueuedSceneMessage message)
 257        {
 27258            manager.pendingMessagesCount++;
 27259            pendingMessagesCount++;
 27260            return pendingMessages.AddLast(message);
 261        }
 262
 263        private void RemoveFirstReliableMessage()
 264        {
 27265            if (pendingMessages.First != null)
 266            {
 27267                pendingMessages.RemoveFirst();
 27268                pendingMessagesCount--;
 27269                manager.pendingMessagesCount--;
 270            }
 27271        }
 272
 273        private void RemoveUnreliableMessage(QueuedSceneMessage message)
 274        {
 0275            if (unreliableMessages.ContainsKey(message.unreliableMessageKey))
 0276                unreliableMessages.Remove(message.unreliableMessageKey);
 0277        }
 278
 279        private void LogMessage(QueuedSceneMessage m, MessagingBus bus, bool logType = true)
 280        {
 0281            string finalTag = WorldStateUtils.TryToGetSceneCoordsID(bus.debugTag);
 282
 0283            if (logType)
 284            {
 0285                Debug.Log($"#{bus.processedMessagesCount} ... bus = {finalTag}, id = {bus.type}... processing msg... typ
 0286            }
 287            else
 288            {
 0289                Debug.Log($"#{bus.processedMessagesCount} ... Bus = {finalTag}, id = {bus.type}... processing msg... {m.
 290            }
 0291        }
 292    }
 293}