Exemple #1
0
        private static IPEndPoint ReadEndPoint(BufferReader reader)
        {
            var ipBytes = reader.ReadBytes(4);
            var port = ReadNetworkOrderShort(reader);

            return new IPEndPoint(new IPAddress(ipBytes.ToArray()), port);
        }
        public void ReadsEnums()
        {
            var reader = new BufferReader(new byte[] { 1, 2, 0, 4, 0, 0, 0 });

            Assert.AreEqual(TestEnum.One, reader.ReadEnum<TestEnum>());
            Assert.AreEqual(TestEnum.Two, reader.ReadEnum<TestEnum, ushort>());
            Assert.AreEqual(TestEnum.Four, reader.ReadEnum<TestEnum, int>());
        }
        public void ReadsSequentiallyAndGenerically()
        {
            var reader = new BufferReader(new byte[] { 10, 0, 10, 67, 65, 75, 69, 00, 00, 2, 2, 0 });

            Assert.AreEqual(10, reader.Read<byte>());
            Assert.AreEqual(2560, reader.Read<ushort>());
            Assert.AreEqual("CAKE", reader.Read<string>());
            Assert.AreEqual("", reader.Read<string>());
            Assert.AreEqual(514, reader.Read<ushort>());
        }
        public void ReadsSequentially()
        {
            var reader = new BufferReader(new byte[] { 10, 0, 10, 67, 65, 75, 69, 00, 00, 2, 2, 0 });

            Assert.AreEqual(10, reader.ReadByte());
            Assert.AreEqual(2560, reader.ReadShort());
            Assert.AreEqual("CAKE", reader.ReadString());
            Assert.AreEqual("", reader.ReadString());
            Assert.AreEqual(514, reader.ReadShort());
        }
        public void ReadsUtf8Chars()
        {
            var reader = new BufferReader(new byte[] { 65, 231, 140, 171, 66 });

            Assert.AreEqual('A', reader.ReadChar());
            Assert.AreEqual(1, reader.CurrentPosition);

            Assert.AreEqual('猫', reader.ReadChar());
            Assert.AreEqual(4, reader.CurrentPosition);

            Assert.AreEqual('B', reader.ReadChar());
        }
        public async Task<string> ReadStringAsync()
        {
            var byteEnumerables = new List<IEnumerable<byte>>();

            while (!_reader.IsStringTerminated())
            {
                byteEnumerables.Add(_reader.ReadBytes(_reader.Remaining));
                _reader = await _sequelRequestAsyncFunc();
            }

            byteEnumerables.Add(_reader.ReadUntil(x => x != 0x00));
            _reader.Skip(1);

            return Encoding.UTF8.GetString(byteEnumerables.SelectMany(x => x).ToArray());
        }
Exemple #7
0
        public static void TestByteBufferOptimisation()
        {
            const int numStrings = 100_000;

            var strings = Enumerable.Range(0, numStrings).Select(i => i.ToString()).ToArray();

            var cancel = new CancellationTokenSource();
            var task   = Task.Run(() =>
            {
                while (!cancel.IsCancellationRequested)
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    Thread.Sleep(1);
                }
            });

            using (var buffer = new ResizableBuffer())
            {
                using (var outStream = new BufferOutputStream(buffer))
                {
                    using var fileWriter = new ParquetFileWriter(outStream, new Column[] { new Column <string>("Name") });

                    using (var groupWriter = fileWriter.AppendRowGroup())
                    {
                        using var columnWriter = groupWriter.NextColumn().LogicalWriter <string>();

                        // Strings to byte arrays memory pooling is done by the ByteBuffer class.
                        // If something is fishy there (e.g. bad memory ownership wrt the GC),
                        // we expect to see consequences here if we write enough strings.
                        // It's not bullet proof, but it has found a few issues.
                        columnWriter.WriteBatch(strings);
                    }

                    fileWriter.Close();
                }

                using var inStream     = new BufferReader(buffer);
                using var fileReader   = new ParquetFileReader(inStream);
                using var groupReader  = fileReader.RowGroup(0);
                using var columnReader = groupReader.Column(0).LogicalReader <string>();

                Assert.AreEqual(strings, columnReader.ReadAll(numStrings));
            }

            cancel.Cancel();
            task.Wait();
        }
        static void Main(string[] args)
        {
            var array = new byte[16];

            var writer = array.CreateWriter();

            writer.WriteBigEndian(18);
            writer.WriteBigEndian(2.01f);

            var reader = new BufferReader(array);

            reader.ReadBigEndian(out int v1);
            reader.ReadBigEndian(out float v2);

            Console.WriteLine("Hello World!");
        }
        /// <summary>
        /// Deserialized byte array to a TagActionData instance
        /// </summary>
        /// <param name="buffer">Byte array contains data of an ActionData instance.</param>
        /// <returns>Bytes count that deserialized in buffer.</returns>
        public uint Deserialize(byte[] buffer)
        {
            BufferReader bufferReader = new BufferReader(buffer);
            PropertyTag  propertyTag  = new PropertyTag
            {
                PropertyType = bufferReader.ReadUInt16(),
                PropertyId   = bufferReader.ReadUInt16()
            };

            this.PropertyTag = propertyTag;
            uint size = bufferReader.Position;

            this.PropertyValue = AdapterHelper.ReadValueByType(this.PropertyTag.PropertyType, bufferReader.ReadToEnd());
            size += (uint)this.PropertyValue.Length;
            return(size);
        }
