public void AddEntriesFrom_Message()
        {
            var message1 = new ForeignMessage { C = 2000 };
            var message2 = new ForeignMessage { C = -250 };

            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);
            output.WriteTag(tag);
            output.WriteMessage(message1);
            output.WriteTag(tag);
            output.WriteMessage(message2);
            output.Flush();
            stream.Position = 0;

            var field = new RepeatedField<ForeignMessage>();
            var input = new CodedInputStream(stream);
            input.AssertNextTag(tag);
            field.AddEntriesFrom(input, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
            CollectionAssert.AreEqual(new[] { message1, message2}, field);
            Assert.IsTrue(input.IsAtEnd);
        }
        public void AddEntriesFrom_NonPackedInt32()
        {
            uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);
            output.WriteTag(nonPackedTag);
            output.WriteInt32(10);
            output.WriteTag(nonPackedTag);
            output.WriteInt32(999);
            output.WriteTag(nonPackedTag);
            output.WriteInt32(-1000); // Just for variety...
            output.Flush();
            stream.Position = 0;

            // Deliberately "expecting" a packed tag, but we detect that the data is
            // actually not packed.
            uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var field = new RepeatedField<int>();
            var input = new CodedInputStream(stream);
            input.AssertNextTag(nonPackedTag);
            field.AddEntriesFrom(input, FieldCodec.ForInt32(packedTag));
            CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
            Assert.IsTrue(input.IsAtEnd);
        }
 /// <inheritdoc/>
 protected override int CalculateSize(byte[] value)
 {
     return(CodedOutputStream.ComputeBytesSize(ByteString.CopyFrom(value)));
 }
Exemple #4
0
 public void WriteTo(CodedOutputStream output)
 {
     codec.keyCodec.WriteTagAndValue(output, Key);
     codec.valueCodec.WriteTagAndValue(output, Value);
 }
 public void WriteTo(CodedOutputStream output)
 {
 }
Exemple #6
0
        static ByteString EncodeObject(object value, MemoryStream buffer, CodedOutputStream stream)
        {
            buffer.SetLength(0);
            if (value == null)
            {
                stream.WriteUInt64(0);
            }
            else if (value is Enum)
            {
                stream.WriteSInt32((int)value);
            }
            else
            {
                var type = value.GetType();
                switch (Type.GetTypeCode(type))
                {
                case TypeCode.Double:
                    stream.WriteDouble((double)value);
                    break;

                case TypeCode.Single:
                    stream.WriteFloat((float)value);
                    break;

                case TypeCode.Int32:
                    stream.WriteSInt32((int)value);
                    break;

                case TypeCode.Int64:
                    stream.WriteSInt64((long)value);
                    break;

                case TypeCode.UInt32:
                    stream.WriteUInt32((uint)value);
                    break;

                case TypeCode.UInt64:
                    stream.WriteUInt64((ulong)value);
                    break;

                case TypeCode.Boolean:
                    stream.WriteBool((bool)value);
                    break;

                case TypeCode.String:
                    stream.WriteString((string)value);
                    break;

                default:
                    if (type == typeof(byte[]))
                    {
                        stream.WriteBytes(ByteString.CopyFrom((byte[])value));
                    }
                    else if (TypeUtils.IsAClassType(type))
                    {
                        stream.WriteUInt64(ObjectStore.Instance.AddInstance(value));
                    }
                    else if (TypeUtils.IsATupleCollectionType(type))
                    {
                        WriteTuple(value, stream);
                    }
                    else if (TypeUtils.IsAListCollectionType(type))
                    {
                        WriteList(value, stream);
                    }
                    else if (TypeUtils.IsASetCollectionType(type))
                    {
                        WriteSet(value, stream);
                    }
                    else if (TypeUtils.IsADictionaryCollectionType(type))
                    {
                        WriteDictionary(value, stream);
                    }
                    else if (TypeUtils.IsAMessageType(type))
                    {
                        WriteMessage(value, stream);
                    }
                    else
                    {
                        throw new ArgumentException(type + " is not a serializable type");
                    }
                    break;
                }
            }
            stream.Flush();
            return(ByteString.CopyFrom(buffer.GetBuffer(), 0, (int)buffer.Length));
        }
Exemple #7
0
 public void WriteTo(CodedOutputStream output)
 {
     this.items_.WriteTo(output, MST_MASTER_MISSION_DISPLAY_INFO_ARRAY._repeated_items_codec);
 }
