public DynamicMessage Decode(string path, Stream stream) { lastError.Clear(); DynamicMessage ret = Create(path); if (null == ret) { lastError.AddLast(string.Format("invalid protocol path {0}", path)); return(null); } if (false == ret.Parse(stream)) { lastError.AddLast(ret.LastError); return(null); } return(ret); }
public DynamicMessage Decode(DynamicMessage.MsgDiscriptor out_desc, Stream stream) { lastError.Clear(); DynamicMessage ret = Create(out_desc); if (null == ret) { lastError.AddLast(string.Format("invalid protocol path {0}.{1}", out_desc.Package, out_desc.Protocol)); return(null); } if (false == ret.Parse(stream)) { lastError.AddLast(ret.LastError); return(null); } return(ret); }
/// <summary> /// 解析protobuf数据 /// </summary> /// <param name="reader"></param> /// <see cref="https://developers.google.com/protocol-buffers/docs/encoding"/> /// <returns>全部成功返回true,部分失败或全失败返回false,这时候可以通过LastError获取失败信息</returns> public bool Parse(ProtoReader reader) { buildIndex(); lastError.Clear(); try { int field_id; SubItemToken token; while ((field_id = reader.ReadFieldHeader()) != 0) { WireType pb_type = reader.WireType; FieldDescriptorProto desc; if (false == msgDescriptor.FieldIdIndex.TryGetValue(field_id, out desc)) { // unknown field skipped reader.SkipField(); continue; } // 类型校验 try { switch (desc.type) { case FieldDescriptorProto.Type.TYPE_DOUBLE: insertField(desc, reader.ReadDouble()); break; case FieldDescriptorProto.Type.TYPE_FLOAT: insertField(desc, reader.ReadSingle()); break; case FieldDescriptorProto.Type.TYPE_INT64: case FieldDescriptorProto.Type.TYPE_SINT64: case FieldDescriptorProto.Type.TYPE_SFIXED64: insertField(desc, reader.ReadInt64()); break; case FieldDescriptorProto.Type.TYPE_UINT64: case FieldDescriptorProto.Type.TYPE_FIXED64: insertField(desc, reader.ReadUInt64()); break; case FieldDescriptorProto.Type.TYPE_INT32: case FieldDescriptorProto.Type.TYPE_SINT32: case FieldDescriptorProto.Type.TYPE_SFIXED32: insertField(desc, reader.ReadInt32()); break; case FieldDescriptorProto.Type.TYPE_BOOL: insertField(desc, reader.ReadBoolean()); break; case FieldDescriptorProto.Type.TYPE_STRING: insertField(desc, reader.ReadString()); break; //case FieldDescriptorProto.Type.TYPE_GROUP: // deprecated // break; case FieldDescriptorProto.Type.TYPE_MESSAGE: token = ProtoReader.StartSubItem(reader); try { DynamicMessage sub_msg = CreateMessage(desc); if (null == sub_msg) { lastError.AddLast(string.Format("{0}.{1}.{2} => invalid message path {3}", msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name, pickMsgName(desc))); break; } if (false == sub_msg.Parse(reader)) { lastError.AddLast(sub_msg.LastError); } else { insertField(desc, sub_msg); } } catch (Exception e) { lastError.AddLast(string.Format("{0}.{1}.{2} => {3}", msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name, e.Message)); } ProtoReader.EndSubItem(token, reader); break; case FieldDescriptorProto.Type.TYPE_BYTES: insertField(desc, ProtoReader.AppendBytes(null, reader)); break; case FieldDescriptorProto.Type.TYPE_FIXED32: case FieldDescriptorProto.Type.TYPE_UINT32: insertField(desc, reader.ReadUInt32()); break; case FieldDescriptorProto.Type.TYPE_ENUM: insertField(desc, reader.ReadInt32()); break; default: // unsupported field lastError.AddLast(string.Format("field type {0} in {1}.{2}.{3} unsupported", desc.type.ToString(), msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name)); reader.SkipField(); break; } } catch (Exception e) { lastError.AddLast(string.Format("{0}.{1}.{2} {3}", msgDescriptor.Package, msgDescriptor.Protocol.name, desc.name, e.ToString())); reader.SkipField(); } } } catch (Exception e) { lastError.AddLast(e.Message); } return(0 == lastError.Count); }