Exemple #10
0
        /// <inheritdoc/>
        public void Deserialize(BufferReader reader, ref Image target, SerializationContext context)
        {
            Shared <EncodedImage> encodedImage = null;

            Serializer.Deserialize(reader, ref encodedImage, context);
            using (var image = ImagePool.GetOrCreate(encodedImage.Resource.Width, encodedImage.Resource.Height, Imaging.PixelFormat.BGR_24bpp))
            {
                ImageDecoder.DecodeTo(encodedImage.Resource, image.Resource);
                target = image.Resource.DeepClone();
            }

            if (encodedImage != null)
            {
                encodedImage.Dispose();
            }
        }
Exemple #11
0
            public override bool Read()
            {
                if (isFinished)
                {
                    return(false);
                }
                hasCurrentRow = BufferReader != null?BufferReader.Read() : DataReader.Read();

                if (!hasCurrentRow)
                {
                    isFinished = true;
                    session.Finish(this);
                }
                hasRead = true;
                return(hasCurrentRow);
            }
        public void ShouldConsumeAllIEnumerables()
        {
            var br = new BufferReader<string>();
            var strings = new String[] { "Peter", "Have", "A", "Buffering", "Reader" };

            br.BeginRead(strings);
            var count = 0;
            while (!br.AllIsRead)
            {
                if (br.Pop() != null)
                {
                    count++;
                }
            }
            Assert.That(count, Is.EqualTo(5));
        }
    static void ParseInt32BufferReaderRaw()
    {
        foreach (var iteration in Benchmark.Iterations)
        {
            var buffer = new ReadOnlyBuffer <byte>(s_data);
            var reader = BufferReader.Create(buffer);

            using (iteration.StartMeasurement())
            {
                while (Utf8Parser.TryParse(reader.CurrentSegment.Slice(reader.ConsumedBytes), out int value, out int consumed))
                {
                    reader.Advance(consumed + 1);
                }
            }
        }
    }
Exemple #14
0
    static void ParseInt32ReadableBufferReader()
    {
        foreach (var iteration in Benchmark.Iterations)
        {
            var buffer = new ReadOnlyBuffer(s_data);
            var reader = BufferReader.Create(buffer);

            using (iteration.StartMeasurement())
            {
                while (Utf8Parser.TryParse(reader.Span.Slice(reader.ConsumedBytes), out int value, out int consumed))
                {
                    reader.Skip(consumed + 1);
                }
            }
        }
    }