Exemple #8
0
        public void TestNegativeEnumPackedArray()
        {
            int arraySize = 1 + (10 * 5);
            int msgSize = 1 + 1 + arraySize;
            byte[] bytes = new byte[msgSize];
            CodedOutputStream output = new CodedOutputStream(bytes);
            // Length-delimited to show we want the packed representation
            uint tag = WireFormat.MakeTag(8, WireFormat.WireType.LengthDelimited);
            output.WriteTag(tag);
            int size = 0;
            for (int i = 0; i >= -5; i--)
            {
                size += CodedOutputStream.ComputeEnumSize(i);
            }
            output.WriteRawVarint32((uint)size);
            for (int i = 0; i >= -5; i--)
            {
                output.WriteEnum(i);
            }
            Assert.AreEqual(0, output.SpaceLeft);

            CodedInputStream input = new CodedInputStream(bytes);
            tag = input.ReadTag();

            RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();
            values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x));

            Assert.AreEqual(6, values.Count);
            Assert.AreEqual(SampleEnum.None, values[0]);
            Assert.AreEqual(((SampleEnum)(-1)), values[1]);
            Assert.AreEqual(SampleEnum.NegativeValue, values[2]);
            Assert.AreEqual(((SampleEnum)(-3)), values[3]);
            Assert.AreEqual(((SampleEnum)(-4)), values[4]);
            Assert.AreEqual(((SampleEnum)(-5)), values[5]);
        }
Exemple #9
0
        public void WriteTo_NonPackedInt32()
        {
            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
            var field = new RepeatedField<int> { 10, 1000, 1000000};
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);
            field.WriteTo(output, FieldCodec.ForInt32(tag));
            output.Flush();
            stream.Position = 0;

            var input = new CodedInputStream(stream);
            input.AssertNextTag(tag);
            Assert.AreEqual(10, input.ReadInt32());
            input.AssertNextTag(tag);
            Assert.AreEqual(1000, input.ReadInt32());
            input.AssertNextTag(tag);
            Assert.AreEqual(1000000, input.ReadInt32());
            Assert.IsTrue(input.IsAtEnd);
        }
Exemple #10
0
 /// <inheritdoc/>
 public override int CalculateSize(long value)
 {
     return(CodedOutputStream.ComputeInt64Size(value));
 }
Exemple #11
0
 /// <inheritdoc/>
 public override int CalculateSize(float value)
 {
     return(CodedOutputStream.ComputeFloatSize(value));
 }
Exemple #12
0
 public void WriteTo(CodedOutputStream output)
 {
     this.items_.WriteTo(output, MST_SVT_TREASURE_DEVICE_ARRAY._repeated_items_codec);
 }
Exemple #13
0
 public override void Write(CodedOutputStream stream, float t)
 {
     stream.WriteFloat(t);
 }
        public async Task CreateAsync(ulong startTimestamp,
                                      ulong endTimestamp,
                                      string region,
                                      int batchNum,
                                      IEnumerable <TemporaryExposureKeyModel> keys)
        {
            Logger.LogInformation($"start {nameof(CreateAsync)}");
            var current = keys;

            while (current.Any())
            {
                var exportKeyModels = current.Take(MaxKeysPerFile).ToArray();
                var exportKeys      = exportKeyModels.Select(_ => _.ToKey()).ToArray();
                current = current.Skip(MaxKeysPerFile);

                var signatureInfo = SignatureService.Create();
                await SignService.SetSignatureAsync(signatureInfo);

                var exportModel = new TemporaryExposureKeyExportModel();
                exportModel.id           = batchNum.ToString();
                exportModel.PartitionKey = region;
                // TODO: not support apple
                //exportModel.BatchNum = batchNum;
                exportModel.BatchNum = 1;
                exportModel.Region   = region;
                // TODO: not support apple
                //exportModel.BatchSize = exportKeyModels.Length;
                exportModel.BatchSize                  = 1;
                exportModel.StartTimestamp             = startTimestamp;
                exportModel.EndTimestamp               = endTimestamp;
                exportModel.TimestampSecondsSinceEpoch = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                exportModel = await TekExportRepository.CreateAsync(exportModel);

                var bin = new TemporaryExposureKeyExport();
                bin.Keys.AddRange(exportKeys);
                bin.BatchNum       = exportModel.BatchNum;
                bin.BatchSize      = exportModel.BatchSize;
                bin.Region         = exportModel.Region;
                bin.StartTimestamp = exportModel.StartTimestamp;
                bin.EndTimestamp   = exportModel.EndTimestamp;
                bin.SignatureInfos.Add(signatureInfo);

                var sig = new TEKSignatureList();

                using var binStream = new MemoryStream();
                await binStream.WriteAsync(FixedHeader, 0, FixedHeaderWidth);

                using var binStreamCoded = new CodedOutputStream(binStream, true);
                bin.WriteTo(binStreamCoded);
                binStreamCoded.Flush();
                await binStream.FlushAsync();

                var signature = await CreateSignatureAsync(binStream, bin.BatchNum, bin.BatchSize);

                signature.SignatureInfo = signatureInfo;
                sig.Signatures.Add(signature);

                using (var s = new MemoryStream())
                {
                    using (var z = new System.IO.Compression.ZipArchive(s, System.IO.Compression.ZipArchiveMode.Create, true))
                    {
                        var binEntry = z.CreateEntry(ExportBinFileName);
                        using (var binFile = binEntry.Open())
                        {
                            binStream.Seek(0, SeekOrigin.Begin);
                            await binStream.CopyToAsync(binFile);

                            await binFile.FlushAsync();
                        }

                        var sigEntry = z.CreateEntry(ExportSigFileName);
                        using (var sigFile = sigEntry.Open())
                            using (var output = new CodedOutputStream(sigFile))
                            {
                                sig.WriteTo(output);
                                output.Flush();
                                await sigFile.FlushAsync();
                            }
                    }
                    s.Seek(0, SeekOrigin.Begin);
                    await BlobService.WriteToBlobAsync(s, exportModel, bin, sig);
                }
                await TekExportRepository.UpdateAsync(exportModel);
            }
        }
 public void WriteTo(CodedOutputStream output, FieldCodec <T> codec)
 {
     _internal.WriteTo(output, codec);
 }
