Ejemplo n.º 1
0
        protected override string GetSessionIdFromMessage(AmpMessage message)
        {
            if (message.Code == 0)
            {
                if (message != null)
                {
                    var rspTemp = ProtobufObjectFactory.GetResponseTemplate(message.ServiceId, message.MessageId);
                    if (rspTemp == null)
                    {
                        return(base.GetSessionIdFromMessage(message));
                    }

                    if (message.Data != null)
                    {
                        rspTemp.MergeFrom(message.Data);
                    }
                    //提取内部的return_code 字段
                    var field_sessionId = rspTemp.Descriptor.FindFieldByName(Constants.SEESIONID_FIELD_NAME);
                    if (field_sessionId != null)
                    {
                        var ObjV = field_sessionId.Accessor.GetValue(rspTemp);
                        if (ObjV != null)
                        {
                            return(ObjV.ToString());
                        }
                    }
                }
            }
            return(base.GetSessionIdFromMessage(message));
        }
Ejemplo n.º 2
0
        protected override string MessageToJson(AmpMessage message)
        {
            if (message.Code == 0)
            {
                var    return_code    = 0;
                var    return_message = "";
                string ret            = "";
                if (message != null)
                {
                    var rspTemp = ProtobufObjectFactory.GetResponseTemplate(message.ServiceId, message.MessageId);
                    if (rspTemp == null)
                    {
                        return(ret);
                    }

                    if (message.Data != null)
                    {
                        rspTemp.MergeFrom(message.Data);
                    }
                    //提取内部的return_code 字段
                    var field_code = rspTemp.Descriptor.FindFieldByName("return_code");
                    if (field_code != null)
                    {
                        var retObjV = field_code.Accessor.GetValue(rspTemp);
                        if (retObjV != null)
                        {
                            if (!int.TryParse(retObjV.ToString(), out return_code))
                            {
                                return_code = 0;
                            }
                        }
                    }
                    //提取return_message
                    var field_msg = rspTemp.Descriptor.FindFieldByName("return_message");
                    if (field_msg != null)
                    {
                        var retObjV = field_msg.Accessor.GetValue(rspTemp);
                        if (retObjV != null)
                        {
                            return_message = retObjV.ToString();
                        }
                    }


                    ret = AmpJsonFormatter.Format(rspTemp);
                }

                return("{\"return_code\":0,\"return_message\":\"\",data:" + ret + "}");
            }
            else
            {
                return("{\"return_code\":" + message.Code + ",\"return_message\":\"\"}");
            }
        }
Ejemplo n.º 3
0
        protected override AmpMessage EncodeRequest(RequestData reqData)
        {
            ushort     serviceId = (ushort)reqData.ServiceId;
            ushort     messageId = (ushort)reqData.MessageId;
            AmpMessage message   = AmpMessage.CreateRequestMessage(serviceId, messageId);


            IMessage reqTemp = ProtobufObjectFactory.GetRequestTemplate(serviceId, messageId);

            if (reqTemp == null)
            {
                return(null);
            }

            try
            {
                var descriptor = reqTemp.Descriptor;
                if (!string.IsNullOrEmpty(reqData.RawBody))
                {
                    reqTemp = descriptor.Parser.ParseJson(reqData.RawBody);
                }

                if (reqData.Data.Count > 0)
                {
                    foreach (var field in descriptor.Fields.InDeclarationOrder())
                    {
                        if (reqData.Data.ContainsKey(field.Name))
                        {
                            field.Accessor.SetValue(reqTemp, reqData.Data[field.Name]);
                        }
                        else if (reqData.Data.ContainsKey(field.JsonName))
                        {
                            field.Accessor.SetValue(reqTemp, reqData.Data[field.JsonName]);
                        }
                    }
                }


                message.Data = reqTemp.ToByteArray();
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "从HTTP请求中解析数据错误:" + ex.Message);
                message = null;
            }

            return(message);
        }