Exemple #15
0
        /// <summary>
        /// Read a TaggedPropertyValue structure from buffer.
        /// </summary>
        /// <param name="buffer">Buffer contain TaggedPropertyValue instance</param>
        /// <returns>A TaggedPropertyvalue structure</returns>
        public static TaggedPropertyValue ReadTaggedProperty(byte[] buffer)
        {
            TaggedPropertyValue tagValue     = new TaggedPropertyValue();
            BufferReader        bufferReader = new BufferReader(buffer);

            PropertyTag newPropertyTag = new PropertyTag
            {
                PropertyType = bufferReader.ReadUInt16(),
                PropertyId   = bufferReader.ReadUInt16()
            };

            tagValue.PropertyTag = newPropertyTag;
            tagValue.Value       = ReadValueByType(tagValue.PropertyTag.PropertyType, bufferReader.ReadToEnd());

            return(tagValue);
        }
Exemple #16
0
        public static object Process(BufferReader reader)
        {
            byte tmpFirst = 0, tmpSecond = 0;

            reader.Read(ref tmpFirst).Read(ref tmpSecond);
            ushort tmpOpcode = tmpFirst;

            tmpOpcode <<= 8;
            tmpOpcode  |= tmpSecond;

            Type tmpType = sCommandOpcode2TypeDict.GetValueByKey(tmpOpcode);

            if (null == tmpType)
            {
                Logger.LogErrorFormat("找不到相应的协议包. first:{0} second:{1}", tmpFirst, tmpSecond);
                return(null);
            }

            object tmpCommand = null;

            try
            {
                tmpCommand = Deserialize(reader, tmpType);
            }
            catch (Exception ex)
            {
                Logger.LogErrorFormat("协议反序列化异常. type:[0]  异常信息:{1}", tmpType, ex.Message);
            }

            if (null != tmpCommand)
            {
                List <MethodInfo> tmpMethodInfoList = null;

                if (sOpcode2MethodInfosDict.TryGetValue(tmpOpcode, out tmpMethodInfoList))
                {
                    sParameterObjs[0] = tmpCommand;

                    for (int i = 0, max = tmpMethodInfoList.Count; i < max; ++i)
                    {
                        MethodInfo tmpMethodInfo = tmpMethodInfoList[i];
                        tmpMethodInfoList[i].Invoke(null, sParameterObjs);
                    }
                }
            }

            return(tmpCommand);
        }
Exemple #17
0
            static bool TryReadAttributeData(ref BufferReader <byte> reader, UsageType usage, out ImmutableArray <byte> value)
            {
                switch (usage)
                {
                case UsageType.ContractHash:
                case UsageType.Vote:
                case UsageType.ECDH02:
                case UsageType.ECDH03:
                case var _ when usage >= UsageType.Hash1 && usage <= UsageType.Hash15:
                {
                    if (reader.TryReadByteArray(32, out var buffer))
                    {
                        value = buffer;
                        return(true);
                    }
                }
                break;

                case UsageType.Script:
                {
                    if (reader.TryReadByteArray(20, out var buffer))
                    {
                        value = buffer;
                        return(true);
                    }
                }
                break;

                case UsageType.Description:
                case var _ when usage >= UsageType.Remark:
                    return(reader.TryReadVarArray(ushort.MaxValue, out value));

                case UsageType.DescriptionUrl:
                {
                    if (reader.TryRead(out byte length) &&
                        reader.TryReadByteArray(length, out var data))
                    {
                        value = data;
                        return(true);
                    }
                }
                break;
                }

                value = default;
                return(false);
            }
Exemple #18
0
        internal override void UntypedDeserialize(BufferReader reader, ref object target, SerializationContext context)
        {
            T typedTarget = default(T);

            if (target is T)
            {
                typedTarget = (T)target;
            }
            else
            {
                target = (object)default(T); // when the target is of a different type, we have to allocate a new object (via boxing)
            }

            context.AddDeserializedObject(target);
            this.innerSerializer.Deserialize(reader, ref typedTarget, context);
            CopyToBox(typedTarget, ref target, context);
        }
        /// <summary>
        /// Deserialized byte array to a Restriction instance
        /// </summary>
        /// <param name="buffer">Byte array contain data of a Restriction instance.</param>
        /// <returns>Bytes count that deserialized in buffer.</returns>
        public override uint Deserialize(byte[] buffer)
        {
            BufferReader reader = new BufferReader(buffer);

            this.RestrictType         = (RestrictionType)reader.ReadByte();
            this.RelOp                = (RelationalOperator)reader.ReadByte();
            this.propTag.PropertyType = reader.ReadUInt16();
            this.propTag.PropertyId   = reader.ReadUInt16();

            uint size = reader.Position;

            byte[] tmpArray = reader.ReadToEnd();
            this.TaggedValue = AdapterHelper.ReadTaggedProperty(tmpArray);
            size            += (uint)this.TaggedValue.Size();

            return(size);
        }
