Esempio n. 1
0
        private void ProcessMessage(IWeChatMessage message, IOfficialAccountService service, Queue <IWeChatMessageHandler> queue, AsyncResult <IWeChatMessageResult> result, bool completedSynchronously)
        {
            if (queue.Count == 0)
            {
                result.MarkCompleted(null, completedSynchronously, null);
                return;
            }

            IWeChatMessageHandler handler = queue.Dequeue();

            handler.BeginProcessMessage(message, service, (ar) => {
                Exception error = null;
                IWeChatMessageResult messageResult = null;
                bool synchronously = completedSynchronously && ar.CompletedSynchronously;

                try {
                    messageResult = handler.EndProcessMessage(ar);
                } catch (Exception ex) {
                    error = ex;
                }

                if (error != null)
                {
                    result.MarkCompleted(error, synchronously, null);
                }
                else if (messageResult != null)
                {
                    result.MarkCompleted(null, synchronously, messageResult);
                }
                else
                {
                    this.ProcessMessage(message, service, queue, result, synchronously);
                }
            }, null);
        }
        public IAsyncResult BeginProcessMessage(byte[] data, AsyncCallback callback, object userState)
        {
            IWeChatMessage        message             = null;
            IWeChatMessageHandler handler             = null;
            string postContent                        = Encoding.UTF8.GetString(data);
            AsyncResult <IWeChatMessageResult> result = new AsyncResult <IWeChatMessageResult>(callback, userState);

            try {
                message = this.m_messageFactory.GetMessage(postContent, this);
            } catch (Exception ex) {
                result.MarkCompleted(ex, true, null);
                return(result);
            }

            if (message != null)
            {
                try {
                    handler = this.m_handlerFactory.GetHandler(message, this);
                } catch (Exception ex) {
                    result.MarkCompleted(ex, true, null);
                    return(result);
                }

                if (handler != null && handler.Priority != WeChatMessageHandlerPriority.Disabled)
                {
                    handler.BeginProcessMessage(message, this, (ar) => {
                        Exception error = null;
                        IWeChatMessageResult messageResult = null;
                        try {
                            messageResult = handler.EndProcessMessage(ar);
                        } catch (Exception ex) {
                            error = ex;
                        }
                        result.MarkCompleted(error, ar.CompletedSynchronously, messageResult ?? WeChatEmptyMessageResult.Instance);
                    }, null);
                }
                else
                {
                    result.MarkCompleted(null, true, WeChatEmptyMessageResult.Instance);
                }
            }
            else
            {
                result.MarkCompleted(null, true, WeChatEmptyMessageResult.Instance);
            }

            return(result);
        }