| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.CRDT; |
| | 6 | | using DCL.ECSRuntime; |
| | 7 | | using RPC; |
| | 8 | |
|
| | 9 | | public class ComponentCrdtWriteSystem : IDisposable |
| | 10 | | { |
| | 11 | | private class MessageData |
| | 12 | | { |
| | 13 | | public string sceneId; |
| | 14 | | public long entityId; |
| | 15 | | public int componentId; |
| | 16 | | public byte[] data; |
| | 17 | | public ECSComponentWriteType writeType; |
| | 18 | | } |
| | 19 | |
|
| | 20 | | private readonly RPCContext rpcContext; |
| | 21 | | private readonly ISceneController sceneController; |
| | 22 | | private readonly IWorldState worldState; |
| | 23 | |
|
| 5 | 24 | | private readonly Dictionary<string, CRDTProtocol> outgoingCrdt = new Dictionary<string, CRDTProtocol>(60); |
| 5 | 25 | | private readonly Queue<MessageData> queuedMessages = new Queue<MessageData>(60); |
| 5 | 26 | | private readonly Queue<MessageData> messagesPool = new Queue<MessageData>(60); |
| | 27 | |
|
| 5 | 28 | | public ComponentCrdtWriteSystem(IWorldState worldState, ISceneController sceneController, RPCContext rpcContext) |
| | 29 | | { |
| 5 | 30 | | this.sceneController = sceneController; |
| 5 | 31 | | this.rpcContext = rpcContext; |
| 5 | 32 | | this.worldState = worldState; |
| | 33 | |
|
| 5 | 34 | | sceneController.OnSceneRemoved += OnSceneRemoved; |
| 5 | 35 | | } |
| | 36 | |
|
| | 37 | | public void Dispose() |
| | 38 | | { |
| 0 | 39 | | sceneController.OnSceneRemoved -= OnSceneRemoved; |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public void WriteMessage(string sceneId, long entityId, int componentId, byte[] data, ECSComponentWriteType writeTyp |
| | 43 | | { |
| 5 | 44 | | MessageData messageData = messagesPool.Count > 0 ? messagesPool.Dequeue() : new MessageData(); |
| | 45 | |
|
| 5 | 46 | | messageData.sceneId = sceneId; |
| 5 | 47 | | messageData.entityId = entityId; |
| 5 | 48 | | messageData.componentId = componentId; |
| 5 | 49 | | messageData.data = data; |
| 5 | 50 | | messageData.writeType = writeType; |
| | 51 | |
|
| 5 | 52 | | queuedMessages.Enqueue(messageData); |
| 5 | 53 | | } |
| | 54 | |
|
| | 55 | | public void LateUpdate() |
| | 56 | | { |
| 5 | 57 | | int messagesCount = queuedMessages.Count; |
| | 58 | |
|
| 5 | 59 | | if (messagesCount == 0) |
| | 60 | | { |
| 0 | 61 | | return; |
| | 62 | | } |
| | 63 | |
|
| 20 | 64 | | for (int i = 0; i < messagesCount; i++) |
| | 65 | | { |
| 5 | 66 | | var message = queuedMessages.Dequeue(); |
| 5 | 67 | | messagesPool.Enqueue(message); |
| | 68 | |
|
| 5 | 69 | | if (!worldState.TryGetScene(message.sceneId, out IParcelScene scene)) |
| | 70 | | continue; |
| | 71 | |
|
| 5 | 72 | | CRDTMessage crdt = scene.crdtExecutor.crdtProtocol.Create((int)message.entityId, message.componentId, messag |
| | 73 | |
|
| 5 | 74 | | if (message.writeType.HasFlag(ECSComponentWriteType.SEND_TO_LOCAL)) |
| | 75 | | { |
| 2 | 76 | | scene.crdtExecutor.Execute(crdt); |
| 2 | 77 | | } |
| | 78 | | else |
| | 79 | | { |
| 3 | 80 | | scene.crdtExecutor.crdtProtocol.ProcessMessage(crdt); |
| | 81 | | } |
| | 82 | |
|
| 5 | 83 | | if (message.writeType.HasFlag(ECSComponentWriteType.SEND_TO_SCENE)) |
| | 84 | | { |
| 3 | 85 | | if (!outgoingCrdt.TryGetValue(message.sceneId, out CRDTProtocol sceneCrdtState)) |
| | 86 | | { |
| 3 | 87 | | sceneCrdtState = new CRDTProtocol(); |
| 3 | 88 | | outgoingCrdt[message.sceneId] = sceneCrdtState; |
| | 89 | | } |
| | 90 | |
|
| 3 | 91 | | sceneCrdtState.ProcessMessage(crdt); |
| | 92 | |
|
| 3 | 93 | | if (!rpcContext.crdtContext.scenesOutgoingCrdts.ContainsKey(message.sceneId)) |
| | 94 | | { |
| 3 | 95 | | rpcContext.crdtContext.scenesOutgoingCrdts.Add(message.sceneId, sceneCrdtState); |
| | 96 | | } |
| | 97 | | } |
| | 98 | | } |
| 5 | 99 | | } |
| | 100 | |
|
| | 101 | | private void OnSceneRemoved(IParcelScene scene) |
| | 102 | | { |
| 0 | 103 | | string sceneId = scene.sceneData.id; |
| 0 | 104 | | outgoingCrdt.Remove(sceneId); |
| 0 | 105 | | rpcContext.crdtContext.scenesOutgoingCrdts.Remove(sceneId); |
| 0 | 106 | | } |
| | 107 | | } |