Exemple #20
0
        private static GameServerType ReadType(BufferReader reader)
        {
            var e = reader.ReadChar();

            switch (e)
            {
                case 'd':
                    return GameServerType.Dedicated;
                case 'l':
                    return GameServerType.NonDedicated;
                case 'p':
                    return GameServerType.SpectatorProxy;
                default:
                    throw new ProtocolViolationException($"Tried to read server type, but {e} is not a legal value.");
            }

        }
Exemple #21
0
 /// <summary>
 /// Deserialize image.
 /// </summary>
 /// <param name="reader">Buffer reader being used.</param>
 /// <param name="target">Target image into which to deserialize.</param>
 /// <param name="context">Serialization context.</param>
 public virtual void Deserialize(BufferReader reader, ref TImage target, SerializationContext context)
 {
     Serializer.Deserialize(reader, ref target.image, context);
     Serializer.Deserialize(reader, ref target.width, context);
     Serializer.Deserialize(reader, ref target.height, context);
     Serializer.Deserialize(reader, ref target.stride, context);
     if (this.Schema.Version <= 2)
     {
         System.Drawing.Imaging.PixelFormat pixFmt = default;
         Serializer.Deserialize(reader, ref pixFmt, context);
         target.pixelFormat = PixelFormatHelper.FromSystemPixelFormat(pixFmt);
     }
     else
     {
         Serializer.Deserialize(reader, ref target.pixelFormat, context);
     }
 }
        public static List <ViofoGpsPoint> ReadMP4FileGpsInfo(string fileName)
        {
            const uint maxSize = 1024 * 1024 * 1024;

            FileInfo info = new FileInfo(fileName);

            if (info.Length > maxSize) //more than 1 GB
            {
                return(null);
            }

            byte[] buff = File.ReadAllBytes(fileName);
            using (IBufferReader reader = new BufferReader(buff))
            //using (IBufferReader reader = new MemoryMappedFileReader(fileName))
            {
                while (reader.Position < reader.Length)
                {
                    uint   size = reader.ReadUintBE();
                    string kind = reader.ReadString(4);
                    if (size > reader.Length)
                    {
                        return(null); //corrupt file
                    }
                    if (kind == "moov")
                    {
                        long end = reader.Position + size - 8;
                        while (reader.Position < end)
                        {
                            uint   size1 = reader.ReadUintBE();
                            string kind1 = reader.ReadString(4);
                            if (kind1 == "gps ")
                            {
                                return(ParseGpsCatalog(reader, size1 - 8)); //gps catalog - position and size list
                            }
                            reader.Position += (size1 - 8);
                        }
                    }
                    else
                    {
                        reader.Position += (size - 8);
                    }
                }
            }

            return(null);
        }
        public static void TestReadingDuplicateStrings([Values(true, false)] bool enableDictionary)
        {
            var columns = new Column[]
            {
                new Column <DateTime>("dateTime"),
                new Column <string>("value")
            };

            const int numRows = 10_000;
            var       rand    = new Random(1);
            var       dates   = Enumerable.Range(0, numRows).Select(i => new DateTime(2020, 01, 01).AddDays(i)).ToArray();
            var       values  = Enumerable.Range(0, numRows).Select(i => (rand.Next(0, 100) * 1000).ToString()).ToArray();

            using var buffer = new ResizableBuffer();

            // Write a file that contains a lot of duplicate strings.
            using (var output = new BufferOutputStream(buffer))
            {
                using var fileWriter  = new ParquetFileWriter(output, columns, CreateWriterProperties(enableDictionary));
                using var groupWriter = fileWriter.AppendRowGroup();

                using var dateWriter = groupWriter.NextColumn().LogicalWriter <DateTime>();
                dateWriter.WriteBatch(dates);

                using var valueWrite = groupWriter.NextColumn().LogicalWriter <string>();
                valueWrite.WriteBatch(values);
            }

            using var input       = new BufferReader(buffer);
            using var fileReader  = new ParquetFileReader(input);
            using var groupReader = fileReader.RowGroup(0);

            using var dateReader = groupReader.Column(0).LogicalReader <DateTime>();
            var readDates = dateReader.ReadAll(numRows);

            using var valueReader = groupReader.Column(1).LogicalReader <string>();
            var readValues = valueReader.ReadAll(numRows);

            Assert.AreEqual(dates, readDates);
            Assert.AreEqual(values, readValues);

            // When reading back the file, we expect the duplicate strings to point to the same memory instances.
            Assert.That(
                readValues.Distinct(new StringReferenceComparer()).Count(),
                enableDictionary ? Is.EqualTo(100) : Is.EqualTo(numRows));
        }
            static bool TryReadNeedStorage(ref BufferReader <byte> reader, byte version, out bool needStorage)
            {
                if (version < 1)
                {
                    needStorage = false;
                    return(true);
                }

                if (reader.TryRead(out var value))
                {
                    needStorage = value != 0;
                    return(true);
                }

                needStorage = default;
                return(false);
            }
        public void PolymorphicSerializer()
        {
            // verify that the default serializer works for polymorphic types
            var c = new TypeWithPolymorphicField();

            c.Name       = "foo";
            c.Enumerable = new List <int>();
            BufferWriter bw = new BufferWriter(100);

            Serializer.Serialize(bw, c, new SerializationContext());

            TypeWithPolymorphicField c2 = null;
            var br = new BufferReader(bw.Buffer);

            Serializer.Deserialize(br, ref c2, new SerializationContext());
            Assert.AreEqual(c.Name, c2.Name);
        }
