当前位置: 首页>>代码示例>>C#>>正文


C# Protobuf.CodedOutputStream类代码示例

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


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


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