Exemple #16
0
        public void UnknownFieldInWrapper()
        {
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);
            var wrapperTag = WireFormat.MakeTag(TestWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited);
            var unknownTag = WireFormat.MakeTag(15, WireFormat.WireType.Varint);
            var valueTag = WireFormat.MakeTag(Int32Value.ValueFieldNumber, WireFormat.WireType.Varint);

            output.WriteTag(wrapperTag);
            output.WriteLength(4); // unknownTag + value 5 + valueType + value 6, each 1 byte
            output.WriteTag(unknownTag);
            output.WriteInt32((int) valueTag); // Sneakily "pretend" it's a tag when it's really a value
            output.WriteTag(valueTag);
            output.WriteInt32(6);

            output.Flush();
            stream.Position = 0;
            
            var message = TestWellKnownTypes.Parser.ParseFrom(stream);
            Assert.AreEqual(6, message.Int32Field);
        }
Exemple #17
0
 public StreamStream(IStream <byte, byte> stream) : base(stream)
 {
     codedOutputStream = new CodedOutputStream(new ByteOutputAdapterStream(stream), true);
 }
Exemple #18
0
        public void WriteTo_Message()
        {
            var message1 = new ForeignMessage { C = 20 };
            var message2 = new ForeignMessage { C = 25 };
            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var field = new RepeatedField<ForeignMessage> { message1, message2 };
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);
            field.WriteTo(output, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
            output.Flush();
            stream.Position = 0;

            var input = new CodedInputStream(stream);
            input.AssertNextTag(tag);
            Assert.AreEqual(message1, input.ReadMessage(ForeignMessage.Parser));
            input.AssertNextTag(tag);
            Assert.AreEqual(message2, input.ReadMessage(ForeignMessage.Parser));
            Assert.IsTrue(input.IsAtEnd);
        }