Exemple #26
0
        public static bool TryRead(ref BufferReader <byte> reader, out Account value)
        {
            if (UInt160.TryRead(ref reader, out var scriptHash) &&
                reader.TryRead(out byte isFrozen) &&
                reader.TryReadVarArray <EncodedPublicKey>(EncodedPublicKey.TryRead, out var votes) &&
                reader.TryReadVarInt(out var balancesCount))
            {
                Debug.Assert(balancesCount < int.MaxValue);

                var builder = ImmutableDictionary.CreateBuilder <UInt256, Fixed8>();
                for (var i = 0; i < (int)balancesCount; i++)
                {
                    if (UInt256.TryRead(ref reader, out var assetId) &&
                        Fixed8.TryRead(ref reader, out var amount))
                    {
                        builder.Add(assetId, amount);
                    }
Exemple #27
0
        public void ShouldConsumeAllIEnumerables()
        {
            var br      = new BufferReader <string>();
            var strings = new String[] { "Peter", "Have", "A", "Buffering", "Reader" };

            br.BeginRead(strings);
            var count = 0;

            while (!br.AllIsRead)
            {
                if (br.Pop() != null)
                {
                    count++;
                }
            }
            Assert.That(count, Is.EqualTo(5));
        }
Exemple #28
0
    public static object Read(string key, Type type, EStorageType ePath)
    {
        try
        {
            byte[] bytes = File.ReadAllBytes(GetStoragePath(ePath) + "/" + key);

            //尝试解密
            // bytes = FileUtility.CopyTo(bytes);

            BufferReader buf = new BufferReader(bytes);
            return(buf.Read(type));
        }
        catch (System.Exception)
        {
            return(null);
        }
    }
Exemple #29
0
            public void ChannelRead(IOSession session, int msgId, byte[] buffer)
            {
                /*收到服务器发送来的消息*/
                BufferReader br = new BufferReader(buffer);

                if (msgId == 2)
                {
                    /*消息唯一回复id*/
                    long mmid = br.ReadInt64();
                    /*收到消息回复消息处理*/
                    log.Error("收到服务器消息:" + session.ID + "  消息唯一回复id" + mmid);

                    BufferWriter bw = new BufferWriter();
                    bw.Write(mmid);
                    session.WriteAndFlush(255, bw.GetBuffer());
                }
            }
Exemple #30
0
        /// <summary>
        /// Constructs a new JsonReader instance. This is a stack-only type.
        /// </summary>
        /// <param name="data">The <see cref="Span{byte}"/> value to consume. </param>
        /// <param name="encoder">An encoder used for decoding bytes from <paramref name="data"/> into characters.</param>
        public Utf8JsonReader(ReadOnlySpan <byte> data)
        {
            _reader          = default;
            _isSingleSegment = true;
            _buffer          = data;
            Depth            = 0;
            _containerMask   = 0;
            Index            = 0;
            StartLocation    = Index;
            _stack           = null;
            _maxDepth        = StackFreeMaxDepth;

            TokenType = JsonTokenType.None;
            Value     = ReadOnlySpan <byte> .Empty;
            ValueType = JsonValueType.Unknown;
            _inObject = false;
        }
Exemple #31
0
            static bool TryReadCommandString(ref BufferReader <byte> reader, out string command)
            {
                Span <byte> commandBytes = stackalloc byte[CommandSize];

                if (reader.TryCopyTo(commandBytes))
                {
                    reader.Advance(CommandSize);
                    var index = commandBytes.IndexOf((byte)0);
                    command = index == -1
                        ? Encoding.ASCII.GetString(commandBytes)
                        : Encoding.ASCII.GetString(commandBytes.Slice(0, index));
                    return(true);
                }

                command = null !;
                return(false);
            }
        public static void TestEncryptJustOneColumn()
        {
            // Case where the footer is unencrypted and all columns are encrypted all with different keys.
            using (var buffer = new ResizableBuffer())
            {
                using (var output = new BufferOutputStream(buffer))
                {
                    using var fileEncryptionProperties = CreateEncryptJustOneColumnProperties();
                    WriteParquetFile(output, fileEncryptionProperties);
                }

                // Decrypt the whole parquet file with matching decrypt properties.
                using (var input = new BufferReader(buffer))
                {
                    using var fileDecryptionProperties = CreateDecryptWithKeyRetrieverProperties();
                    ReadParquetFile(fileDecryptionProperties, input, rowGroupMetadata =>
                    {
                        using var colMetadata0 = rowGroupMetadata.GetColumnChunkMetaData(0);
                        using var colMetadata1 = rowGroupMetadata.GetColumnChunkMetaData(1);
                        using var crypto0      = colMetadata0.CryptoMetadata;
                        using var crypto1      = colMetadata1.CryptoMetadata;

                        Assert.AreEqual(null, crypto0);

                        Assert.AreEqual("Value", crypto1.ColumnPath.ToDotString());
                        Assert.AreEqual(false, crypto1.EncryptedWithFooterKey);
                        Assert.AreEqual("Key2", crypto1.KeyMetadata);
                    });
                }

                // Decrypt only the unencrypted column without providing any decrypt properties.
                using (var input = new BufferReader(buffer))
                {
                    using var fileReader  = new ParquetFileReader(input);
                    using var groupReader = fileReader.RowGroup(0);

                    var numRows = (int)groupReader.MetaData.NumRows;

                    using (var idReader = groupReader.Column(0).LogicalReader <int>())
                    {
                        Assert.AreEqual(Ids, idReader.ReadAll(numRows));
                    }
                }
            }
        }
Exemple #33
0
        public static void TestBigArrayRoundtrip()
        {
            // Create a big array of float arrays. Try to detect buffer-size related issues.
            var m  = 8196;
            var ar = new float[m];

            for (var i = 0; i < m; i++)
            {
                ar[i] = i;
            }

            var n        = 4;
            var expected = new float[n][];

            for (var i = 0; i < n; i++)
            {
                expected[i] = ar;
            }

            using var buffer = new ResizableBuffer();

            // Write out a single column
            using (var outStream = new BufferOutputStream(buffer))
            {
                using var fileWriter = new ParquetFileWriter(outStream, new Column[] { new Column <float[]>("big_array_field") });

                using (var rowGroupWriter = fileWriter.AppendRowGroup())
                {
                    using var colWriter = rowGroupWriter.NextColumn().LogicalWriter <float[]>();
                    colWriter.WriteBatch(expected);
                }

                fileWriter.Close();
            }

            // Read it back.
            using var inStream     = new BufferReader(buffer);
            using var fileReader   = new ParquetFileReader(inStream);
            using var rowGroup     = fileReader.RowGroup(0);
            using var columnReader = rowGroup.Column(0).LogicalReader <float[]>();

            var allData = columnReader.ReadAll((int)rowGroup.MetaData.NumRows);

            Assert.AreEqual(expected, allData);
        }
Exemple #34
0
        private void OnFrameReceived()
        {
            if (_opcode > 0x7)
            {
                var msg = _payloadData;
                _payloadData   = new BufferData();
                _payloadLength = 0;
                if (_opcode == 0x08)
                {
                    if (msg.ByteLength == 0)
                    {
                        HandleClose(0, null);
                    }
                    else if (msg.ByteLength == 1)
                    {
                        Error("Invalid Payload Length", 1002);
                    }
                    else
                    {
                        var reader = new BufferReader(msg);
                        var code   = reader.ReadUInt16();
                        var desc   = reader.ReadString(msg.ByteLength - 2);
                        HandleClose(code, desc);
                    }
                }

                else if (_opcode == 0x09)
                {
                    SendFrame(0x0a, _sendingMaskMode && !_isserver, true, msg);
                }
                else if (_opcode == 0x0a)
                {
                }
            }
            else if (_fin)
            {
                var msg = _payloadData;
                _payloadData   = new BufferData();
                _payloadLength = 0;
                if (OnMessage != null)
                {
                    OnMessage.Invoke(this, msg, _opcode);
                }
            }
        }
        public void ReaderIndexIsCorrect()
        {
            var buffer = Factory.CreateWithContent(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            var reader = BufferReader.Create(buffer);

            var counter = 1;

            while (!reader.End)
            {
                var span = reader.CurrentSegment;
                for (int i = reader.Index; i < span.Length; i++)
                {
                    Assert.Equal(counter++, reader.CurrentSegment[i]);
                }
                reader.Advance(span.Length);
            }
            Assert.Equal(buffer.Length, reader.ConsumedBytes);
        }
Exemple #36
0
        /// <summary>
        /// Read message envelope and raw bytes from stream.
        /// </summary>
        /// <param name="reader">Buffer reader used to read.</param>
        /// <param name="stream">Stream from which to read.</param>
        /// <returns>Message envelope and raw bytes.</returns>
        public static Tuple <Envelope, byte[]> ReadMessage(BufferReader reader, Stream stream)
        {
            int headerLen;

            unsafe
            {
                headerLen = sizeof(Envelope) + sizeof(int);
            }

            reader.Reset(headerLen);
            Read(reader.Buffer, headerLen, stream);
            var envelope = reader.ReadEnvelope();
            var length   = reader.ReadInt32();
            var buffer   = new byte[length];

            Read(buffer, length, stream);
            return(Tuple.Create(envelope, buffer));
        }
Exemple #37
0
        /// <summary>
        /// Deserialized byte array to an Restriction instance
        /// </summary>
        /// <param name="buffer">Byte array contain data of an Restriction instance</param>
        /// <returns>Bytes count that deserialized in buffer</returns>
        public uint Deserialize(byte[] buffer)
        {
            BufferReader reader = new BufferReader(buffer);

            this.RestrictType             = (RestrictionType)reader.ReadByte();
            this.FuzzyLevelLow            = (FuzzyLevelLowValue)reader.ReadUInt16();
            this.FuzzyLevelHigh           = (FuzzyLevelHighValue)reader.ReadUInt16();
            this.propertyTag.PropertyId   = reader.ReadUInt16();
            this.propertyTag.PropertyType = reader.ReadUInt16();

            uint size = reader.Position;

            byte[] tmpArray = reader.ReadToEnd();
            this.TaggedValue = AdapterHelper.ReadTaggedProperty(tmpArray);
            size            += (uint)this.TaggedValue.Size();

            return(size);
        }
Exemple #38
0
        private static GameServerEnvironment ReadEnvironment(BufferReader reader)
        {
            var e = reader.ReadChar();

            switch (e)
            {
                case 'l':
                    return GameServerEnvironment.Linux;
                case 'w':
                    return GameServerEnvironment.Windows;
                case 'm':
                case 'o':
                    return GameServerEnvironment.Mac;
                default:
                    throw new ProtocolViolationException($"Tried to read server environment, but {e} is not a legal value.");
            }
            
        }
Exemple #39
0
            /// <inheritdoc/>
            public override void Deserialize(BufferReader reader, ref DepthImage target, SerializationContext context)
            {
                var depthCompressionMethod = DepthCompressionMethod.None;

                if (this.Schema.Version >= 4)
                {
                    Serializer.Deserialize(reader, ref depthCompressionMethod, context);
                }

                if (depthCompressionMethod == DepthCompressionMethod.None)
                {
                    base.Deserialize(reader, ref target, context);
                }
                else
                {
                    depthImageCompressor.Deserialize(reader, ref target, context);
                }
            }
Exemple #40
0
        internal static GameServerInfo Parse(IGameServer server, ushort ping, BufferReader reader)
        {
            var result = new GameServerInfo
            {
                Server = server,
                ProtocolVersion = reader.ReadByte(),
                Name = reader.ReadString(),
                Map = reader.ReadString(),
                Gamedir = reader.ReadString(),
                Game = reader.ReadString(),
                AppId = reader.ReadShort(),
                Players = reader.ReadByte(),
                MaxPlayers = reader.ReadByte(),
                Bots = reader.ReadByte(),
                Type = ReadType(reader),
                Environment = ReadEnvironment(reader),
                HasPassword = reader.ReadBool(),
                Vac = reader.ReadBool(),
                Version = reader.ReadString(),
                Ping = ping
            };

            return result;
        }
Exemple #41
0
 private static IEnumerable<IPEndPoint> ReadEndPointsFromPacket(BufferReader reader)
 {
     while (reader.Remaining >= IpEndPointLength)
         yield return ReadEndPoint(reader);
 }
 public void ReadsFloat()
 {
     var reader = new BufferReader(new byte[] { 133, 235, 85, 65 });
     
     Assert.AreEqual(13.37f, reader.ReadFloat());
 }
 public MultiPacketStringReader(BufferReader initialReader, Func<Task<BufferReader>> sequelRequestAsyncFunc)
 {
     _reader = initialReader;
     _sequelRequestAsyncFunc = sequelRequestAsyncFunc;
 }
        public async Task ReadsMultiPacketStringExcludingPacketHeaders()
        {
            var bytes = new byte[] { 65, 65, 65 };
            var extraPackets = new[]
            {
                new byte[] { 00, 66, 66, 66 },
                new byte[] { 00, 67, 67 },
                new byte[] { 00, 67 },
                new byte[] { 00, 68, 68, 68, 00 },
            };

            var index = 0;

            var reader = new MultiPacketStringReader(new BufferReader(bytes), async () =>
            {
                await Task.Delay(1000);

                var sequelReader = new BufferReader(extraPackets[index++]);
                sequelReader.Skip(1);

                return sequelReader;
            });

            Assert.AreEqual("AAABBBCCCDDD", await reader.ReadStringAsync());
        }
Exemple #45
0
 private static ushort ReadNetworkOrderShort(BufferReader reader)
 {
     return BitConverter.ToUInt16(reader.ReadBytes(2).Reverse().ToArray(), 0);
 }
        public void ReadsPartialUnterminatedString()
        {
            var reader = new BufferReader(new byte[] { 65, 66, 67 });

            Assert.IsFalse(reader.IsStringTerminated());
            Assert.AreEqual("ABC", reader.ReadPartialString());
        }
Exemple #47
-1
        public static async Task<BufferReader> ReceiveBufferReaderAsync(UdpClient client, int headerSize)
        {
            var response = await client.ReceiveAsync();
            var reader = new BufferReader(response.Buffer);
            reader.Skip(headerSize);

            return reader;
        }