private byte[] Compress(IMessageLite message) { using (var bos = new MemoryStream(BufferSize)) using (var gzipStream = new GZipStream(bos, CompressionMode.Compress)) { message.WriteTo(gzipStream); gzipStream.Close(); return bos.ToArray(); } }
public void WriteUnknownGroup(int fieldNumber, IMessageLite value) { WriteTag(fieldNumber, WireFormat.WireType.StartGroup); value.WriteTo(this); WriteTag(fieldNumber, WireFormat.WireType.EndGroup); }
/// <summary> /// Compute the number of bytes that would be needed to encode an /// embedded message field, including the tag. /// </summary> public static int ComputeMessageSizeNoTag(IMessageLite value) { int size = value.SerializedSize; return(ComputeRawVarint32Size((uint)size) + size); }
protected override bool ParseUnknownField(CodedInputStream input, ExtensionRegistry extensionRegistry, uint tag) { FieldSet extensions = MessageBeingBuilt.Extensions; WireFormat.WireType wireType = WireFormat.GetTagWireType(tag); int fieldNumber = WireFormat.GetTagFieldNumber(tag); IGeneratedExtensionLite extension = extensionRegistry[DefaultInstanceForType, fieldNumber]; bool unknown = false; bool packed = false; if (extension == null) { unknown = true; // Unknown field. } else if (wireType == FieldMappingAttribute.WireTypeFromFieldType(extension.Descriptor.FieldType, false /* isPacked */)) { packed = false; // Normal, unpacked value. } else if (extension.Descriptor.IsRepeated && //?? just returns true ?? extension.Descriptor.type.isPackable() && wireType == FieldMappingAttribute.WireTypeFromFieldType(extension.Descriptor.FieldType, true /* isPacked */)) { packed = true; // Packed value. } else { unknown = true; // Wrong wire type. } if (unknown) // Unknown field or wrong wire type. Skip. { return(input.SkipField(tag)); } if (packed) { int length = (int)Math.Min(int.MaxValue, input.ReadRawVarint32()); int limit = input.PushLimit(length); if (extension.Descriptor.FieldType == FieldType.Enum) { while (!input.ReachedLimit) { int rawValue = input.ReadEnum(); Object value = extension.Descriptor.EnumType.FindValueByNumber(rawValue); if (value == null) { // If the number isn't recognized as a valid value for this // enum, drop it (don't even add it to unknownFields). return(true); } extensions.AddRepeatedField(extension.Descriptor, value); } } else { while (!input.ReachedLimit) { Object value = input.ReadPrimitiveField(extension.Descriptor.FieldType); extensions.AddRepeatedField(extension.Descriptor, value); } } input.PopLimit(limit); } else { Object value; switch (extension.Descriptor.MappedType) { case MappedType.Message: { IBuilderLite subBuilder = null; if (!extension.Descriptor.IsRepeated) { IMessageLite existingValue = extensions[extension.Descriptor] as IMessageLite; if (existingValue != null) { subBuilder = existingValue.WeakToBuilder(); } } if (subBuilder == null) { subBuilder = extension.MessageDefaultInstance.WeakCreateBuilderForType(); } if (extension.Descriptor.FieldType == FieldType.Group) { input.ReadGroup(extension.Number, subBuilder, extensionRegistry); } else { input.ReadMessage(subBuilder, extensionRegistry); } value = subBuilder.WeakBuild(); break; } case MappedType.Enum: int rawValue = input.ReadEnum(); value = extension.Descriptor.EnumType.FindValueByNumber(rawValue); // If the number isn't recognized as a valid value for this enum, // drop it. if (value == null) { return(true); } break; default: value = input.ReadPrimitiveField(extension.Descriptor.FieldType); break; } if (extension.Descriptor.IsRepeated) { extensions.AddRepeatedField(extension.Descriptor, value); } else { extensions[extension.Descriptor] = value; } } return(true); }
/* * Compute the number of bytes that would be needed to encode a * MessageSet extension to the stream. For historical reasons, * the wire format differs from normal fields. */ /// <summary> /// Compute the number of bytes that would be needed to encode a /// MessageSet extension to the stream. For historical reasons, /// the wire format differs from normal fields. /// </summary> public static int ComputeMessageSetExtensionSize(int fieldNumber, IMessageLite value) { return ComputeTagSize(WireFormat.MessageSetField.Item)*2 + ComputeUInt32Size(WireFormat.MessageSetField.TypeID, (uint) fieldNumber) + ComputeMessageSize(WireFormat.MessageSetField.Message, value); }
public static int ComputeUnknownGroupSize(int fieldNumber, IMessageLite value) { return ComputeTagSize(fieldNumber)*2 + value.SerializedSize; }
private byte[] Compress(IMessageLite proto) { using (var memStream = new MemoryStream(BufferSize)) using (var gzip = new GZipStream(memStream, CompressionLevel.Fastest)) { proto.WriteTo(gzip); gzip.Flush(); memStream.Position = 0; return memStream.ToArray(); } }
/// <summary> /// Writes the message to the the formatted stream. /// </summary> public override void WriteMessage(IMessageLite message) { message.WriteTo(this); }
/// <summary> /// Writes a group field value, without a tag, to the stream. /// </summary> public void WriteGroupNoTag(IMessageLite value) { value.WriteTo(this); }
public void WriteMessageSetExtension(int fieldNumber, IMessageLite value) { WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.StartGroup); WriteUInt32(WireFormat.MessageSetField.TypeID, (uint)fieldNumber); WriteMessage(WireFormat.MessageSetField.Message, value); WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.EndGroup); }
public void WriteMessage(int fieldNumber, IMessageLite value) { WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited); WriteRawVarint32((uint)value.SerializedSize); value.WriteTo(this); }
/// <summary> /// Called by subclasses to parse an unknown field or an extension. /// </summary> /// <returns>true unless the tag is an end-group tag</returns> protected override bool ParseUnknownField(ICodedInputStream input, ExtensionRegistry extensionRegistry, uint tag, string fieldName) { FieldSet extensions = MessageBeingBuilt.Extensions; WireFormat.WireType wireType = WireFormat.GetTagWireType(tag); int fieldNumber = WireFormat.GetTagFieldNumber(tag); IGeneratedExtensionLite extension = extensionRegistry[DefaultInstanceForType, fieldNumber]; if (extension == null) //unknown field { return(input.SkipField()); } IFieldDescriptorLite field = extension.Descriptor; // Unknown field or wrong wire type. Skip. if (field == null) { return(input.SkipField()); } WireFormat.WireType expectedType = field.IsPacked ? WireFormat.WireType.LengthDelimited : WireFormat.GetWireType(field.FieldType); if (wireType != expectedType) { expectedType = WireFormat.GetWireType(field.FieldType); if (wireType == expectedType) { //Allowed as of 2.3, this is unpacked data for a packed array } else if (field.IsRepeated && wireType == WireFormat.WireType.LengthDelimited && (expectedType == WireFormat.WireType.Varint || expectedType == WireFormat.WireType.Fixed32 || expectedType == WireFormat.WireType.Fixed64)) { //Allowed as of 2.3, this is packed data for an unpacked array } else { return(input.SkipField()); } } if (!field.IsRepeated && wireType != WireFormat.GetWireType(field.FieldType)) //invalid wire type { return(input.SkipField()); } switch (field.FieldType) { case FieldType.Group: case FieldType.Message: { if (!field.IsRepeated) { IMessageLite message = extensions[extension.Descriptor] as IMessageLite; IBuilderLite subBuilder = (message ?? extension.MessageDefaultInstance).WeakToBuilder(); if (field.FieldType == FieldType.Group) { input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry); } else { input.ReadMessage(subBuilder, extensionRegistry); } extensions[field] = subBuilder.WeakBuild(); } else { List <IMessageLite> list = new List <IMessageLite>(); if (field.FieldType == FieldType.Group) { input.ReadGroupArray(tag, fieldName, list, extension.MessageDefaultInstance, extensionRegistry); } else { input.ReadMessageArray(tag, fieldName, list, extension.MessageDefaultInstance, extensionRegistry); } foreach (IMessageLite m in list) { extensions.AddRepeatedField(field, m); } return(true); } break; } case FieldType.Enum: { if (!field.IsRepeated) { object unknown; IEnumLite value = null; if (input.ReadEnum(ref value, out unknown, field.EnumType)) { extensions[field] = value; } } else { ICollection <object> unknown; List <IEnumLite> list = new List <IEnumLite>(); input.ReadEnumArray(tag, fieldName, list, out unknown, field.EnumType); foreach (IEnumLite en in list) { extensions.AddRepeatedField(field, en); } } break; } default: { if (!field.IsRepeated) { object value = null; if (input.ReadPrimitiveField(field.FieldType, ref value)) { extensions[field] = value; } } else { List <object> list = new List <object>(); input.ReadPrimitiveArray(field.FieldType, tag, fieldName, list); foreach (object oval in list) { extensions.AddRepeatedField(field, oval); } } break; } } return(true); }
public IGeneratedExtensionLite FindByName(IMessageLite defaultInstanceOfType, string fieldName) { return(FindExtensionByName(defaultInstanceOfType, fieldName)); }
/// <summary> /// Writes a message as an element using the name defined in <see cref="RootElementName"/> /// </summary> public override void WriteMessage(IMessageLite message) { WriteMessage(_rootElementName, message); }
/// <summary> /// Writes a message /// </summary> protected override void WriteMessageOrGroup(string field, IMessageLite message) { _output.WriteStartElement(field); if (TestOption(XmlWriterOptions.OutputJsonTypes)) { _output.WriteAttributeString("type", "object"); } message.WriteTo(this); _output.WriteEndElement(); }
public void WriteMessageNoTag(IMessageLite value) { WriteRawVarint32((uint)value.SerializedSize); value.WriteTo(this); }
public ProtobufDecoder(IMessageLite prototype) : this(prototype, null) { }
/// <summary> /// For Lite exceptions that do not known how to enumerate missing fields /// </summary> public UninitializedMessageException(IMessageLite message) : base(String.Format("Message {0} is missing required fields", message.GetType())) { missingFields = new List <string>(); }
/// <summary> /// Writes a message /// </summary> protected override void WriteMessageOrGroup(string field, IMessageLite message) { WriteName(field); WriteMessage(message); }
public override TBuilder MergeFrom(IMessageLite other) { //do nothing, Lite runtime does not support cross-message merges return(ThisBuilder); }
public static int ComputeUnknownGroupSizeNoTag(IMessageLite value) { return value.SerializedSize; }
public static byte[] Encode(IMessageLite messageLite) { return(messageLite.ToByteArray()); }
/// <summary> /// Verifies that the given object is of the correct type to be a valid /// value for the given field. /// </summary> /// <remarks> /// For repeated fields, this checks if the object is of the right /// element type, not whether it's a list. /// </remarks> /// <exception cref="ArgumentException">The value is not of the right type.</exception> /// <exception cref="ArgumentNullException">The value is null.</exception> private static void VerifyType(IFieldDescriptorLite field, object value) { ThrowHelper.ThrowIfNull(value, "value"); bool isValid = false; switch (field.MappedType) { case MappedType.Int32: isValid = value is int; break; case MappedType.Int64: isValid = value is long; break; case MappedType.UInt32: isValid = value is uint; break; case MappedType.UInt64: isValid = value is ulong; break; case MappedType.Single: isValid = value is float; break; case MappedType.Double: isValid = value is double; break; case MappedType.Boolean: isValid = value is bool; break; case MappedType.String: isValid = value is string; break; case MappedType.ByteString: isValid = value is ByteString; break; case MappedType.Enum: IEnumLite enumValue = value as IEnumLite; isValid = enumValue != null && field.EnumType.IsValidValue(enumValue); break; case MappedType.Message: IMessageLite messageValue = value as IMessageLite; isValid = messageValue != null; break; } if (!isValid) { // When chaining calls to SetField(), it can be hard to tell from // the stack trace which exact call failed, since the whole chain is // considered one line of code. So, let's make sure to include the // field name and other useful info in the exception. string message = "Wrong object type used with protocol message reflection."; throw new ArgumentException(message); } }
public void Run1(AddressBook addressBook, bool isCompositeBuffer) { AddressBook.Builder builder = AddressBook.CreateBuilder(); IMessageLite protoType = builder.DefaultInstanceForType; var channel = new EmbeddedChannel( new ProtobufVarint32FrameDecoder(), new ProtobufDecoder(protoType, null), new ProtobufVarint32LengthFieldPrepender(), new ProtobufEncoder()); Assert.True(channel.WriteOutbound(addressBook)); var buffer = channel.ReadOutbound <IByteBuffer>(); Assert.NotNull(buffer); Assert.True(buffer.ReadableBytes > 0); var data = new byte[buffer.ReadableBytes]; buffer.ReadBytes(data); IByteBuffer inputBuffer; if (isCompositeBuffer) { inputBuffer = new CompositeByteBuffer(UnpooledByteBufferAllocator.Default, 2, Unpooled.CopiedBuffer(data, 0, 2), Unpooled.CopiedBuffer(data, 2, data.Length - 2)); } else { inputBuffer = Unpooled.WrappedBuffer(data); } Assert.True(channel.WriteInbound(inputBuffer)); var message = channel.ReadInbound <IMessage>(); Assert.NotNull(message); Assert.IsType <AddressBook>(message); var roundTripped = (AddressBook)message; Assert.Equal(addressBook.PersonList.Count, roundTripped.PersonList.Count); for (int i = 0; i < addressBook.PersonList.Count; i++) { Assert.Equal(addressBook.PersonList[i].Id, roundTripped.PersonList[i].Id); Assert.Equal(addressBook.PersonList[i].Email, roundTripped.PersonList[i].Email); Assert.Equal(addressBook.PersonList[i].Name, roundTripped.PersonList[i].Name); Assert.Equal(addressBook.PersonList[i].PhoneList.Count, roundTripped.PersonList[i].PhoneList.Count); for (int j = 0; j < addressBook.PersonList[i].PhoneList.Count; j++) { Assert.Equal(addressBook.PersonList[i].PhoneList[j].Type, roundTripped.PersonList[i].PhoneList[j].Type); Assert.Equal(addressBook.PersonList[i].PhoneList[j].Number, roundTripped.PersonList[i].PhoneList[j].Number); } } Assert.False(channel.Finish()); }
public static int ComputeUnknownGroupSizeNoTag(IMessageLite value) { return(value.SerializedSize); }
/* * Compute the number of bytes that would be needed to encode a * MessageSet extension to the stream. For historical reasons, * the wire format differs from normal fields. */ /// <summary> /// Compute the number of bytes that would be needed to encode a /// MessageSet extension to the stream. For historical reasons, /// the wire format differs from normal fields. /// </summary> public static int ComputeMessageSetExtensionSize(int fieldNumber, IMessageLite value) { return(ComputeTagSize(WireFormat.MessageSetField.Item) * 2 + ComputeUInt32Size(WireFormat.MessageSetField.TypeID, (uint)fieldNumber) + ComputeMessageSize(WireFormat.MessageSetField.Message, value)); }
/// <summary> /// Writes a message as an element with the given name /// </summary> public void WriteMessage(string elementName, IMessageLite message) { WriteMessageStart(elementName); message.WriteTo(this); WriteMessageEnd(); }
/// <summary> /// Writes the message to the the formatted stream. /// </summary> public override void WriteMessage(IMessageLite message) { WriteMessageStart(); message.WriteTo(this); WriteMessageEnd(); }
/// <summary> /// Writes a message or group as a field /// </summary> protected override void WriteMessageOrGroup(string field, IMessageLite message) { DictionaryWriter writer = Create(); writer.WriteMessage(message); _output[field] = writer.ToDictionary(); }
public void process(byte[] datas, MessageLength length) { Debug.Log("process receive Data " + length + " state " + state); MessageLength totallen = 0; while (length > 0 && expectSize > 0) { if (state == READ_STATE.READ_STATE_FLAG) //1 { if (length >= expectSize) { Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize); totallen += expectSize; stream.wpos += (int)expectSize; length -= expectSize; flag = stream.readUint8(); stream.clear(); state = READ_STATE.READ_STATE_MSGLEN; expectSize = 4; } else { Array.Copy(datas, totallen, stream.data(), stream.wpos, length); stream.wpos += (int)length; expectSize -= length; break; } } else if (state == READ_STATE.READ_STATE_MSGLEN) //4 { if (length >= expectSize) { Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize); totallen += expectSize; stream.wpos += (int)expectSize; length -= expectSize; msglen = stream.readUint32(); stream.clear(); state = READ_STATE.READ_STATE_FLOWID; expectSize = 4; } else { Array.Copy(datas, totallen, stream.data(), stream.wpos, length); stream.wpos += (int)length; expectSize -= length; break; } } else if (state == READ_STATE.READ_STATE_FLOWID) //4 { if (length >= expectSize) { Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize); totallen += expectSize; stream.wpos += (int)expectSize; length -= expectSize; flowId = stream.readUint32(); stream.clear(); state = READ_STATE.READ_STATE_MODULEID; expectSize = 1; } else { Array.Copy(datas, totallen, stream.data(), stream.wpos, length); stream.wpos += (int)length; expectSize -= length; break; } } else if (state == READ_STATE.READ_STATE_MODULEID) //1 { if (length >= expectSize) { Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize); totallen += expectSize; stream.wpos += (int)expectSize; length -= expectSize; moduleId = stream.readUint8(); stream.clear(); state = READ_STATE.READ_STATE_MSGID; expectSize = 2; } else { Array.Copy(datas, totallen, stream.data(), stream.wpos, length); stream.wpos += (int)length; expectSize -= length; break; } } else if (state == READ_STATE.READ_STATE_MSGID) //2 { if (length >= expectSize) { Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize); totallen += expectSize; stream.wpos += (int)expectSize; length -= expectSize; msgid = stream.readUint16(); stream.clear(); state = READ_STATE.READ_STATE_BODY; expectSize = msglen - 4 - 1 - 2; } else { Array.Copy(datas, totallen, stream.data(), stream.wpos, length); stream.wpos += (int)length; expectSize -= length; break; } } /* * body Can be empty handle body immediately */ if (state == READ_STATE.READ_STATE_BODY) { if (length >= expectSize) { Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize); totallen += expectSize; stream.wpos += (int)expectSize; length -= expectSize; /* * No Handler Or PushMessage forward To IPacketHandler * Call Who's RPC Method Or Register Many RPC Method to Handle It ? * [PushHandler] * void GCPushSpriteInfo(Packet packet) { * } * * PacketHandler namespace * IPacketHandler---->GCPushSpriteInfo */ ChuMeng.ServerMsgReader.MessageHandler handler = msgHandle; //KBEngine.Message msg = new KBEngine.Message(); IMessageLite pbmsg = KBEngine.Message.handlePB(moduleId, msgid, stream); KBEngine.Packet p = new KBEngine.Packet(flag, msglen, flowId, moduleId, msgid, 0, 0, pbmsg); //var fullName = pbmsg.GetType().FullName; KBEngine.KBEngineApp.app.queueInLoop(delegate() { handler(p); }); stream.clear(); state = READ_STATE.READ_STATE_FLAG; expectSize = 1; } else { Array.Copy(datas, totallen, stream.data(), stream.wpos, length); stream.wpos += (int)length; expectSize -= length; break; } } } Debug.Log("server state after " + state + " msglen " + msglen + " " + length); Debug.Log(" server MessageReader:: prop flag" + flag + " msglen " + msglen + " flowId " + flowId + " moduleId " + moduleId + " msgid " + msgid + " responseTime " + 0 + " responseFlag " + 0 + " expectSize " + expectSize); }
internal ExtensionInfo(FieldDescriptor descriptor, IMessageLite defaultInstance) { Descriptor = descriptor; DefaultInstance = defaultInstance; }
public void TestIMessageLiteWeakCreateBuilderForType() { IMessageLite msg = TestRequiredLite.DefaultInstance; Assert.AreEqual(typeof(TestRequiredLite.Builder), msg.WeakCreateBuilderForType().GetType()); }
public ProtobufDecoder(IMessageLite prototype, ExtensionRegistry extensions) { _prototype = prototype; _extensions = extensions; }
public void TestMessageLiteWeakDefaultInstanceForType() { IMessageLite msg = TestRequiredLite.DefaultInstance; Assert.IsTrue(Object.ReferenceEquals(TestRequiredLite.DefaultInstance, msg.WeakDefaultInstanceForType)); }
IBuilderLite IBuilderLite.WeakMergeFrom(IMessageLite message) { return MergeFrom((UnknownFieldSet)message); }
public static int ComputeUnknownGroupSize(int fieldNumber, IMessageLite value) { return(ComputeTagSize(fieldNumber) * 2 + value.SerializedSize); }
/// <summary> /// Compute the number of bytes that would be needed to encode an /// embedded message field, including the tag. /// </summary> public static int ComputeMessageSize(int fieldNumber, IMessageLite value) { int size = value.SerializedSize; return(ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint)size) + size); }
/// <summary> /// Compute the number of bytes that would be needed to encode an /// embedded message field, including the tag. /// </summary> public static int ComputeMessageSize(int fieldNumber, IMessageLite value) { int size = value.SerializedSize; return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint) size) + size; }
/// <summary> /// Compute the number of bytes that would be needed to encode an /// embedded message field, including the tag. /// </summary> public static int ComputeMessageSizeNoTag(IMessageLite value) { int size = value.SerializedSize; return ComputeRawVarint32Size((uint) size) + size; }
/// <summary> /// For Lite exceptions that do not known how to enumerate missing fields /// </summary> public UninitializedMessageException(IMessageLite message) : base(String.Format("Message {0} is missing required fields", message.GetType())) { missingFields = new List<string>(); }
/// <summary> /// Reads an array of T messages /// </summary> public virtual bool ReadMessageArray <T>(string field, ICollection <T> items, IMessageLite messageType, ExtensionRegistry registry) { bool success = false; foreach (string next in ForeachArrayItem(field)) { IBuilderLite builder = messageType.WeakCreateBuilderForType(); if (ReadMessage(builder, registry)) { items.Add((T)builder.WeakBuild()); success |= true; } } return(success); }