Ejemplo n.º 1
0
        private void InternalOnMessage(object sender, EndpointContext context)
        {
            if (this.Log.IsDebugEnabled())
            {
                this.Log.Debug("messsage from {0}: {1}", context.MessageFrom, this.Dump(context.Message));
            }
            if (this.OnMessage == null)
            {
                return;
            }

            ThreadPool.QueueUserWorkItem(o =>
            {
                if (!this.running)
                {
                    if (this.Log.IsDebugEnabled())
                    {
                        this.Log.Debug(string.Format("message dropped as client closed: {0}", this.Dump(context.Message)));
                    }
                    return;
                }

                Message msg = this.ParseMessage(context.Message);
                var args    = new MessageArgs(msg, m => this.Confirm(m));
                var sw      = new Stopwatch();
                try
                {
                    sw.Start();
                    this.OnMessage(this, args);
                    sw.Stop();
                }
                catch (Exception e)
                {
                    args.Fail(e.Message);
                }

                if (args._isFail)
                {
                    if (this.Log.IsDebugEnabled())
                    {
                        this.Log.Debug("process message error: {0}", args._reason);
                    }
                    return;
                }

                // prevent confirm attach
                if (sw.ElapsedMilliseconds <= 1)
                {
                    if (this.Log.IsDebugEnabled())
                    {
                        this.Log.Debug("maybe too fast or attack? server maybe reject your request");
                    }
                    Thread.Sleep(10);
                }

                if (args._isConfirmed)
                {
                    return;
                }
                try
                {
                    this.Confirm(msg);
                    if (this.Log.IsDebugEnabled())
                    {
                        this.Log.Debug("confirm message: {0}", this.Dump(context.Message));
                    }
                }
                catch (Exception e)
                {
                    this.Log.Warn(string.Format("confirm message {0} error {1}", this.Dump(context.Message), e.StackTrace));
                }
            });
        }
Ejemplo n.º 2
0
        private void InternalOnMessage(object sender, EndpointContext context)
        {
            if (enableTraceLog)
            {
                this.Log.Info("messsage from {0}: {1}", context.MessageFrom, this.Dump(context.Message));
            }

            if (this.OnMessage == null)
            {
                return;
            }

            ThreadPool.QueueUserWorkItem(o =>
            {
                if (!this.running)
                {
                    this.Log.Info(string.Format("message dropped as client closed: {0}", this.Dump(context.Message)));
                    return;
                }

                Message msg = this.ParseMessage(context.Message);
                var args    = new MessageArgs(msg, m => this.Confirm(m.Id));
                var sw      = new Stopwatch();
                try
                {
                    sw.Start();
                    this.OnMessage(this, args);
                    sw.Stop();
                }
                catch (Exception e)
                {
                    args.Fail(e.Message);
                }

                if (args._isFail)
                {
                    this.Log.Info("process message error: {0}", args._reason);
                    this.Fail(msg.Id, args._reason.Length > 128 ? args._reason.Substring(0, 128) : args._reason);
                    return;
                }

                // prevent confirm attach
                if (sw.ElapsedMilliseconds <= 1)
                {
                    Thread.Sleep(10);
                }

                if (args._isConfirmed)
                {
                    return;
                }

                try
                {
                    this.Confirm(msg.Id);

                    if (enableTraceLog)
                    {
                        this.Log.Info("confirm message topic: {0}, dataid: {1}", msg.Topic, msg.Dataid);
                    }
                }
                catch (Exception e)
                {
                    this.Log.Warn(string.Format("confirm message {0} error {1}", this.Dump(context.Message), e.StackTrace));
                }
            });
        }