/// <summary> /// 把一个用户加入新的版块。警告:本方法不是线程安全的,需自行加锁 /// </summary> /// <param name="forumID"></param> /// <param name="onlineGuest"></param> private void AddForumOnlineGuest(int forumID, OnlineGuest onlineGuest) { bool newForum = false; OnlineGuestCollection onlineGuestsInForum; //新板块还没有建立列表,那么立即创建一个 if (m_ForumOnlineGuests.TryGetValue(forumID, out onlineGuestsInForum) == false) { lock (m_ForumOnlineGuests_AllLocker) { if (m_ForumOnlineGuests.TryGetValue(forumID, out onlineGuestsInForum) == false) { onlineGuestsInForum = new OnlineGuestCollection(); onlineGuestsInForum.Add(onlineGuest); m_ForumOnlineGuests.Add(forumID, onlineGuestsInForum); newForum = true; } } } //就算是更新原来的值,也不对原列表进行改动,而是复制一份新列表出来,并增加新项,以免发生“集合已更改”的问题 if (newForum == false) { OnlineGuestCollection newOnlineGuestsInForum = new OnlineGuestCollection(onlineGuestsInForum); newOnlineGuestsInForum.Add(onlineGuest); m_ForumOnlineGuests[forumID] = newOnlineGuestsInForum; } }
public void Update(AuthUser my, RequestVariable request, OnlineAction action, int forumID, int threadID, string subject) { subject = StringUtil.CutString(subject, 20); int userID = my.UserID; string ip = request.IpAddress; bool addnew = false; //已经登录的用户 if (userID > 0) { #region 已经登录的用户的处理 OnlineMember onlineMember; if (m_OnlineMemberTable.TryGetValue(userID, out onlineMember) == false) { string location = IPUtil.GetIpArea(ip); RoleInOnline role = GetUserRoleInOnline(userID); lock (m_OnlineMembers_Locker) { if (m_OnlineMemberTable.TryGetValue(userID, out onlineMember) == false) { #region 增加一个OnlineMember DateTime now = DateTimeUtil.Now; onlineMember = new OnlineMember(); onlineMember.UserID = userID; onlineMember.Action = action; onlineMember.Username = my.Username; onlineMember.IsInvisible = my.IsInvisible; onlineMember.IP = ip; onlineMember.Location = location; onlineMember.ForumID = forumID; onlineMember.ThreadID = threadID; onlineMember.ThreadSubject = subject; onlineMember.Platform = request.Platform; onlineMember.Browser = request.Browser; onlineMember.CreateDate = now; onlineMember.UpdateDate = now; onlineMember.RoleSortOrder = role.SortOrder; onlineMember.RoleIdentityIDString = my.RoleIdentityIDString; m_OnlineMemberTable.Add(userID, onlineMember); OnlineMemberCollection newOnlineMembers = new OnlineMemberCollection(m_OnlineMembers); newOnlineMembers.Add(onlineMember); m_OnlineMembers = newOnlineMembers; if (forumID != 0) { lock (GetForumOnlineMemberLocker(forumID)) { //加入新的版块 AddForumOnlineMember(forumID, onlineMember); } } #endregion addnew = true; } } } if (addnew == false) { lock (onlineMember) { //ForumID为-1意味着其实已经删除,无需再更新 if (onlineMember.ForumID != -1) { #region 更新原来OnlineMember的值 onlineMember.Action = action; onlineMember.Username = my.Username; onlineMember.IsInvisible = my.IsInvisible; if (my.RoleIdentityIDString != onlineMember.RoleIdentityIDString) { RoleInOnline role = GetUserRoleInOnline(userID); onlineMember.RoleSortOrder = role.SortOrder; onlineMember.RoleIdentityIDString = my.RoleIdentityIDString; } if (onlineMember.IP != ip) { onlineMember.IP = ip; onlineMember.Location = IPUtil.GetIpArea(ip); } //----------------- int oldForumID = onlineMember.ForumID; onlineMember.ThreadID = threadID; onlineMember.ThreadSubject = subject; if (oldForumID != forumID) { if (oldForumID != 0 && forumID != 0) { lock (GetForumOnlineMemberLocker(oldForumID)) { lock (GetForumOnlineMemberLocker(forumID)) { //从原来的版块移除 RemoveForumOnlineMember(oldForumID, onlineMember); //加入新的版块 AddForumOnlineMember(forumID, onlineMember); } } } else if (oldForumID != 0) { lock (GetForumOnlineMemberLocker(oldForumID)) { //从原来的版块移除 RemoveForumOnlineMember(oldForumID, onlineMember); } } else if (forumID != 0) { lock (GetForumOnlineMemberLocker(forumID)) { //加入新的版块 AddForumOnlineMember(forumID, onlineMember); } } onlineMember.ForumID = forumID; } onlineMember.Platform = request.Platform; onlineMember.Browser = request.Browser; onlineMember.UpdateDate = DateTimeUtil.Now; #endregion } else LogHelper.CreateDebugLog("OnlineMember线程同步监视1"); } } #endregion //如果是已登陆状态,但客户端仍然提交了GuestID,则把这个GuestID移除 //if (my.MachineIDIsNew == false) string guestID = my.GuestID; if (guestID != null) { RemoveOnlineGuest(guestID); } } else //尚未登录的用户 { //TODO : 暂不统计游客在线情况,因为可能存在溢出,下个版本改进 #region 尚未登录的用户的处理 bool isSpider = request.IsSpider; string guestID = my.BuildGuestID(); OnlineGuest onlineGuest; if (m_OnlineGuestTable.TryGetValue(guestID, out onlineGuest) == false) { string location = IPUtil.GetIpArea(ip); lock (m_OnlineGuests_Locker) { //假如游客列表中还没有这个游客 if (m_OnlineGuestTable.TryGetValue(guestID, out onlineGuest) == false) { bool success = UpdateOnlineGuestIP(guestID, null, ip, isSpider);// AddGuestToIP(guestID, ip, request.IsSpider); //bool success = true; if (success) { #region 增加一个OnlineGuest DateTime now = DateTimeUtil.Now; onlineGuest = new OnlineGuest(); onlineGuest.GuestID = my.GuestID; onlineGuest.IsSpider = isSpider; onlineGuest.Action = action; onlineGuest.IP = ip; onlineGuest.Location = location; onlineGuest.Platform = request.Platform; onlineGuest.Browser = request.Browser; onlineGuest.CreateDate = now; onlineGuest.UpdateDate = now; onlineGuest.ForumID = forumID; onlineGuest.ThreadID = threadID; onlineGuest.ThreadSubject = subject; m_OnlineGuestTable.Add(guestID, onlineGuest); OnlineGuestCollection newOnlineGuests = new OnlineGuestCollection(m_OnlineGuests); newOnlineGuests.Add(onlineGuest); m_OnlineGuests = newOnlineGuests; if (forumID != 0) { lock (GetForumOnlineGuestLocker(forumID)) { //加入新的版块 AddForumOnlineGuest(forumID, onlineGuest); } } #endregion addnew = true; } else return; } } } //如果只是更新原来的值,那么开始更新 if (addnew == false) { lock (onlineGuest) { //ForumID为-1表示其实这个对象已经从在线列表中移除了 if (onlineGuest.ForumID != -1) { #region 更新原来OnlineGuest的值 onlineGuest.Action = action; //如果这个游客仅仅发生了IP变化,那么要更新IP表 if (onlineGuest.IP != ip) { UpdateOnlineGuestIP(guestID, onlineGuest.IP, ip, isSpider); //RemoveGuestFromIP(guestID, onlineGuest.IP); onlineGuest.IsSpider = isSpider; onlineGuest.IP = ip; onlineGuest.Location = IPUtil.GetIpArea(ip); //AddGuestToIP(guestID, onlineGuest.IP, request.IsSpider); } //---------------------- int oldForumID = onlineGuest.ForumID; if (oldForumID != forumID) { if (oldForumID != 0 && forumID != 0) { lock (GetForumOnlineGuestLocker(oldForumID)) { lock (GetForumOnlineGuestLocker(forumID)) { //从原来的版块移除 RemoveForumOnlineGuest(oldForumID, onlineGuest); //加入新的版块 AddForumOnlineGuest(forumID, onlineGuest); } } } else if (oldForumID != 0) { lock (GetForumOnlineMemberLocker(oldForumID)) { //从原来的版块移除 RemoveForumOnlineGuest(oldForumID, onlineGuest); } } else if (forumID != 0) { lock (GetForumOnlineMemberLocker(forumID)) { //加入新的版块 AddForumOnlineGuest(forumID, onlineGuest); } } onlineGuest.ForumID = forumID; } onlineGuest.Platform = request.Platform; onlineGuest.Browser = request.Browser; onlineGuest.UpdateDate = DateTimeUtil.Now; onlineGuest.ThreadID = threadID; onlineGuest.ThreadSubject = subject; #endregion } else LogHelper.CreateDebugLog("OnlineGuest线程同步监视1"); } } #endregion } }
/// <summary> /// 把一个用户加入新的版块。警告:本方法不是线程安全的,需自行加锁 /// </summary> /// <param name="forumID"></param> /// <param name="onlineGuest"></param> private void AddForumOnlineGuest(int forumID, OnlineGuest onlineGuest) { bool newForum = false; OnlineGuestCollection onlineGuestsInForum; //新板块还没有建立列表,那么立即创建一个 if (m_ForumOnlineGuests.TryGetValue(forumID, out onlineGuestsInForum) == false) { lock (m_ForumOnlineGuests_AllLocker) { if (m_ForumOnlineGuests.TryGetValue(forumID, out onlineGuestsInForum) == false) { onlineGuestsInForum = new OnlineGuestCollection(); onlineGuestsInForum.Add(onlineGuest); m_ForumOnlineGuests.Add(forumID, onlineGuestsInForum); newForum = true; } } } //就算是更新原来的值,也不对原列表进行改动,而是复制一份新列表出来,并增加新项,以免发生“集合已更改”的问题 if (newForum == false) { OnlineGuestCollection newOnlineGuestsInForum = new OnlineGuestCollection(onlineGuestsInForum); newOnlineGuestsInForum.Add(onlineGuest); m_ForumOnlineGuests[forumID] = newOnlineGuestsInForum; } }
public void Update(AuthUser my, RequestVariable request, OnlineAction action, int forumID, int threadID, string subject) { subject = StringUtil.CutString(subject, 20); int userID = my.UserID; string ip = request.IpAddress; bool addnew = false; //已经登录的用户 if (userID > 0) { #region 已经登录的用户的处理 OnlineMember onlineMember; if (m_OnlineMemberTable.TryGetValue(userID, out onlineMember) == false) { string location = IPUtil.GetIpArea(ip); RoleInOnline role = GetUserRoleInOnline(userID); lock (m_OnlineMembers_Locker) { if (m_OnlineMemberTable.TryGetValue(userID, out onlineMember) == false) { #region 增加一个OnlineMember DateTime now = DateTimeUtil.Now; onlineMember = new OnlineMember(); onlineMember.UserID = userID; onlineMember.Action = action; onlineMember.Username = my.Username; onlineMember.IsInvisible = my.IsInvisible; onlineMember.IP = ip; onlineMember.Location = location; onlineMember.ForumID = forumID; onlineMember.ThreadID = threadID; onlineMember.ThreadSubject = subject; onlineMember.Platform = request.Platform; onlineMember.Browser = request.Browser; onlineMember.CreateDate = now; onlineMember.UpdateDate = now; onlineMember.RoleSortOrder = role.SortOrder; onlineMember.RoleIdentityIDString = my.RoleIdentityIDString; m_OnlineMemberTable.Add(userID, onlineMember); OnlineMemberCollection newOnlineMembers = new OnlineMemberCollection(m_OnlineMembers); newOnlineMembers.Add(onlineMember); m_OnlineMembers = newOnlineMembers; if (forumID != 0) { lock (GetForumOnlineMemberLocker(forumID)) { //加入新的版块 AddForumOnlineMember(forumID, onlineMember); } } #endregion addnew = true; } } } if (addnew == false) { lock (onlineMember) { //ForumID为-1意味着其实已经删除,无需再更新 if (onlineMember.ForumID != -1) { #region 更新原来OnlineMember的值 onlineMember.Action = action; onlineMember.Username = my.Username; onlineMember.IsInvisible = my.IsInvisible; if (my.RoleIdentityIDString != onlineMember.RoleIdentityIDString) { RoleInOnline role = GetUserRoleInOnline(userID); onlineMember.RoleSortOrder = role.SortOrder; onlineMember.RoleIdentityIDString = my.RoleIdentityIDString; } if (onlineMember.IP != ip) { onlineMember.IP = ip; onlineMember.Location = IPUtil.GetIpArea(ip); } //----------------- int oldForumID = onlineMember.ForumID; onlineMember.ThreadID = threadID; onlineMember.ThreadSubject = subject; if (oldForumID != forumID) { if (oldForumID != 0 && forumID != 0) { lock (GetForumOnlineMemberLocker(oldForumID)) { lock (GetForumOnlineMemberLocker(forumID)) { //从原来的版块移除 RemoveForumOnlineMember(oldForumID, onlineMember); //加入新的版块 AddForumOnlineMember(forumID, onlineMember); } } } else if (oldForumID != 0) { lock (GetForumOnlineMemberLocker(oldForumID)) { //从原来的版块移除 RemoveForumOnlineMember(oldForumID, onlineMember); } } else if (forumID != 0) { lock (GetForumOnlineMemberLocker(forumID)) { //加入新的版块 AddForumOnlineMember(forumID, onlineMember); } } onlineMember.ForumID = forumID; } onlineMember.Platform = request.Platform; onlineMember.Browser = request.Browser; onlineMember.UpdateDate = DateTimeUtil.Now; #endregion } else { LogHelper.CreateDebugLog("OnlineMember线程同步监视1"); } } } #endregion //如果是已登陆状态,但客户端仍然提交了GuestID,则把这个GuestID移除 //if (my.MachineIDIsNew == false) string guestID = my.GuestID; if (guestID != null) { RemoveOnlineGuest(guestID); } } else //尚未登录的用户 { //TODO : 暂不统计游客在线情况,因为可能存在溢出,下个版本改进 #region 尚未登录的用户的处理 bool isSpider = request.IsSpider; string guestID = my.BuildGuestID(); OnlineGuest onlineGuest; if (m_OnlineGuestTable.TryGetValue(guestID, out onlineGuest) == false) { string location = IPUtil.GetIpArea(ip); lock (m_OnlineGuests_Locker) { //假如游客列表中还没有这个游客 if (m_OnlineGuestTable.TryGetValue(guestID, out onlineGuest) == false) { bool success = UpdateOnlineGuestIP(guestID, null, ip, isSpider);// AddGuestToIP(guestID, ip, request.IsSpider); //bool success = true; if (success) { #region 增加一个OnlineGuest DateTime now = DateTimeUtil.Now; onlineGuest = new OnlineGuest(); onlineGuest.GuestID = my.GuestID; onlineGuest.IsSpider = isSpider; onlineGuest.Action = action; onlineGuest.IP = ip; onlineGuest.Location = location; onlineGuest.Platform = request.Platform; onlineGuest.Browser = request.Browser; onlineGuest.CreateDate = now; onlineGuest.UpdateDate = now; onlineGuest.ForumID = forumID; onlineGuest.ThreadID = threadID; onlineGuest.ThreadSubject = subject; m_OnlineGuestTable.Add(guestID, onlineGuest); OnlineGuestCollection newOnlineGuests = new OnlineGuestCollection(m_OnlineGuests); newOnlineGuests.Add(onlineGuest); m_OnlineGuests = newOnlineGuests; if (forumID != 0) { lock (GetForumOnlineGuestLocker(forumID)) { //加入新的版块 AddForumOnlineGuest(forumID, onlineGuest); } } #endregion addnew = true; } else { return; } } } } //如果只是更新原来的值,那么开始更新 if (addnew == false) { lock (onlineGuest) { //ForumID为-1表示其实这个对象已经从在线列表中移除了 if (onlineGuest.ForumID != -1) { #region 更新原来OnlineGuest的值 onlineGuest.Action = action; //如果这个游客仅仅发生了IP变化,那么要更新IP表 if (onlineGuest.IP != ip) { UpdateOnlineGuestIP(guestID, onlineGuest.IP, ip, isSpider); //RemoveGuestFromIP(guestID, onlineGuest.IP); onlineGuest.IsSpider = isSpider; onlineGuest.IP = ip; onlineGuest.Location = IPUtil.GetIpArea(ip); //AddGuestToIP(guestID, onlineGuest.IP, request.IsSpider); } //---------------------- int oldForumID = onlineGuest.ForumID; if (oldForumID != forumID) { if (oldForumID != 0 && forumID != 0) { lock (GetForumOnlineGuestLocker(oldForumID)) { lock (GetForumOnlineGuestLocker(forumID)) { //从原来的版块移除 RemoveForumOnlineGuest(oldForumID, onlineGuest); //加入新的版块 AddForumOnlineGuest(forumID, onlineGuest); } } } else if (oldForumID != 0) { lock (GetForumOnlineMemberLocker(oldForumID)) { //从原来的版块移除 RemoveForumOnlineGuest(oldForumID, onlineGuest); } } else if (forumID != 0) { lock (GetForumOnlineMemberLocker(forumID)) { //加入新的版块 AddForumOnlineGuest(forumID, onlineGuest); } } onlineGuest.ForumID = forumID; } onlineGuest.Platform = request.Platform; onlineGuest.Browser = request.Browser; onlineGuest.UpdateDate = DateTimeUtil.Now; onlineGuest.ThreadID = threadID; onlineGuest.ThreadSubject = subject; #endregion } else { LogHelper.CreateDebugLog("OnlineGuest线程同步监视1"); } } } #endregion } }