예제 #1
0
        // 线程核心执行函数
        private void QueueCometWaitRequest_WaitCallback()
        {
            while (true)
            {
                CometWaitRequest[] processRequest;

                lock (state)
                {
                    processRequest = WaitRequests.ToArray();
                }
                //if (processRequest.Length == 0)
                //    break;

                if (processRequest.Length == 0)
                {
                    Thread.Sleep(100);
                }
                else
                {
                    for (int i = 0; i < processRequest.Length; i++)
                    {
                        try
                        {
                            CometClient cometClient = StateManager.StateProvider.GetCometClient(processRequest[i].ClientPrivateToken);

                            if (processRequest[i].Active)
                            {
                                Thread.Sleep(100);

                                if (DateTime.Now.Subtract(processRequest[i].DateTimeAdded).TotalSeconds >= cometClient.ConnectionTimeoutSeconds)
                                {
                                    DeactivateCometWaitRequest(processRequest[i]);
                                    CometMessage timeoutMessage = new CometMessage()
                                    {
                                        MessageId = 0,
                                        Tip       = "aspNetComet.timeout",
                                        Content   = null
                                    };
                                    processRequest[i].Result.CometMessages = new CometMessage[] { timeoutMessage };
                                    this.QueueCometWaitRequest_Finished(processRequest[i]);
                                }
                                else
                                {
                                    // 这里是消息响应的根本,响应给客户端的是 消息数组
                                    CometMessage[] messages = CheckForServerPushMessages(processRequest[i]);

                                    if (messages != null && messages.Length > 0)
                                    {
                                        processRequest[i].Result.CometMessages = messages;
                                        DeactivateCometWaitRequest(processRequest[i]);
                                        QueueCometWaitRequest_Finished(processRequest[i]);
                                    }
                                }
                            }
                            else
                            {
                                // 长连接 连接超时,断网或者 离线
                                this.CheckForIdleCometWaitRequest(processRequest[i], cometClient);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (processRequest[i].Active)
                            {
                                //  ok, this one has screwed up, so
                                //  we need to dequeue the request from ASP.NET, basically disable it and return
                                //  dequeue the request
                                DeactivateCometWaitRequest(processRequest[i]);

                                //  get the message
                                CometMessage errorMessage = new CometMessage()
                                {
                                    MessageId = 0,
                                    Tip       = "aspNetComet.error",
                                    Content   = ex.Message
                                };

                                //
                                //  ok, we we timeout the message
                                processRequest[i].Result.CometMessages = new CometMessage[] { errorMessage };
                                //  call the message
                                QueueCometWaitRequest_Finished(processRequest[i]);
                            }
                            else
                            {
                                //  this is not an active request, so we dequeue it from the
                                //  thread
                                DequeueCometWaitRequest(processRequest[i].ClientPrivateToken);
                            }
                        }
                    }
                }
            }
        }