⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 room.cs

📁 定义社区节点
💻 CS
📖 第 1 页 / 共 2 页
字号:
            if(this.URL.Equals(string.Empty))
            {
                return ;
            }
            if (this.IsGroup)
            {
                return;
            }
            if (this.InRefresh)
            {
                return ;
            }
            int flags;
            if (!Win32API.InternetGetConnectedState(out flags, 0))
            {

                return ;
            }
            if (!this.ManuleRefresh && ((TimeSpan)(DateTime.Now - this.RefreshTime)).TotalMinutes < Setting.RefreshMinutes)
            {
                return;
            }

            
            this.RefreshTime = DateTime.Now;
            WEBFunc.Get(new RequestInfo(this.URL), new RequestCompleteCallback(this.WebRequestComplete), new ExceptionCallback(this.WebRequestException), new StartDownloadCallback(this.StartDownload), this.ManuleRefresh ? RequestStatePriority.ManualRefresh : RequestStatePriority.Refresh );

        }
        private static object _lockobject=new object();
        private bool  StartDownload()
        {
            lock (_lockobject)
            {
                if (this.InRefresh)
                {
                    return false ;
                }
                this.InRefresh = true;
            }
            return true;

        }

        private void WebRequestComplete(byte[] dataBuffer)
        {
            this.InRefresh = false;
            WorkerFunc.AddWorkerTask(new LoadHtmlDelegate(this.RefreshProc),WorkPriority.Refresh , new object[] {  dataBuffer });
        }

        private void WebRequestException(Exception e)
        {
            try
            {
               
            }
            finally
            {
                this.InRefresh = false;
            }
        }


        private void RefreshProc(byte[] dataBuffer)
        {
            try
            {
                string strHTML = Variant.encoding.GetString(dataBuffer);
                bool blnNoRead = false;


                Topic Topic_old1st = null;

                if (this.Topics != null && this.Topics.Count > 0 && this.Topics[0] != null)
                {
                    Topic_old1st = this.Topics[0].Clone();
                }

                this.Items = -1;
                this.UnreadItems = -1;

                int intOldItems = this.Items;
                int intOldUnreadItems = this.UnreadItems;

                if (Variant.CurrentRoom != null && Variant.CurrentRoom.URL == this.URL)
                    Variant.CurrentRoom.ManuleRefresh = true;



                if (strHTML.Length == 0) return;

                if (Variant.CurrentRoom != null && Variant.CurrentRoom.URL == this.URL)
                    Variant.CurrentRoom.ManuleRefresh = false;

                //使用正则表达式分析出帖子列表
                //格式为:
                //<a href="/Expert/TopicView1.asp?id=帖子ID" target="_blank">标题</a>
                //</td>
                //<td align="right">用户</td>
                //<td width="30" align="right">分数</td>
                //<td width="30" align="right">回复</td>
                //<td width="80" align="right">时间</td>
                //2007-08-22 格式改为:
                //<a href="/Expert/TopicView1.asp?id=帖子ID" target="_blank">标题</a>
                //</td>
                //<td align="right">
                //<a href="http://hi.csdn.net/用户" target="_blank">用户</a>
                //</td>
                //<td width="30" align="right">分数</td>
                //<td width="30" align="right">回复</td>
                //<td width="80" align="right">时间</td>
                //string strRegex = @"<a[^<]*TopicView1.asp[^<]*id=(?<ID>[^<]*)""[^<]*target[^<]*>(?<topic>[^<]*)</a>[^<]*</td>[^<]*<td[^>]*>[^<]*<a[^<]*hi.csdn.net[^<]*""[^<]*target[^<]*>(?<user>[^<]*)</a>[^<]*</td>[^<]*<td[^>]*>(?<Points>[^<]*)</td>[^<]*<td[^>]*>(?<Replies>[^<]*)</td>[^<]*<td[^>]*>(?<ReplayTime>[^<]*)</td>";//<a[^<]*TopicView1.asp[^<]*id=(?<ID>[^<]*)""[^<]*target[^<]*>(?<topic>[^<]*)</a>[^<]*</td>[^<]*<td[^>]*>(?<user>[^<]*)</td>[^<]*<td[^>]*>(?<Points>[^<]*)</td>[^<]*<td[^>]*>(?<Replies>[^<]*)</td>[^<]*<td[^>]*>(?<ReplayTime>[^<]*)</td>";

                //2007-10-01   csdn改版,样子全变啦 :(
                //string strRegex = @"<a[^<]*topic.csdn.net(?<ID>[^<]*)""[^<]*target[^<]*>(?<topic>[^<]*)</a>((?!</td).)*</td>[^<]*<td[^>]*>(?<Points>[^<]*)</td>[^<]*<td[^>]*>[^<]*<a[^<]*hi.csdn.net[^<]*""[^<]*>(?<user>[^<]*)</a>[^<]*<br[^<]*/>(?<PostTime>[^<]*)</td>[^<]*<td[^>]*>(?<Replies>[^<]*)</td>[^<]*<td[^>]*>[^<]*<a[^<]*hi.csdn.net[^<]*""[^<]*>(?<replayuser>[^<]*)</a>[^<]*<br[^<]*/>(?<ReplayTime>[^<]*)</td>[^>]*<td>[^>]*<a[^>]*TopicManageView.aspx[^>]*forumID=(?<forumID>[^<]*)&topicID=(?<topicID>[^<]*)&date=(?<topicdate>[^""]*)""";
                //2009-01-16    新样式
                string strRegex ="";
                strRegex += @"<a[^<]*title=""[^<]*""[^<]*href=""[^<]*topic.csdn.net(?<ID>[^<]*)""[^<]*>(?<topic>[^<]*)</a>";
                strRegex += @"((?!</td).)*</td>[^<]*<td>(?<Points>[^<]*)</td>";
                strRegex += @"[^<]*<td>[^<]*<a[^<]*href=""http://hi.csdn.net/[^<]*""[^<]*>(?<user>[^<]*)</a>";
                strRegex += @"[^<]*<br[^<]*/>(?<PostTime>[^<]*)</td>";
                strRegex += @"[^<]*<td[^>]*>(?<Replies>[^<]*)</td>";
                strRegex += @"[^<]*<td[^>]*>[^<]*<a[^<]*hi.csdn.net[^<]*""[^<]*>(?<replayuser>[^<]*)</a>[^<]*<br[^<]*/>(?<ReplayTime>[^<]*)</td>";
                strRegex += @"[^>]*<td>[^>]*<a[^>]*TopicManageView.aspx[^>]*forumID=(?<forumID>[^<]*)&topicID=(?<topicID>[^<]*)&date=(?<topicdate>[^""]*)""";

                Regex r;
                MatchCollection m;
                r = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                m = r.Matches(strHTML);

                
                if (this.Name.IndexOf("C#") >= 0)
                {
                    string strdebug;
                    strdebug = strHTML;
                    
                    
                }

                if (m.Count <= 0) return;
                             

                List<Topic> OldTopicsClone = new List<Topic>(this.Topics);

                List<Topic> NewTopics = new List<Topic>(m.Count + this.Topics.Count);
                for (int i = 0; i < m.Count; i++)
                {
                    //将帖子存入Topic对象
                    Topic t = new Topic(this.ID, m[i].Groups["ID"].Value, m[i].Groups["topic"].Value, m[i].Groups["user"].Value, m[i].Groups["Points"].Value, m[i].Groups["Replies"].Value, m[i].Groups["replayuser"].Value, m[i].Groups["ReplayTime"].Value, m[i].Groups["forumID"].Value, m[i].Groups["topicID"].Value, m[i].Groups["topicdate"].Value);
                    

                    Topic topicOld = t.Contains(OldTopicsClone);
                    if (topicOld != null)
                    {
                        //没有新回复,即从这帖开始后面都是旧帖
                        if (t.ReplyUser == topicOld.ReplyUser && t.Replies == topicOld.Replies && t.ReplyTime.CompareTo(topicOld.ReplyTime) <= 0)
                        {
                            break;
                        }
                        


                        OldTopicsClone.Remove(topicOld);
                                                
                        if (topicOld.UpdateTopic(t))
                        {
                            t = topicOld;
                        }

                        
                    }
                    else
                    {

                    }

                    t.OnOpenedTopic(t.Id, false);
                    //过滤帖子
                    if (t.Filter())
                    {                        
                        NewTopics.Add(t);
                    }
                    else
                    {

                    }
                }

                //旧帖还是显示
                for (int intTopic = 0; intTopic < OldTopicsClone.Count; intTopic++)
                {                    
                    NewTopics.Add(OldTopicsClone[intTopic]);
                }
                
                Refresh( blnNoRead, Topic_old1st, intOldItems, intOldUnreadItems, NewTopics );
            }
            finally
            {
                this.InRefresh = false;
            }

        }
        #endregion Refrash Room
        
        private void Refresh(  bool blnNoRead, Topic Topic_old1st, int intOldItems, int intOldUnreadItems, List<Topic> NewTopics)
        {
            bool blnCountChanged = false;
            Topic Topic_new1st = null;

            this.Topics.Clear();
            this.Topics.AddRange(NewTopics);
            this.Items = -1;
            this.UnreadItems = -1;

            if (intOldItems != this.Items || intOldUnreadItems != this.UnreadItems)
            {
                blnCountChanged = true;
            }
            if (this.Topics != null && this.Topics.Count > 0 && this.Topics[0] != null)
            {
                Topic_new1st = this.Topics[0];
            }
            if (Topic_new1st != null)
            {
                if (Topic_old1st == null)
                {
                    blnNoRead = true;
                }
                else if (!Topic_new1st.HaveRead)
                {
                    if (Topic_old1st.Replies != Topic_new1st.Replies || Topic_old1st.Id != Topic_new1st.Id || Topic_old1st.ReplyTime != Topic_new1st.ReplyTime || Topic_old1st.HaveRead != Topic_new1st.HaveRead)
                    {
                        blnNoRead = true;
                    }
                }
            }

            this.ManuleRefresh = false;


            DelegateFunc.OnRefreshTopics(this.ID, blnNoRead, blnCountChanged);
        }

        public Room Clone()
        {
            Model.Room r = new Model.Room();

            //r.ChildRooms.AddRange(this.ChildRooms);
            r.GroupID = this.GroupID;
            r.ID = this.ID;
            r.IsGroup = this.IsGroup;
            r.Items = this.Items;
            r.ManuleRefresh = this.ManuleRefresh;
            r.Name = this.Name;
            r.RefreshTime = this.RefreshTime;
             
            r.Topics.AddRange(this.Topics);
            r.UnreadItems = this.UnreadItems;
            r.URL = this.URL;
            return r;
        }

        private void CheckTopicExist(Topic topic)
        {
            if (topic == null || topic.RoomId!=this.ID) 
            {
                return;
            }
            bool blnExist = false;
            foreach (Topic t in this.Topics)
            {
                if (t.Id == topic.Id && topic.RoomId == this.ID)
                {
                    blnExist = true;
                    break;
                }
            }
            if (!blnExist)
            {
                this.Topics.Add(topic);
            }
        }

        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -