Ejemplo n.º 1
0
        public string ReadString(DataBufferMarker marker = default(DataBufferMarker))
        {
            var encoding = (UTF8Encoding)Encoding.UTF8;

            if (!marker.Valid)
            {
                marker = CreateMarker(CurrReadIndex);
            }

            var strDataLength     = ReadValue <int>(marker);
            var strDataEnd        = ReadValue <int>(marker.GetOffset(sizeof(int) * 1));
            var strExpectedLength = ReadValue <int>(marker.GetOffset(sizeof(int) * 2));
            var strDataStart      = GetReadIndex(marker.GetOffset(sizeof(int) * 3));

            if (strDataLength <= 0)
            {
                if (strDataLength < 0)
                {
                    throw new Exception("No string found, maybe you are reading at the wrong location or you've done a bad write?");
                }

                return(string.Empty);
            }

            var str = encoding.GetString(DataPtr + strDataStart, Math.Min(strDataEnd - strDataStart, strDataLength));

            CurrReadIndex = strDataEnd;

            if (str.Length != strExpectedLength)
            {
                return(str.Substring(0, strExpectedLength));
            }

            return(str);
        }
Ejemplo n.º 2
0
        public ulong ReadDynInteger(DataBufferMarker marker = default(DataBufferMarker))
        {
            var byteCount = ReadValue <byte>();

            if (byteCount == 0)
            {
                return(0);
            }
            if (byteCount == sizeof(byte))
            {
                return(ReadValue <byte>());
            }
            if (byteCount == sizeof(ushort))
            {
                return(ReadValue <ushort>());
            }
            if (byteCount == sizeof(uint))
            {
                return(ReadValue <uint>());
            }
            if (byteCount == sizeof(ulong))
            {
                return(ReadValue <ulong>());
            }

            throw new InvalidOperationException($"Expected byte count range: [{sizeof(byte)}..{sizeof(ulong)}], received: {byteCount}");
        }
Ejemplo n.º 3
0
        public int GetReadIndex(DataBufferMarker marker)
        {
            var readIndex = !marker.Valid ? CurrReadIndex : marker.Index;

            if (readIndex >= Length)
            {
                throw new IndexOutOfRangeException("p1");
            }

            return(readIndex);
        }
Ejemplo n.º 4
0
        public Span <T> ReadSpan <T>(int size, DataBufferMarker marker = default)
            where T : struct
        {
            size *= Unsafe.SizeOf <T>();
            var readIndex = GetReadIndexAndSetNew(marker, size);

            // Set it for later usage
            CurrReadIndex = readIndex + size;

            return(MemoryMarshal.Cast <byte, T>(Span.Slice(readIndex, size)));
        }
Ejemplo n.º 5
0
        public string ReadString(DataBufferMarker marker = default)
        {
            var length = ReadValue <int>();

            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }

            return(new string(ReadSpan <char>(length)));
        }
Ejemplo n.º 6
0
        public T ReadValue <T>(DataBufferMarker marker = default(DataBufferMarker))
            where T : struct
        {
            var val       = default(T);
            var size      = Unsafe.SizeOf <T>();
            var readIndex = GetReadIndexAndSetNew(marker, size);

            // Set it for later usage
            CurrReadIndex = readIndex + size;
            // Read the value
            ReadUnsafe((byte *)Unsafe.AsPointer(ref val), readIndex, size);

            return(val);
        }
Ejemplo n.º 7
0
        public int GetReadIndexAndSetNew(DataBufferMarker marker, int size)
        {
            var readIndex = !marker.Valid ? CurrReadIndex : marker.Index;

            if (readIndex >= Length)
            {
                throw new IndexOutOfRangeException($"p1 r={readIndex} >= l={Length}");
            }

            CurrReadIndex = readIndex + size;
            if (CurrReadIndex > Length)
            {
                throw new IndexOutOfRangeException("p2");
            }

            return(readIndex);
        }
Ejemplo n.º 8
0
        public TCharBuffer ReadBuffer <TCharBuffer>(DataBufferMarker marker = default(DataBufferMarker))
            where TCharBuffer : struct, ICharBuffer
        {
            var length = ReadValue <int>();

            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }

            var buffer = new TCharBuffer {
                Length = length
            };

            ReadDataSafe(buffer.Span.Slice(0, length));

            return(buffer);
        }
Ejemplo n.º 9
0
        public int GetReadIndexAndSetNew(DataBufferMarker marker, int size)
        {
            if (size < 0)
            {
                throw new InvalidOperationException();
            }

            var readIndex = !marker.Valid ? CurrReadIndex : marker.Index;

            if (readIndex >= Length)
            {
                throw new IndexOutOfRangeException($"p1 r={readIndex} >= l={Length}");
            }

            CurrReadIndex = readIndex + size;
            if (CurrReadIndex > Length)
            {
                throw new IndexOutOfRangeException($"p2 {CurrReadIndex} ({readIndex} + {size}) > {Length}");
            }

            return(readIndex);
        }
Ejemplo n.º 10
0
 public T ReadValue <T>(DataBufferMarker marker = default(DataBufferMarker))
     where T : struct
 {
     return(ReadSpan <T>(1, marker)[0]);
 }
Ejemplo n.º 11
0
 public void ReadDataSafe <T>(Span <T> span, DataBufferMarker marker = default)
     where T : struct
 {
     ReadSpan <T>(span.Length).CopyTo(span);
 }