Пример #1
0
        public override async Task <Response> SendAsync(object message, bool sent)
        {
            //Console.WriteLine("step6:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            if (_sendReconnect && !IsConnected)
            {
                await ConnectAsync();
            }
            //Console.WriteLine("step7:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            IChannel channel = GetChannel();

            //TODO Can the value returned by getChannel() be null? need improvement.
            if (channel == null || !channel.IsConnected)
            {
                throw new RemotingException(this, "message can not send, because channel is closed . url:" + Url);
            }

            var id = (message as Request)?.Mid ?? 0;

            InvocationUtils.SetInvocation(id, (message as Request)?.Mdata);
            //Console.WriteLine("step8:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            var task = RegisterResultCallbackAsync(id);//todo user msg.id

            try
            {
                //Console.WriteLine("step9:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                await channel.SendAsync(message, sent);

                // Console.WriteLine("step10:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                return(await task);
            }
            catch (Exception e)
            {
                _logger.Error(e);
                throw;
            }
        }
Пример #2
0
        protected override object DecodeBody(IChannel channel, MemoryStream input, byte[] header)
        {
            //Console.WriteLine("decode step4:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            byte           flag = header[2], proto = (byte)(flag & SerializationMask);
            ISerialization s = GetSerialization(channel);
            //Console.WriteLine("decode step5:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            // get request id.
            var id = ByteUtil.Bytes2Long(header, 4);

            if ((flag & FlagRequest) == 0)
            {
                // decode response.
                Response res = new Response(id);
                if ((flag & FlagRequest) != 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, Deserialize(s, input));
                        }
                        else if (res.Mevent)
                        {
                            data = DecodeEventData(channel, Deserialize(s, input));
                        }
                        else
                        {
                            DecodeableRpcResult result;
                            if (channel.Url.GetParameter(Constants.DecodeInIoThreadKey, Constants.DefaultDecodeInIoThread))
                            {
                                //Console.WriteLine("decode step6:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                                result = new DecodeableRpcResult(channel, res, input,
                                                                 (IInvocation)InvocationUtils.GetInvocation(id), proto);
                                // Console.WriteLine("decode step7:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                                result.Decode();
                                //Console.WriteLine("decode step8:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                            }
                            else
                            {
                                result = new DecodeableRpcResult(channel, res, input,
                                                                 (IInvocation)InvocationUtils.GetInvocation(id), proto);
                            }
                            data = result;
                        }
                        res.Mresult = data;
                    }
                    catch (Exception t)
                    {
                        res.Mstatus   = Response.ClientError;
                        res.MerrorMsg = t.ToString();
                    }
                }
                else
                {
                    res.MerrorMsg = Deserialize(s, input).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, Deserialize(s, input));
                }
                else if (req.IsEvent)
                {
                    data = DecodeEventData(channel, Deserialize(s, input));
                }
                else
                {
                    DecodeableRpcInvocation inv;
                    if (channel.Url.GetParameter(Constants.DecodeInIoThreadKey, Constants.DefaultDecodeInIoThread))
                    {
                        inv = new DecodeableRpcInvocation(channel, req, input, proto);
                        inv.Decode();
                    }
                    else
                    {
                        inv = new DecodeableRpcInvocation(channel, req, input, proto);
                    }
                    data = inv;
                }
                req.Mdata = (data);
            }
            catch (Exception t)
            {
                // bad request
                req.IsBroken = (true);
                req.Mdata    = (t);
            }
            return(req);
        }