| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.Profiling; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | public class MessagingController : IDisposable |
| | 11 | | { |
| | 12 | | const char SEPARATOR = '_'; |
| | 13 | |
|
| | 14 | | public enum QueueState |
| | 15 | | { |
| | 16 | | Init, |
| | 17 | | Systems, |
| | 18 | | } |
| | 19 | |
|
| 656 | 20 | | public Dictionary<MessagingBusType, MessagingBus> messagingBuses = new Dictionary<MessagingBusType, MessagingBus |
| | 21 | | public IMessageProcessHandler messageHandler; |
| | 22 | | public string debugTag; |
| 656 | 23 | | public bool enabled = true; |
| | 24 | |
|
| | 25 | | private QueueState currentQueueState; |
| | 26 | |
|
| | 27 | | public readonly MessagingBus initBus; |
| | 28 | | public readonly MessagingBus systemBus; |
| | 29 | | public readonly MessagingBus uiBus; |
| | 30 | |
|
| | 31 | | public IMessagingControllersManager messagingManager; |
| | 32 | |
|
| 656 | 33 | | public MessagingController(IMessagingControllersManager messagingManager, IMessageProcessHandler messageHandler, |
| | 34 | | { |
| 656 | 35 | | this.messagingManager = messagingManager; |
| 656 | 36 | | this.debugTag = debugTag; |
| 656 | 37 | | this.messageHandler = messageHandler; |
| | 38 | |
|
| | 39 | | //TODO(Brian): This is too hacky, most of the controllers won't be using this system. Refactor this in the f |
| 656 | 40 | | uiBus = AddMessageBus(MessagingBusType.UI); |
| 656 | 41 | | initBus = AddMessageBus(MessagingBusType.INIT); |
| 656 | 42 | | systemBus = AddMessageBus(MessagingBusType.SYSTEM); |
| | 43 | |
|
| 656 | 44 | | currentQueueState = QueueState.Init; |
| | 45 | |
|
| 656 | 46 | | StartBus(MessagingBusType.INIT); |
| 656 | 47 | | StartBus(MessagingBusType.UI); |
| 656 | 48 | | } |
| | 49 | |
|
| | 50 | | private MessagingBus AddMessageBus(MessagingBusType type) |
| | 51 | | { |
| 1968 | 52 | | var newMessagingBus = new MessagingBus(type, messageHandler, this); |
| 1968 | 53 | | newMessagingBus.debugTag = debugTag; |
| | 54 | |
|
| 1968 | 55 | | messagingBuses.Add(type, newMessagingBus); |
| 1968 | 56 | | return newMessagingBus; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public void StartBus(MessagingBusType busType) |
| | 60 | | { |
| 1312 | 61 | | if (messagingBuses.ContainsKey(busType)) |
| | 62 | | { |
| 1312 | 63 | | messagingBuses[busType].Start(); |
| | 64 | | } |
| 1312 | 65 | | } |
| | 66 | |
|
| | 67 | | public void StopBus(MessagingBusType busType) |
| | 68 | | { |
| 0 | 69 | | if (messagingBuses.ContainsKey(busType)) |
| | 70 | | { |
| 0 | 71 | | messagingBuses[busType].Stop(); |
| | 72 | | } |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public void Stop() |
| | 76 | | { |
| 1285 | 77 | | using (var iterator = messagingBuses.GetEnumerator()) |
| | 78 | | { |
| 5140 | 79 | | while (iterator.MoveNext()) |
| | 80 | | { |
| 3855 | 81 | | iterator.Current.Value.Stop(); |
| | 82 | | } |
| 1285 | 83 | | } |
| 1285 | 84 | | } |
| | 85 | |
|
| | 86 | | public void Dispose() |
| | 87 | | { |
| 656 | 88 | | using (var iterator = messagingBuses.GetEnumerator()) |
| | 89 | | { |
| 2624 | 90 | | while (iterator.MoveNext()) |
| | 91 | | { |
| 1968 | 92 | | iterator.Current.Value.Dispose(); |
| | 93 | | } |
| 656 | 94 | | } |
| 656 | 95 | | } |
| | 96 | |
|
| 54 | 97 | | public void ForceEnqueue(MessagingBusType busType, QueuedSceneMessage queuedMessage) { messagingBuses[busType].E |
| | 98 | |
|
| | 99 | | public void Enqueue(bool isUiBus, QueuedSceneMessage_Scene queuedMessage, out MessagingBusType busType) |
| | 100 | | { |
| 0 | 101 | | busType = MessagingBusType.NONE; |
| | 102 | |
|
| 0 | 103 | | QueueMode queueMode = QueueMode.Reliable; |
| | 104 | |
|
| | 105 | | // If current scene is the Global Scene, the bus id should be UI |
| 0 | 106 | | if (isUiBus) |
| 0 | 107 | | busType = MessagingBusType.UI; |
| 0 | 108 | | else if (currentQueueState == QueueState.Init) |
| 0 | 109 | | busType = MessagingBusType.INIT; |
| | 110 | | else |
| 0 | 111 | | busType = MessagingBusType.SYSTEM; |
| | 112 | |
|
| | 113 | | // Check if the message type is an EntityComponentCreateOrUpdate |
| 0 | 114 | | if (queuedMessage.payload is Protocol.EntityComponentCreateOrUpdate) |
| | 115 | | { |
| | 116 | | // We need to extract the entityId and the classId from the tag. |
| | 117 | | // The tag format is "entityId_classId", i.e: "E1_2". |
| 0 | 118 | | GetEntityIdAndClassIdFromTag(queuedMessage.tag, out int classId); |
| | 119 | |
|
| | 120 | | // If it is a transform update, the queue mode is Lossy |
| 0 | 121 | | if (classId == (int) CLASS_ID_COMPONENT.TRANSFORM) |
| 0 | 122 | | queueMode = QueueMode.Lossy; |
| 0 | 123 | | } |
| 0 | 124 | | else if (queuedMessage.payload is Protocol.QueryPayload) |
| | 125 | | { |
| 0 | 126 | | busType = MessagingBusType.UI; |
| 0 | 127 | | queueMode = QueueMode.Lossy; |
| 0 | 128 | | } |
| 0 | 129 | | else if (queuedMessage.payload is Protocol.SceneReady) |
| | 130 | | { |
| | 131 | | // When a INIT DONE message is enqueued, the next messages should be |
| | 132 | | // enqueued in SYSTEM message bus, but we don't process them until |
| | 133 | | // scene started has been processed |
| 0 | 134 | | currentQueueState = QueueState.Systems; |
| | 135 | | } |
| | 136 | |
|
| 0 | 137 | | switch (busType) |
| | 138 | | { |
| | 139 | | case MessagingBusType.INIT: |
| 0 | 140 | | initBus.Enqueue(queuedMessage, queueMode); |
| 0 | 141 | | break; |
| | 142 | | case MessagingBusType.SYSTEM: |
| 0 | 143 | | systemBus.Enqueue(queuedMessage, queueMode); |
| 0 | 144 | | break; |
| | 145 | | case MessagingBusType.UI: |
| 0 | 146 | | uiBus.Enqueue(queuedMessage, queueMode); |
| | 147 | | break; |
| | 148 | | } |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | private void GetEntityIdAndClassIdFromTag(string tag, out int classId) |
| | 152 | | { |
| 0 | 153 | | int lastSeparator = tag.LastIndexOf(SEPARATOR); |
| 0 | 154 | | if (!int.TryParse(tag.Substring(lastSeparator + 1), out classId)) |
| 0 | 155 | | Debug.LogError("Couldn't parse classId string to int"); |
| 0 | 156 | | } |
| | 157 | | } |
| | 158 | | } |