< Summary

Class:DCL.CRDT.CRDTDeserializer
Assembly:DCL.CRDTProtocol.Deserializer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/CRDTProtocol/Deserializer/CRDTDeserializer.cs
Covered lines:41
Uncovered lines:33
Coverable lines:74
Total lines:203
Line coverage:55.4% (41 of 74)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:5
Method coverage:60% (3 of 5)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DeserializeBatch()0%17.0614075%
DeserializePutComponent(...)0%4.024089.47%
DeserializeAppendComponent(...)0%20400%
DeserializeDeleteComponent(...)0%22090%
DeserializeDeleteEntity(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/CRDTProtocol/Deserializer/CRDTDeserializer.cs

#LineLine coverage
 1using KernelCommunication;
 2using System;
 3using System.Collections.Generic;
 4
 5namespace 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        {
 1412            int position = 0;
 1413            int remainingBytesToRead = 0;
 14
 15            // While we have a header to read
 2716            while (position + CrdtConstants.MESSAGE_HEADER_LENGTH <= memory.Length)
 17            {
 1818                int messageLength = ByteUtils.ReadInt32(memory.Span, position);
 1819                position += 4;
 20
 1821                CrdtMessageType messageType = (CrdtMessageType)ByteUtils.ReadInt32(memory.Span, position);
 1822                position += 4;
 23
 24                // Message length lower than minimal, it's an invalid message
 1825                if (messageLength <= CrdtConstants.MESSAGE_HEADER_LENGTH)
 26                {
 27                    break;
 28                }
 29
 30                // Do we have the bytes computed in the header?
 1831                remainingBytesToRead = messageLength - CrdtConstants.MESSAGE_HEADER_LENGTH;
 32
 1833                if (position + remainingBytesToRead > memory.Length)
 34                {
 35                    break;
 36                }
 37
 38                switch (messageType)
 39                {
 40                    case CrdtMessageType.PUT_COMPONENT:
 1441                        yield return DeserializePutComponent(memory, ref position);
 1242                        break;
 43
 44                    case CrdtMessageType.DELETE_COMPONENT:
 145                        yield return DeserializeDeleteComponent(memory, ref position);
 146                        break;
 47
 48                    case CrdtMessageType.DELETE_ENTITY:
 049                        yield return DeserializeDeleteEntity(memory, ref position);
 050                        break;
 51
 52                    case CrdtMessageType.APPEND_COMPONENT:
 053                        yield return DeserializeAppendComponent(memory, ref position);
 054                        break;
 55
 56                    default:
 057                        position += messageLength - CrdtConstants.MESSAGE_HEADER_LENGTH;
 58                        break;
 59                }
 60            }
 1261        }
 62
 63        public static CrdtMessage? DeserializePutComponent(ReadOnlyMemory<byte> memory, ref int memoryPosition)
 64        {
 1665            ReadOnlySpan<byte> memorySpan = memory.Span;
 66
 1667            if (memoryPosition + CrdtConstants.CRDT_PUT_COMPONENT_HEADER_LENGTH > memorySpan.Length)
 68            {
 069                return null;
 70            }
 71
 1672            long entityId = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 1673            memoryPosition += 4;
 1674            int componentId = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 1675            memoryPosition += 4;
 1676            int timestamp = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 1677            memoryPosition += 4;
 1678            int dataLength = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 1679            memoryPosition += 4;
 80
 1681            if (memoryPosition + dataLength > memorySpan.Length)
 82            {
 083                return null;
 84            }
 85
 1686            byte[] data = null;
 87
 1688            if (dataLength > 0)
 89            {
 1590                data = memorySpan.Slice(memoryPosition, dataLength).ToArray();
 91            }
 92            else
 93            {
 194                data = new byte[0];
 95            }
 96
 1697            memoryPosition += dataLength;
 98
 1699            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        {
 0111            ReadOnlySpan<byte> memorySpan = memory.Span;
 112
 0113            if (memoryPosition + CrdtConstants.CRDT_APPEND_COMPONENT_HEADER_LENGTH > memorySpan.Length)
 114            {
 0115                return null;
 116            }
 117
 0118            long entityId = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 0119            memoryPosition += 4;
 0120            int componentId = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 0121            memoryPosition += 4;
 0122            int timestamp = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 0123            memoryPosition += 4;
 0124            int dataLength = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 0125            memoryPosition += 4;
 126
 0127            if (memoryPosition + dataLength > memorySpan.Length)
 128            {
 0129                return null;
 130            }
 131
 0132            byte[] data = null;
 133
 0134            if (dataLength > 0)
 135            {
 0136                data = memorySpan.Slice(memoryPosition, dataLength).ToArray();
 137            }
 138            else
 139            {
 0140                data = new byte[0];
 141            }
 142
 0143            memoryPosition += dataLength;
 144
 0145            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        {
 1157            ReadOnlySpan<byte> memorySpan = memory.Span;
 158
 1159            if (memoryPosition + CrdtConstants.CRDT_DELETE_COMPONENT_HEADER_LENGTH > memorySpan.Length)
 160            {
 0161                return null;
 162            }
 163
 1164            long entityId = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 1165            memoryPosition += 4;
 1166            int componentId = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 1167            memoryPosition += 4;
 1168            int timestamp = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 1169            memoryPosition += 4;
 170
 1171            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        {
 0183            ReadOnlySpan<byte> memorySpan = memory.Span;
 184
 0185            if (memoryPosition + CrdtConstants.CRDT_DELETE_ENTITY_HEADER_LENGTH > memorySpan.Length)
 186            {
 0187                return null;
 188            }
 189
 0190            long entityId = ByteUtils.ReadInt32(memorySpan, memoryPosition);
 0191            memoryPosition += 4;
 192
 0193            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}