| | 1 | | using KernelCommunication; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | |
|
| | 5 | | namespace DCL.CRDT |
| | 6 | | { |
| | 7 | | // Deserialize CRDT binary messages (BigEndian) |
| | 8 | | public static class CRDTDeserializer |
| | 9 | | { |
| | 10 | | public static IEnumerator<object> DeserializeBatch(ReadOnlyMemory<byte> memory) |
| | 11 | | { |
| 14 | 12 | | int position = 0; |
| 14 | 13 | | int remainingBytesToRead = 0; |
| | 14 | |
|
| | 15 | | // While we have a header to read |
| 27 | 16 | | while (position + CrdtConstants.MESSAGE_HEADER_LENGTH <= memory.Length) |
| | 17 | | { |
| 18 | 18 | | int messageLength = ByteUtils.ReadInt32(memory.Span, position); |
| 18 | 19 | | position += 4; |
| | 20 | |
|
| 18 | 21 | | CrdtMessageType messageType = (CrdtMessageType)ByteUtils.ReadInt32(memory.Span, position); |
| 18 | 22 | | position += 4; |
| | 23 | |
|
| | 24 | | // Message length lower than minimal, it's an invalid message |
| 18 | 25 | | if (messageLength <= CrdtConstants.MESSAGE_HEADER_LENGTH) |
| | 26 | | { |
| | 27 | | break; |
| | 28 | | } |
| | 29 | |
|
| | 30 | | // Do we have the bytes computed in the header? |
| 18 | 31 | | remainingBytesToRead = messageLength - CrdtConstants.MESSAGE_HEADER_LENGTH; |
| | 32 | |
|
| 18 | 33 | | if (position + remainingBytesToRead > memory.Length) |
| | 34 | | { |
| | 35 | | break; |
| | 36 | | } |
| | 37 | |
|
| | 38 | | switch (messageType) |
| | 39 | | { |
| | 40 | | case CrdtMessageType.PUT_COMPONENT: |
| 14 | 41 | | yield return DeserializePutComponent(memory, ref position); |
| 12 | 42 | | break; |
| | 43 | |
|
| | 44 | | case CrdtMessageType.DELETE_COMPONENT: |
| 1 | 45 | | yield return DeserializeDeleteComponent(memory, ref position); |
| 1 | 46 | | break; |
| | 47 | |
|
| | 48 | | case CrdtMessageType.DELETE_ENTITY: |
| 0 | 49 | | yield return DeserializeDeleteEntity(memory, ref position); |
| 0 | 50 | | break; |
| | 51 | |
|
| | 52 | | case CrdtMessageType.APPEND_COMPONENT: |
| 0 | 53 | | yield return DeserializeAppendComponent(memory, ref position); |
| 0 | 54 | | break; |
| | 55 | |
|
| | 56 | | default: |
| 0 | 57 | | position += messageLength - CrdtConstants.MESSAGE_HEADER_LENGTH; |
| | 58 | | break; |
| | 59 | | } |
| | 60 | | } |
| 12 | 61 | | } |
| | 62 | |
|
| | 63 | | public static CrdtMessage? DeserializePutComponent(ReadOnlyMemory<byte> memory, ref int memoryPosition) |
| | 64 | | { |
| 16 | 65 | | ReadOnlySpan<byte> memorySpan = memory.Span; |
| | 66 | |
|
| 16 | 67 | | if (memoryPosition + CrdtConstants.CRDT_PUT_COMPONENT_HEADER_LENGTH > memorySpan.Length) |
| | 68 | | { |
| 0 | 69 | | return null; |
| | 70 | | } |
| | 71 | |
|
| 16 | 72 | | long entityId = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 16 | 73 | | memoryPosition += 4; |
| 16 | 74 | | int componentId = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 16 | 75 | | memoryPosition += 4; |
| 16 | 76 | | int timestamp = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 16 | 77 | | memoryPosition += 4; |
| 16 | 78 | | int dataLength = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 16 | 79 | | memoryPosition += 4; |
| | 80 | |
|
| 16 | 81 | | if (memoryPosition + dataLength > memorySpan.Length) |
| | 82 | | { |
| 0 | 83 | | return null; |
| | 84 | | } |
| | 85 | |
|
| 16 | 86 | | byte[] data = null; |
| | 87 | |
|
| 16 | 88 | | if (dataLength > 0) |
| | 89 | | { |
| 15 | 90 | | data = memorySpan.Slice(memoryPosition, dataLength).ToArray(); |
| | 91 | | } |
| | 92 | | else |
| | 93 | | { |
| 1 | 94 | | data = new byte[0]; |
| | 95 | | } |
| | 96 | |
|
| 16 | 97 | | memoryPosition += dataLength; |
| | 98 | |
|
| 16 | 99 | | return new CrdtMessage |
| | 100 | | ( |
| | 101 | | entityId: entityId, |
| | 102 | | componentId: componentId, |
| | 103 | | timestamp: timestamp, |
| | 104 | | data: data, |
| | 105 | | type: CrdtMessageType.PUT_COMPONENT |
| | 106 | | ); |
| | 107 | | } |
| | 108 | |
|
| | 109 | | private static CrdtMessage? DeserializeAppendComponent(ReadOnlyMemory<byte> memory, ref int memoryPosition) |
| | 110 | | { |
| 0 | 111 | | ReadOnlySpan<byte> memorySpan = memory.Span; |
| | 112 | |
|
| 0 | 113 | | if (memoryPosition + CrdtConstants.CRDT_APPEND_COMPONENT_HEADER_LENGTH > memorySpan.Length) |
| | 114 | | { |
| 0 | 115 | | return null; |
| | 116 | | } |
| | 117 | |
|
| 0 | 118 | | long entityId = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 0 | 119 | | memoryPosition += 4; |
| 0 | 120 | | int componentId = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 0 | 121 | | memoryPosition += 4; |
| 0 | 122 | | int timestamp = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 0 | 123 | | memoryPosition += 4; |
| 0 | 124 | | int dataLength = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 0 | 125 | | memoryPosition += 4; |
| | 126 | |
|
| 0 | 127 | | if (memoryPosition + dataLength > memorySpan.Length) |
| | 128 | | { |
| 0 | 129 | | return null; |
| | 130 | | } |
| | 131 | |
|
| 0 | 132 | | byte[] data = null; |
| | 133 | |
|
| 0 | 134 | | if (dataLength > 0) |
| | 135 | | { |
| 0 | 136 | | data = memorySpan.Slice(memoryPosition, dataLength).ToArray(); |
| | 137 | | } |
| | 138 | | else |
| | 139 | | { |
| 0 | 140 | | data = new byte[0]; |
| | 141 | | } |
| | 142 | |
|
| 0 | 143 | | memoryPosition += dataLength; |
| | 144 | |
|
| 0 | 145 | | return new CrdtMessage |
| | 146 | | ( |
| | 147 | | entityId: entityId, |
| | 148 | | componentId: componentId, |
| | 149 | | timestamp: timestamp, |
| | 150 | | data: data, |
| | 151 | | type: CrdtMessageType.APPEND_COMPONENT |
| | 152 | | ); |
| | 153 | | } |
| | 154 | |
|
| | 155 | | private static CrdtMessage? DeserializeDeleteComponent(ReadOnlyMemory<byte> memory, ref int memoryPosition) |
| | 156 | | { |
| 1 | 157 | | ReadOnlySpan<byte> memorySpan = memory.Span; |
| | 158 | |
|
| 1 | 159 | | if (memoryPosition + CrdtConstants.CRDT_DELETE_COMPONENT_HEADER_LENGTH > memorySpan.Length) |
| | 160 | | { |
| 0 | 161 | | return null; |
| | 162 | | } |
| | 163 | |
|
| 1 | 164 | | long entityId = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 1 | 165 | | memoryPosition += 4; |
| 1 | 166 | | int componentId = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 1 | 167 | | memoryPosition += 4; |
| 1 | 168 | | int timestamp = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 1 | 169 | | memoryPosition += 4; |
| | 170 | |
|
| 1 | 171 | | return new CrdtMessage |
| | 172 | | ( |
| | 173 | | entityId: entityId, |
| | 174 | | componentId: componentId, |
| | 175 | | timestamp: timestamp, |
| | 176 | | data: null, |
| | 177 | | type: CrdtMessageType.DELETE_COMPONENT |
| | 178 | | ); |
| | 179 | | } |
| | 180 | |
|
| | 181 | | private static CrdtMessage? DeserializeDeleteEntity(ReadOnlyMemory<byte> memory, ref int memoryPosition) |
| | 182 | | { |
| 0 | 183 | | ReadOnlySpan<byte> memorySpan = memory.Span; |
| | 184 | |
|
| 0 | 185 | | if (memoryPosition + CrdtConstants.CRDT_DELETE_ENTITY_HEADER_LENGTH > memorySpan.Length) |
| | 186 | | { |
| 0 | 187 | | return null; |
| | 188 | | } |
| | 189 | |
|
| 0 | 190 | | long entityId = ByteUtils.ReadInt32(memorySpan, memoryPosition); |
| 0 | 191 | | memoryPosition += 4; |
| | 192 | |
|
| 0 | 193 | | return new CrdtMessage |
| | 194 | | ( |
| | 195 | | entityId: entityId, |
| | 196 | | componentId: 0, |
| | 197 | | timestamp: 0, |
| | 198 | | data: null, |
| | 199 | | type: CrdtMessageType.DELETE_ENTITY |
| | 200 | | ); |
| | 201 | | } |
| | 202 | | } |
| | 203 | | } |