• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Protobuf.CodedOutputStream类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中Google.Protobuf.CodedOutputStream的典型用法代码示例。如果您正苦于以下问题:C# CodedOutputStream类的具体用法?C# CodedOutputStream怎么用?C# CodedOutputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



CodedOutputStream类属于Google.Protobuf命名空间,在下文中一共展示了CodedOutputStream类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Encode

 /// <summary>
 /// Encode an object of the given type using the protocol buffer encoding scheme.
 /// Should not be called directly. This interface is used by service client stubs.
 /// </summary>
 public static ByteString Encode (object value, Type type)
 {
     using (var buffer = new MemoryStream ()) {
         var stream = new CodedOutputStream (buffer, true);
         return EncodeObject (value, type, buffer, stream);
     }
 }
开发者ID:paperclip,项目名称:krpc,代码行数:11,代码来源:Encoder.cs


示例2: EncodeObject

 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);
 }
开发者ID:paperclip,项目名称:krpc,代码行数:60,代码来源:Encoder.cs


示例3: ProtoToByteArray

 public static byte[] ProtoToByteArray(IMessage message)
 {
     int size = message.CalculateSize();
     byte[] buffer = new byte[size];
     CodedOutputStream output = new CodedOutputStream(buffer);
     message.WriteTo(output);
     return buffer;
 }
开发者ID:R-ichardBall,项目名称:or-tools,代码行数:8,代码来源:ProtoHelper.cs


示例4: EncodeObject

 static ByteString EncodeObject (object value, MemoryStream buffer, CodedOutputStream stream)
 {
     buffer.SetLength (0);
     if (value == null) {
         stream.WriteUInt64 (0);
     } else if (value is Enum) {
         stream.WriteInt32 ((int)value);
     } else {
         Type type = value.GetType ();
         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 == typeof(byte[]))
                 stream.WriteBytes (ByteString.CopyFrom ((byte[])value));
             else if (TypeUtils.IsAClassType (type))
                 stream.WriteUInt64 (ObjectStore.Instance.AddInstance (value));
             else if (TypeUtils.IsAMessageType (type))
                 WriteMessage (value, stream);
             else if (TypeUtils.IsAListCollectionType (type))
                 WriteList (value, stream);
             else if (TypeUtils.IsADictionaryCollectionType (type))
                 WriteDictionary (value, stream);
             else if (TypeUtils.IsASetCollectionType (type))
                 WriteSet (value, stream);
             else if (TypeUtils.IsATupleCollectionType (type))
                 WriteTuple (value, stream);
             else
                 throw new ArgumentException (type + " is not a serializable type");
             break;
         }
     }
     stream.Flush ();
     return ByteString.CopyFrom (buffer.GetBuffer (), 0, (int)buffer.Length);
 }
开发者ID:paperclip,项目名称:krpc,代码行数:57,代码来源:Encoder.cs


示例5: ToByteArray

 public static byte[] ToByteArray(this IMessage message)
 {
     Preconditions.CheckNotNull(message, "message");
     byte[] result = new byte[message.CalculateSize()];
     CodedOutputStream output = new CodedOutputStream(result);
     message.WriteTo(output);
     output.CheckNoSpaceLeft();
     return result;
 }
开发者ID:rbarraud,项目名称:protobuf,代码行数:9,代码来源:MessageExtensions.cs


示例6: ValueTypeToByteString

 public void ValueTypeToByteString ()
 {
     // From Google's protobuf documentation, varint encoding example:
     // 300 = 1010 1100 0000 0010 = 0xAC 0x02
     const int value = 300;
     var buffer = new byte [2];
     var codedStream = new CodedOutputStream (buffer);
     codedStream.WriteUInt32 (value);
     Assert.AreEqual ("ac02", buffer.ToHexString ());
 }
开发者ID:paperclip,项目名称:krpc,代码行数:10,代码来源:SchemaTest.cs


示例7: ValueTypeToByteString

 public void ValueTypeToByteString()
 {
     // From Google's protobuf documentation, varint encoding example:
     // 300 = 1010 1100 0000 0010 = 0xAC 0x02
     const int value = 300;
     var buffer = new byte [2];
     var codedStream = new CodedOutputStream (buffer);
     codedStream.WriteUInt32 (value);
     string hex = ("0x" + BitConverter.ToString (buffer)).Replace ("-", " 0x");
     Assert.AreEqual ("0xAC 0x02", hex);
 }
开发者ID:artwhaley,项目名称:krpc,代码行数:11,代码来源:RpcTest.cs