Exemple #19
0
        static ByteString EncodeObject(object value, Type type, MemoryStream buffer, CodedOutputStream stream)
        {
            buffer.SetLength(0);
            if (value != null && !type.IsInstanceOfType(value))
            {
                throw new ArgumentException("Value of type " + value.GetType() + " cannot be encoded to type " + type);
            }
            if (value == null && !type.IsSubclassOf(typeof(RemoteObject)) && !IsACollectionType(type))
            {
                throw new ArgumentException("null cannot be encoded to type " + type);
            }
            if (value == null)
            {
                stream.WriteUInt64(0);
            }
            else if (value is Enum)
            {
                stream.WriteInt32((int)value);
            }
            else
            {
                switch (Type.GetTypeCode(type))
                {
                case TypeCode.Int32:
                    stream.WriteInt32((int)value);
                    break;

                case TypeCode.Int64:
                    stream.WriteInt64((long)value);
                    break;

                case TypeCode.UInt32:
                    stream.WriteUInt32((uint)value);
                    break;

                case TypeCode.UInt64:
                    stream.WriteUInt64((ulong)value);
                    break;

                case TypeCode.Single:
                    stream.WriteFloat((float)value);
                    break;

                case TypeCode.Double:
                    stream.WriteDouble((double)value);
                    break;

                case TypeCode.Boolean:
                    stream.WriteBool((bool)value);
                    break;

                case TypeCode.String:
                    stream.WriteString((string)value);
                    break;

                default:
                    if (type.Equals(typeof(byte[])))
                    {
                        stream.WriteBytes(ByteString.CopyFrom((byte[])value));
                    }
                    else if (IsAClassType(type))
                    {
                        stream.WriteUInt64(((RemoteObject)value).id);
                    }
                    else if (IsAMessageType(type))
                    {
                        ((IMessage)value).WriteTo(buffer);
                    }
                    else if (IsAListType(type))
                    {
                        WriteList(value, type, buffer);
                    }
                    else if (IsADictionaryType(type))
                    {
                        WriteDictionary(value, type, buffer);
                    }
                    else if (IsASetType(type))
                    {
                        WriteSet(value, type, buffer);
                    }
                    else if (IsATupleType(type))
                    {
                        WriteTuple(value, type, buffer);
                    }
                    else
                    {
                        throw new ArgumentException(type + " is not a serializable type");
                    }
                    break;
                }
            }
            stream.Flush();
            return(ByteString.CopyFrom(buffer.GetBuffer(), 0, (int)buffer.Length));
        }
Exemple #20
0
 public void WriteTo(CodedOutputStream output)
 {
     throw new System.NotImplementedException();
 }
Exemple #21
0
 /// <inheritdoc/>
 protected override int CalculateSize(Guid value)
 {
     return(CodedOutputStream.ComputeBytesSize(ByteString.CopyFrom(value.ToByteArray())));
 }
Exemple #22
0
 public void WriteTo(CodedOutputStream output)
 {
     this.items_.WriteTo(output, MST_EVENT_ITEM_DISPLAY_ARRAY._repeated_items_codec);
 }
Exemple #23
0
 public void WriteTo(CodedOutputStream output)
 {
     // Message adapter is an internal class and we know that all the writing will happen via InternalWriteTo.
     throw new NotImplementedException();
 }
Exemple #24
0
 public void WriteTo(CodedOutputStream output)
 {
     this.items_.WriteTo(output, MST_AI_FIELD_ARRAY._repeated_items_codec);
 }
 public override void Write(CodedOutputStream stream, double t)
 {
     stream.WriteDouble(t);
 }
 public void WriteTo(CodedOutputStream output)
 {
     this.items_.WriteTo(output, MST_EQUIP_EXP_ARRAY._repeated_items_codec);
 }
Exemple #27
0
 public void WriteTo(CodedOutputStream output)
 {
     output.WriteRawMessage(this);
 }
Exemple #28
0
 public void WriteTo(CodedOutputStream output)
 {
     this.items_.WriteTo(output, MST_QUEST_RELEASE_ARRAY._repeated_items_codec);
 }
Exemple #29
0
 public override void Write(CodedOutputStream stream, long t)
 {
     stream.WriteInt64(t);
 }
Exemple #30
0
 public void WriteTo(CodedOutputStream output)
 {
     this.items_.WriteTo(output, NPC_FOLLOWER_ARRAY._repeated_items_codec);
 }
 public void WriteTo(CodedOutputStream output)
 {
     this.items_.WriteTo(output, MST_SVT_PASSIVE_SKILL_ARRAY._repeated_items_codec);
 }
Exemple #32
0
        public void RepeatedWrappersBinaryFormat()
        {
            // At one point we accidentally used a packed format for repeated wrappers, which is wrong (and weird).
            // This test is just to prove that we use the right format.

            var rawOutput = new MemoryStream();
            var output = new CodedOutputStream(rawOutput);
            // Write a value of 5
            output.WriteTag(RepeatedWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited);
            output.WriteLength(2);
            output.WriteTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.Varint);
            output.WriteInt32(5);
            // Write a value of 0 (empty message)
            output.WriteTag(RepeatedWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited);
            output.WriteLength(0);
            output.Flush();
            var expectedBytes = rawOutput.ToArray();

            var message = new RepeatedWellKnownTypes { Int32Field = { 5, 0 } };
            var actualBytes = message.ToByteArray();
            Assert.AreEqual(expectedBytes, actualBytes);
        }
