Exemple #1
0
        protected virtual object DecodeBody(IChannel channel, MemoryStream input, byte[] header)
        {
            byte           flag = header[2], proto = (byte)(flag & SerializationMask);
            ISerialization s        = GetSerialization(channel);
            IObjectInput   inStream = s.Deserialize(input);
            // get request id.
            long id = ByteUtil.Bytes2Long(header, 4);

            if ((flag & FlagRequest) == 0)
            {
                // decode response.
                Response res = new Response(id);
                if ((flag & FlagEvent) != 0)
                {
                    res.SetEvent(Response.HeartbeatEvent);
                }
                // get status.
                byte status = header[3];
                res.Mstatus = status;
                if (status == Response.Ok)
                {
                    try
                    {
                        object data;
                        if (res.IsHeartBeat())
                        {
                            data = DecodeEventData(channel, inStream);
                        }
                        else if (res.Mevent)
                        {
                            data = DecodeEventData(channel, inStream);
                        }
                        else
                        {
                            data = DecodeResponseData(channel, inStream);
                        }
                        res.Mresult = data;
                    }
                    catch (Exception t)
                    {
                        res.Mstatus   = Response.ClientError;
                        res.MerrorMsg = t.ToString();
                    }
                }
                else
                {
                    res.MerrorMsg = inStream.ReadUTF();
                }
                return(res);
            }

            // decode request.
            Request req = new Request(id);

            req.Mversion = "2.0.0";
            req.IsTwoWay = (flag & FlagRequest) != 0;
            if ((flag & FlagEvent) != 0)
            {
                req.SetEvent(Request.HeartBeatEvent);
            }
            try
            {
                object data;
                if (req.IsHeartbeat())
                {
                    data = DecodeEventData(channel, inStream);
                }
                else if (req.IsEvent)
                {
                    data = DecodeEventData(channel, inStream);
                }
                else
                {
                    data = DecodeRequestData(channel, inStream);
                }

                req.Mdata = data;
            }
            catch (Exception t)
            {
                // bad request
                req.IsBroken = true;
                req.Mdata    = t;
            }
            return(req);
        }
Exemple #2
0
        public object Decode(IChannel channel, MemoryStream input)
        {
            IObjectInput inputo = CodecSupport.GetSerialization(channel.Url, _serializationType)
                                  .Deserialize(input);

            SetAttachment(Constants.DubboVersionKey, inputo.ReadUTF());
            SetAttachment(Constants.PathKey, inputo.ReadUTF());
            SetAttachment(Constants.VersionKey, inputo.ReadUTF());

            MethodName = (inputo.ReadUTF());
            try
            {
                object[] args;
                Type[]   pts;
                string   desc = inputo.ReadUTF();
                if (desc.Length == 0)
                {
                    pts  = DubboCodec.EmptyClassArray;
                    args = DubboCodec.EmptyObjectArray;
                }
                else
                {
                    pts  = ReflectUtil.Desc2ClassArray(desc);
                    args = new object[pts.Length];
                    for (int i = 0; i < args.Length; i++)
                    {
                        try
                        {
                            args[i] = inputo.ReadObject(pts[i]);
                        }
                        catch (Exception)
                        {
                            //
                        }
                    }
                }
                ParameterTypes = (pts);

                Dictionary <string, string> map = inputo.ReadObject <Dictionary <string, string> >();
                if (map != null && map.Count > 0)
                {
                    Dictionary <string, string> attachment = Attachments;
                    if (attachment == null)
                    {
                        attachment = new Dictionary <string, string>();
                    }

                    foreach (var kv in map)
                    {
                        attachment.Add(kv.Key, kv.Value);
                    }

                    Attachments = attachment;
                }
                //decode argument ,may be callback
                for (int i = 0; i < args.Length; i++)
                {
                    args[i] = inputo.ReadObject();
                }

                Arguments = args;
            } catch (Exception e) {
                throw new IOException("Read invocation data failed." + e);
            } finally {
            }
            return(this);
        }