示例8: Dispose_DisposesUnderlyingStream

 public void Dispose_DisposesUnderlyingStream()
 {
     var memoryStream = new MemoryStream();
     Assert.IsTrue(memoryStream.CanWrite);
     using (var cos = new CodedOutputStream(memoryStream))
     {
         cos.WriteRawByte(0);
         Assert.AreEqual(0, memoryStream.Position); // Not flushed yet
     }
     Assert.AreEqual(1, memoryStream.ToArray().Length); // Flushed data from CodedOutputStream to MemoryStream
     Assert.IsFalse(memoryStream.CanWrite); // Disposed
 }
开发者ID:mirror,项目名称:chromium,代码行数:12,代码来源:CodedOutputStreamTest.cs


示例9: Dispose_WithLeaveOpen

 public void Dispose_WithLeaveOpen()
 {
     var memoryStream = new MemoryStream();
     Assert.IsTrue(memoryStream.CanWrite);
     using (var cos = new CodedOutputStream(memoryStream, true))
     {
         cos.WriteRawByte(0);
         Assert.AreEqual(0, memoryStream.Position); // Not flushed yet
     }
     Assert.AreEqual(1, memoryStream.Position); // Flushed data from CodedOutputStream to MemoryStream
     Assert.IsTrue(memoryStream.CanWrite); // We left the stream open
 }
开发者ID:mirror,项目名称:chromium,代码行数:12,代码来源:CodedOutputStreamTest.cs


示例10: StringToByteBuffer

        private byte[] StringToByteBuffer(string str)
        {
            int t = CodedOutputStream.ComputeTagSize(9);
            int s = CodedOutputStream.ComputeStringSize(str);

            s += t;
            byte[] bytes = new byte[s];
            CodedOutputStream cos = new CodedOutputStream(bytes);
            cos.WriteTag((9 << 3) + 2);
            cos.WriteString(str);
            cos.Flush();
            return bytes;
        }
开发者ID:infinispan,项目名称:dotnet-client,代码行数:13,代码来源:BasicTypesProtostreamMarshaller.cs


示例11: AssertWriteVarint

        /// <summary>
        /// Writes the given value using WriteRawVarint32() and WriteRawVarint64() and
        /// checks that the result matches the given bytes
        /// </summary>
        private static void AssertWriteVarint(byte[] data, ulong value)
        {
            // Only do 32-bit write if the value fits in 32 bits.
            if ((value >> 32) == 0)
            {
                MemoryStream rawOutput = new MemoryStream();
                CodedOutputStream output = new CodedOutputStream(rawOutput);
                output.WriteRawVarint32((uint) value);
                output.Flush();
                Assert.AreEqual(data, rawOutput.ToArray());
                // Also try computing size.
                Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint32Size((uint) value));
            }

            {
                MemoryStream rawOutput = new MemoryStream();
                CodedOutputStream output = new CodedOutputStream(rawOutput);
                output.WriteRawVarint64(value);
                output.Flush();
                Assert.AreEqual(data, rawOutput.ToArray());

                // Also try computing size.
                Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint64Size(value));
            }

            // Try different buffer sizes.
            for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
            {
                // Only do 32-bit write if the value fits in 32 bits.
                if ((value >> 32) == 0)
                {
                    MemoryStream rawOutput = new MemoryStream();
                    CodedOutputStream output =
                        new CodedOutputStream(rawOutput, bufferSize);
                    output.WriteRawVarint32((uint) value);
                    output.Flush();
                    Assert.AreEqual(data, rawOutput.ToArray());
                }

                {
                    MemoryStream rawOutput = new MemoryStream();
                    CodedOutputStream output = new CodedOutputStream(rawOutput, bufferSize);
                    output.WriteRawVarint64(value);
                    output.Flush();
                    Assert.AreEqual(data, rawOutput.ToArray());
                }
            }
        }
开发者ID:2php,项目名称:protobuf,代码行数:52,代码来源:CodedOutputStreamTest.cs


示例12: ObjectToByteBuffer

        private byte[] ObjectToByteBuffer(int descriptorId, object obj)
        {
            IMessage u = (IMessage)obj;

            int size = u.CalculateSize();
            byte[] bytes = new byte[size];
            CodedOutputStream cos = new CodedOutputStream(bytes);
            u.WriteTo(cos);
            
            cos.Flush();
            WrappedMessage wm = new WrappedMessage();
            wm.WrappedMessageBytes = ByteString.CopyFrom(bytes);
            wm.WrappedDescriptorId = descriptorId;

            byte[] msgBytes = new byte[wm.CalculateSize()];
            CodedOutputStream msgCos = new CodedOutputStream(msgBytes);
            wm.WriteTo(msgCos);
            msgCos.Flush();
            return msgBytes;
        }
