예제 #1
0
        public bool Encode(DynamicMessage msg, Stream stream)
        {
            lastError.Clear();
            if (null == msg)
            {
                lastError.AddLast("DynamicMessage can not be null");
                return(false);
            }

            if (false == msg.Serialize(stream))
            {
                lastError.AddLast(msg.LastError);
                return(false);
            }

            return(true);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        public DynamicMessage MutableMessage(FieldDescriptorProto desc)
        {
            if (desc.type != FieldDescriptorProto.Type.TYPE_MESSAGE)
            {
                lastError.Clear();
                lastError.AddLast(string.Format("field {0} is not a message", desc.type_name));
                return(null);
            }

            DynamicMessage ret = GetFieldMessage(desc);

            if (null != ret)
            {
                return(ret);
            }

            ret = CreateMessage(desc);
            if (AddField(desc, ret))
            {
                return(ret);
            }
            return(null);
        }
예제 #5
0
        /// <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);
        }
예제 #6
0
        public bool Reload()
        {
            Clear();

            string full_path = FileTools.GetDocFilePath(fileName);

            try {
                var header_desc = factory.GetMsgDiscriptor("com.owent.xresloader.pb.xresloader_datablocks");
                if (null == header_desc)
                {
                    LitLogger.ErrorFormat("load configure file {0} failed, com.owent.xresloader.pb.xresloader_datablocks not registered", fileName);
                    return(false);
                }

                var msg_desc = factory.GetMsgDiscriptor(protocol);
                if (null == msg_desc)
                {
                    LitLogger.ErrorFormat("load configure file {0} failed, {1} not registered", fileName, protocol);
                    return(false);
                }

                DynamicMessage data_set = factory.Decode(header_desc, File.OpenRead(full_path));
                if (null == data_set)
                {
                    LitLogger.ErrorFormat("load configure file {0} failed, {1}", fileName, factory.LastError);
                    return(false);
                }

                foreach (var cfg_item in data_set.GetFieldList("data_block"))
                {
                    DynamicMessage data_item = factory.Decode(msg_desc, new MemoryStream((byte[])cfg_item));
                    if (null == data_item)
                    {
                        LitLogger.ErrorFormat("load configure file {0} failed, {1}", fileName, factory.LastError);
                        continue;
                    }

                    bool filter_pass = true;
                    foreach (var fn in filters)
                    {
                        filter_pass = fn(data_item);
                        if (!filter_pass)
                        {
                            break;
                        }
                    }

                    if (!filter_pass)
                    {
                        continue;
                    }

                    datas.Add(data_item);

                    foreach (var index in kvIndex)
                    {
                        if (null != index.Handle)
                        {
                            Key key = index.Handle(data_item);
                            index.Index[key] = data_item;
                        }
                    }

                    foreach (var index in klIndex)
                    {
                        if (null != index.Handle)
                        {
                            List <DynamicMessage> ls;
                            Key key = index.Handle(data_item);
                            if (index.Index.TryGetValue(key, out ls))
                            {
                                ls.Add(data_item);
                            }
                            else
                            {
                                index.Index[key] = new List <DynamicMessage> {
                                    data_item
                                };
                            }
                        }
                    }
                }

                foreach (var index in klIndex)
                {
                    if (null != index.SortRule)
                    {
                        foreach (var ls in index.Index)
                        {
                            ls.Value.Sort(index.SortRule);
                        }
                    }
                }
            } catch (Exception e) {
                LitLogger.ErrorFormat("{0}", e.Message);
                return(false);
            }
            return(true);
        }