Exemple #33
0
 public static int TagSize(int fieldNumber)
 {
     return(CodedOutputStream.ComputeTagSize(fieldNumber));
 }
Exemple #34
0
        public void AddEntriesFrom_String()
        {
            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);
            output.WriteTag(tag);
            output.WriteString("Foo");
            output.WriteTag(tag);
            output.WriteString("");
            output.WriteTag(tag);
            output.WriteString("Bar");
            output.Flush();
            stream.Position = 0;

            var field = new RepeatedField<string>();
            var input = new CodedInputStream(stream);
            input.AssertNextTag(tag);
            field.AddEntriesFrom(input, FieldCodec.ForString(tag));
            CollectionAssert.AreEqual(new[] { "Foo", "", "Bar" }, field);
            Assert.IsTrue(input.IsAtEnd);
        }
Exemple #35
0
 public static int VarintSize32(uint value)
 {
     return(CodedOutputStream.ComputeRawVarint32Size(value));
 }
Exemple #36
0
        public void WriteTo_String()
        {
            uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var field = new RepeatedField<string> { "Foo", "", "Bar" };
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);
            field.WriteTo(output, FieldCodec.ForString(tag));
            output.Flush();
            stream.Position = 0;

            var input = new CodedInputStream(stream);
            input.AssertNextTag(tag);
            Assert.AreEqual("Foo", input.ReadString());
            input.AssertNextTag(tag);
            Assert.AreEqual("", input.ReadString());
            input.AssertNextTag(tag);
            Assert.AreEqual("Bar", input.ReadString());
            Assert.IsTrue(input.IsAtEnd);
        }
Exemple #37
0
 public static int UInt32Size(uint value)
 {
     return(CodedOutputStream.ComputeUInt32Size(value));
 }
Exemple #38
0
        public void TestNegativeEnumArray()
        {
            int arraySize = 1 + 1 + (11 * 5);
            int msgSize = arraySize;
            byte[] bytes = new byte[msgSize];
            CodedOutputStream output = new CodedOutputStream(bytes);
            uint tag = WireFormat.MakeTag(8, WireFormat.WireType.Varint);
            for (int i = 0; i >= -5; i--)
            {
                output.WriteTag(tag);
                output.WriteEnum(i);
            }

            Assert.AreEqual(0, output.SpaceLeft);

            CodedInputStream input = new CodedInputStream(bytes);
            tag = input.ReadTag();

            RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();
            values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x));

            Assert.AreEqual(6, values.Count);
            Assert.AreEqual(SampleEnum.None, values[0]);
            Assert.AreEqual(((SampleEnum)(-1)), values[1]);
            Assert.AreEqual(SampleEnum.NegativeValue, values[2]);
            Assert.AreEqual(((SampleEnum)(-3)), values[3]);
            Assert.AreEqual(((SampleEnum)(-4)), values[4]);
            Assert.AreEqual(((SampleEnum)(-5)), values[5]);
        }
 public void WriteTo(CodedOutputStream output)
 {
     this.items_.WriteTo(output, MST_STONE_SHOP_ARRAY._repeated_items_codec);
 }
Exemple #40
0
        public void MergingCornerCase()
        {
            var message = new TestWellKnownTypes { Int32Field = 5 };

            // Create a byte array which has the data of an Int32Value explicitly containing a value of 0.
            // This wouldn't normally happen.
            byte[] bytes;
            var wrapperTag = WireFormat.MakeTag(TestWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited);
            var valueTag = WireFormat.MakeTag(Int32Value.ValueFieldNumber, WireFormat.WireType.Varint);
            using (var stream = new MemoryStream())
            {
                var coded = new CodedOutputStream(stream);
                coded.WriteTag(wrapperTag);
                coded.WriteLength(2); // valueTag + a value 0, each one byte
                coded.WriteTag(valueTag);
                coded.WriteInt32(0);
                coded.Flush();
                bytes = stream.ToArray();
            }

            message.MergeFrom(bytes);
            // A normal implementation would have 0 now, as the explicit default would have been overwritten the 5.
            Assert.AreEqual(5, message.Int32Field);
        }
 public void WriteTo(CodedOutputStream output)
 {
     this.items_.WriteTo(output, MST_EVENT_MISSION_CONDITION_ARRAY._repeated_items_codec);
 }