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