开发者ID:infinispan,项目名称:dotnet-client,代码行数:20,代码来源:BasicTypesProtostreamMarshaller.cs


示例13: TestRoundTripNegativeEnums

        public void TestRoundTripNegativeEnums()
        {
            NegativeEnumMessage msg = new NegativeEnumMessage
            {
                Value = NegativeEnum.MinusOne,
                Values = { NegativeEnum.NEGATIVE_ENUM_ZERO, NegativeEnum.MinusOne, NegativeEnum.FiveBelow },
                PackedValues = { NegativeEnum.NEGATIVE_ENUM_ZERO, NegativeEnum.MinusOne, NegativeEnum.FiveBelow }
            };

            Assert.AreEqual(58, msg.CalculateSize());

            byte[] bytes = new byte[58];
            CodedOutputStream output = new CodedOutputStream(bytes);

            msg.WriteTo(output);
            Assert.AreEqual(0, output.SpaceLeft);

            NegativeEnumMessage copy = NegativeEnumMessage.Parser.ParseFrom(bytes);
            Assert.AreEqual(msg, copy);
        }
开发者ID:mockingbirdnest,项目名称:protobuf,代码行数:20,代码来源:TestCornerCases.cs


示例14: SimpleProtobufUsage

        public void SimpleProtobufUsage ()
        {
            const string SERVICE = "a";
            const string METHOD = "b";

            var request = new Request ();
            request.Service = SERVICE;
            request.Procedure = METHOD;

            Assert.AreEqual (METHOD, request.Procedure);
            Assert.AreEqual (SERVICE, request.Service);

            var buffer = new byte [request.CalculateSize ()];
            var stream = new CodedOutputStream (buffer);
            request.WriteTo (stream);

            Request reqCopy = Request.Parser.ParseFrom (buffer);

            Assert.AreEqual (METHOD, reqCopy.Procedure);
            Assert.AreEqual (SERVICE, reqCopy.Service);
        }
开发者ID:paperclip,项目名称:krpc,代码行数:21,代码来源:SchemaTest.cs


示例15: SetUp

        public void SetUp ()
        {
            // Create a request object and get the binary representation of it
            expectedRequest = new Request ("TestService", "ProcedureNoArgsNoReturn");
            using (var stream = new MemoryStream ()) {
                var codedStream = new CodedOutputStream (stream, true);
                codedStream.WriteInt32 (expectedRequest.ToProtobufMessage ().CalculateSize ());
                expectedRequest.ToProtobufMessage ().WriteTo (codedStream);
                codedStream.Flush ();
                requestBytes = stream.ToArray ();
            }

            // Create a response object and get the binary representation of it
            expectedResponse = new Response ();
            expectedResponse.Error = "SomeErrorMessage";
            expectedResponse.Time = 42;
            using (var stream = new MemoryStream ()) {
                var codedStream = new CodedOutputStream (stream, true);
                codedStream.WriteInt32 (expectedResponse.ToProtobufMessage ().CalculateSize ());
                expectedResponse.ToProtobufMessage ().WriteTo (codedStream);
                codedStream.Flush ();
                responseBytes = stream.ToArray ();
            }
        }
开发者ID:paperclip,项目名称:krpc,代码行数:24,代码来源:RPCStreamTest.cs


示例16: MapNonContiguousEntries

        public void MapNonContiguousEntries()
        {
            var memoryStream = new MemoryStream();
            var output = new CodedOutputStream(memoryStream);

            // Message structure:
            // Entry for MapInt32Int32
            // Entry for MapStringString
            // Entry for MapInt32Int32

            // First entry
            var key1 = 10;
            var value1 = 20;
            output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
            output.WriteLength(4);
            output.WriteTag(1, WireFormat.WireType.Varint);
            output.WriteInt32(key1);
            output.WriteTag(2, WireFormat.WireType.Varint);
            output.WriteInt32(value1);

            // Second entry
            var key2 = "a";
            var value2 = "b";
            output.WriteTag(TestMap.MapStringStringFieldNumber, WireFormat.WireType.LengthDelimited);
            output.WriteLength(6); // 3 bytes per entry: tag, size, character
            output.WriteTag(1, WireFormat.WireType.LengthDelimited);
            output.WriteString(key2);
            output.WriteTag(2, WireFormat.WireType.LengthDelimited);
            output.WriteString(value2);

            // Third entry
            var key3 = 15;
            var value3 = 25;
            output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
            output.WriteLength(4);
            output.WriteTag(1, WireFormat.WireType.Varint);
            output.WriteInt32(key3);
            output.WriteTag(2, WireFormat.WireType.Varint);
            output.WriteInt32(value3);

            output.Flush();
            var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
            var expected = new TestMap
            {
                MapInt32Int32 = { { key1, value1 }, { key3, value3 } },
                MapStringString = { { key2, value2 } }
            };
            Assert.AreEqual(expected, parsed);
        }
