| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | |
|
| | 4 | | namespace DCL.CRDT |
| | 5 | | { |
| | 6 | | public class CRDTProtocol |
| | 7 | | { |
| 99 | 8 | | internal readonly List<CRDTMessage> state = new List<CRDTMessage>(); |
| 99 | 9 | | private readonly Dictionary<int, Dictionary<int, int>> stateIndexer = new Dictionary<int, Dictionary<int, int>>( |
| | 10 | |
|
| | 11 | | private bool clearOnUpdated = false; |
| | 12 | |
|
| | 13 | | public CRDTMessage ProcessMessage(CRDTMessage message) |
| | 14 | | { |
| 33 | 15 | | if (clearOnUpdated) |
| | 16 | | { |
| 0 | 17 | | Clear(); |
| | 18 | | } |
| | 19 | |
|
| 33 | 20 | | TryGetState(message.key1, message.key2, out CRDTMessage storedMessage); |
| | 21 | |
|
| | 22 | | // The received message is > than our current value, update our state. |
| 33 | 23 | | if (storedMessage == null || storedMessage.timestamp < message.timestamp) |
| | 24 | | { |
| 33 | 25 | | return UpdateState(message.key1, message.key2, message.data, message.timestamp); |
| | 26 | | } |
| | 27 | |
|
| | 28 | | // Outdated Message. Resend our state message through the wire. |
| 0 | 29 | | if (storedMessage.timestamp > message.timestamp) |
| | 30 | | { |
| 0 | 31 | | return storedMessage; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | // Same data, same timestamp. Weirdo echo message. |
| 0 | 35 | | if (IsSameData(storedMessage.data, message.data)) |
| | 36 | | { |
| 0 | 37 | | return storedMessage; |
| | 38 | | } |
| | 39 | |
|
| | 40 | | // Race condition, same timestamp diff data. Should keep stored data? |
| 0 | 41 | | if (CompareData(storedMessage.data, message.data)) |
| | 42 | | { |
| 0 | 43 | | return storedMessage; |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | return UpdateState(message.key1, message.key2, message.data, message.timestamp); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | public CRDTMessage GetState(int key1, int key2) |
| | 50 | | { |
| 31 | 51 | | TryGetState(key1, key2, out CRDTMessage crdtMessage); |
| 31 | 52 | | return crdtMessage; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public bool TryGetState(int key1, int key2, out CRDTMessage crdtMessage) |
| | 56 | | { |
| 74 | 57 | | if (stateIndexer.TryGetValue(key1, out Dictionary<int, int> innerDictionary)) |
| | 58 | | { |
| 16 | 59 | | if (innerDictionary.TryGetValue(key2, out int index)) |
| | 60 | | { |
| 12 | 61 | | crdtMessage = state[index]; |
| 12 | 62 | | return true; |
| | 63 | | } |
| | 64 | | } |
| 62 | 65 | | crdtMessage = null; |
| 62 | 66 | | return false; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | public IReadOnlyList<CRDTMessage> GetState() |
| | 70 | | { |
| 0 | 71 | | return state; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | public CRDTMessage Create(int entityId, int componentId, byte[] data) |
| | 75 | | { |
| 10 | 76 | | var result = new CRDTMessage() |
| | 77 | | { |
| | 78 | | key1 = entityId, |
| | 79 | | key2 = componentId, |
| | 80 | | data = data, |
| | 81 | | timestamp = 0 |
| | 82 | | }; |
| 10 | 83 | | if (TryGetState(result.key1, result.key2, out CRDTMessage storedMessage)) |
| | 84 | | { |
| 1 | 85 | | result.timestamp = storedMessage.timestamp + 1; |
| | 86 | | } |
| 10 | 87 | | return result; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | public void Clear() |
| | 91 | | { |
| 0 | 92 | | state.Clear(); |
| 0 | 93 | | stateIndexer.Clear(); |
| 0 | 94 | | clearOnUpdated = false; |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void ClearOnUpdated() |
| | 98 | | { |
| 0 | 99 | | clearOnUpdated = true; |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | private CRDTMessage UpdateState(int key1, int key2, object data, long remoteTimestamp) |
| | 103 | | { |
| 33 | 104 | | long stateTimeStamp = 0; |
| 33 | 105 | | int crdtStateIndex = 0; |
| 33 | 106 | | bool stateExists = false; |
| | 107 | |
|
| 33 | 108 | | if (stateIndexer.TryGetValue(key1, out Dictionary<int, int> innerDictionary)) |
| | 109 | | { |
| 6 | 110 | | stateExists = innerDictionary.TryGetValue(key2, out crdtStateIndex); |
| | 111 | | } |
| | 112 | |
|
| 33 | 113 | | if (stateExists) |
| | 114 | | { |
| 4 | 115 | | stateTimeStamp = state[crdtStateIndex].timestamp; |
| | 116 | | } |
| | 117 | |
|
| 33 | 118 | | long timestamp = Math.Max(remoteTimestamp, stateTimeStamp); |
| 33 | 119 | | var newMessageState = new CRDTMessage() |
| | 120 | | { |
| | 121 | | key1 = key1, |
| | 122 | | key2 = key2, |
| | 123 | | timestamp = timestamp, |
| | 124 | | data = data |
| | 125 | | }; |
| | 126 | |
|
| 33 | 127 | | if (stateExists) |
| | 128 | | { |
| 4 | 129 | | state[crdtStateIndex] = newMessageState; |
| 4 | 130 | | } |
| | 131 | | else |
| | 132 | | { |
| 29 | 133 | | state.Add(newMessageState); |
| 29 | 134 | | int newStateIndex = state.Count - 1; |
| 29 | 135 | | if (innerDictionary != null) |
| | 136 | | { |
| 2 | 137 | | innerDictionary.Add(key2, newStateIndex); |
| 2 | 138 | | } |
| | 139 | | else |
| | 140 | | { |
| 27 | 141 | | stateIndexer[key1] = new Dictionary<int, int>() { { key2, newStateIndex } }; |
| | 142 | | } |
| | 143 | | } |
| | 144 | |
|
| 33 | 145 | | return newMessageState; |
| | 146 | | } |
| | 147 | |
|
| | 148 | | internal static bool IsSameData(object a, object b) |
| | 149 | | { |
| 0 | 150 | | if (a == b) |
| | 151 | | { |
| 0 | 152 | | return true; |
| | 153 | | } |
| | 154 | |
|
| 0 | 155 | | if (a is byte[] bytesA && b is byte[] bytesB) |
| | 156 | | { |
| 0 | 157 | | if (bytesA.Length != bytesB.Length) |
| | 158 | | { |
| 0 | 159 | | return false; |
| | 160 | | } |
| | 161 | |
|
| 0 | 162 | | for (int i = 0; i < bytesA.Length; i++) |
| | 163 | | { |
| 0 | 164 | | if (bytesA[i] != bytesB[i]) |
| | 165 | | { |
| 0 | 166 | | return false; |
| | 167 | | } |
| | 168 | | } |
| 0 | 169 | | return true; |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | if (a is string strA && b is string strB) |
| | 173 | | { |
| 0 | 174 | | return String.Compare(strA, strB, StringComparison.Ordinal) == 0; |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | return false; |
| | 178 | | } |
| | 179 | |
|
| | 180 | | private static bool CompareData(object a, object b) |
| | 181 | | { |
| 0 | 182 | | if (a is byte[] bytesA && b is byte[] bytesB) |
| | 183 | | { |
| 0 | 184 | | return bytesA.Length > bytesB.Length; |
| | 185 | | } |
| | 186 | |
|
| 0 | 187 | | if (a is int numberA && b is int numberB) |
| | 188 | | { |
| 0 | 189 | | return numberA > numberB; |
| | 190 | | } |
| | 191 | |
|
| 0 | 192 | | if (a is string strA && b is string strB) |
| | 193 | | { |
| 0 | 194 | | return String.Compare(strA, strB, StringComparison.Ordinal) > 0; |
| | 195 | | } |
| | 196 | |
|
| 0 | 197 | | return true; |
| | 198 | | } |
| | 199 | | } |
| | 200 | | } |