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