开发者ID:senyu0210,项目名称:protobuf,代码行数:49,代码来源:GeneratedMessageTest.cs


示例17: MapFieldOrderIsIrrelevant

        public void MapFieldOrderIsIrrelevant()
        {
            var memoryStream = new MemoryStream();
            var output = new CodedOutputStream(memoryStream);

            output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);

            var key = 10;
            var value = 20;

            // Each field can be represented in a single byte, with a single byte tag.
            // Total message size: 4 bytes.
            output.WriteLength(4);
            output.WriteTag(2, WireFormat.WireType.Varint);
            output.WriteInt32(value);
            output.WriteTag(1, WireFormat.WireType.Varint);
            output.WriteInt32(key);
            output.Flush();

            var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
            Assert.AreEqual(value, parsed.MapInt32Int32[key]);
        }
开发者ID:senyu0210,项目名称:protobuf,代码行数:22,代码来源:GeneratedMessageTest.cs


示例18: MapIgnoresExtraFieldsWithinEntryMessages

        public void MapIgnoresExtraFieldsWithinEntryMessages()
        {
            // Hand-craft the stream to contain a single entry with three fields
            var memoryStream = new MemoryStream();
            var output = new CodedOutputStream(memoryStream);

            output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);

            var key = 10; // Field 1 
            var value = 20; // Field 2
            var extra = 30; // Field 3

            // Each field can be represented in a single byte, with a single byte tag.
            // Total message size: 6 bytes.
            output.WriteLength(6);
            output.WriteTag(1, WireFormat.WireType.Varint);
            output.WriteInt32(key);
            output.WriteTag(2, WireFormat.WireType.Varint);
            output.WriteInt32(value);
            output.WriteTag(3, WireFormat.WireType.Varint);
            output.WriteInt32(extra);
            output.Flush();

            var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
            Assert.AreEqual(value, parsed.MapInt32Int32[key]);
        }
开发者ID:senyu0210,项目名称:protobuf,代码行数:26,代码来源:GeneratedMessageTest.cs


示例19: MapWithOnlyValue

        public void MapWithOnlyValue()
        {
            // Hand-craft the stream to contain a single entry with just a value.
            var memoryStream = new MemoryStream();
            var output = new CodedOutputStream(memoryStream);
            output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited);
            var nestedMessage = new ForeignMessage { C = 20 };
            // Size of the entry (tag, size written by WriteMessage, data written by WriteMessage)
            output.WriteLength(2 + nestedMessage.CalculateSize());
            output.WriteTag(2, WireFormat.WireType.LengthDelimited);
            output.WriteMessage(nestedMessage);
            output.Flush();

            var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray());
            Assert.AreEqual(nestedMessage, parsed.MapInt32ForeignMessage[0]);
        }
开发者ID:senyu0210,项目名称:protobuf,代码行数:16,代码来源:GeneratedMessageTest.cs


示例20: TestNetwork

 private static void TestNetwork()
 {
     var info = new NetMessage.PB_UserInfo();
     info.Guid = 1000L;
     info.Level = 5;
     info.NickName = "limotao";
     byte[] data = new byte[info.CalculateSize()];
     var buff = new Google.Protobuf.CodedOutputStream(data);
     info.WriteTo(buff);
     Console.ReadKey();
     string message = Convert.ToBase64String(data);
     //mSocketClient.Send(data, 0, data.Length);
     mSocketClient.Send(message);
     //Google.Protobuf.MessageParser<NetMessage.PB_UserInfo>()
     Console.ReadKey();
     mSocketClient.Close(0, "logout!");
 }
开发者ID:CNC-Not-Coder,项目名称:learning,代码行数:17,代码来源:Program.cs



注:本文中的Google.Protobuf.CodedOutputStream类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Multiplayer.TurnBasedMatch类代码示例发布时间:2022-05-26
下一篇:
C# Protobuf.CodedInputStream类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap