/// <summary> /// 更新本地数据库组织机构版本信息 /// </summary> public static void UpdateOrgVersion(IMLibrary3.Protocol.OrgVersion orgVersion) { try { if (orgVersion.RoomsVersion == null) { orgVersion.RoomsVersion = ""; } if (orgVersion.GroupsVersion == null) { orgVersion.GroupsVersion = ""; } if (orgVersion.UsersVersion == null) { orgVersion.UsersVersion = ""; } string sql = "update OrgVersion set Password='',GroupsVersion=@GroupsVersion,UsersVersion=@UsersVersion,RoomsVersion=@RoomsVersion"; System.Data.SQLite.SQLiteParameter[] parameters = new System.Data.SQLite.SQLiteParameter[] { new System.Data.SQLite.SQLiteParameter("@GroupsVersion", orgVersion.GroupsVersion), new System.Data.SQLite.SQLiteParameter("@UsersVersion", orgVersion.UsersVersion), new System.Data.SQLite.SQLiteParameter("@RoomsVersion", orgVersion.RoomsVersion), }; SQLiteDBHelper.ExecuteNonQuery(sql, parameters); } catch (Exception ex) { MessageBox.Show(ex.Source + ex.Message); } }
void msgServer_RequestChangeGroupVcard(object sender, MsgServerEventArgs e) { GroupVcard card = e.obj as GroupVcard; if (card.GroupID.Trim() == "" || card.GroupName.Trim() == "") { return; } card.GroupID = card.GroupID.Trim(); if (card.type == type.New && e.RequestUser.isAdmin) //如果用户是管理员 { if (DBHelper.CreateGroupVcard(card) > 0) //保存于数据库 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(e.XMLMsg); //将创建的分组信息广播给所有在线用户 } } else if (card.type == type.set && e.RequestUser.isAdmin) //如果是更新分组信息 { if (DBHelper.UpdateGroupVcard(card) > 0) //更新数据库 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 SendMessageToUser(e.RequestUser, e.XMLMsg); //通知管理员创建新用户成功 } } else if (card.type == type.get)//如果是更新 { card.type = type.result; SendMessageToUser(e.RequestUser, Factory.CreateXMLMsg(card)); //将分组信息资料发送给请求者 } else if (card.type == type.delete && e.RequestUser.isAdmin) //如果是删除分组 { if (DBHelper.DelGroup(card.GroupID) > 0) //数据库中删除 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(e.XMLMsg); //将删除的用户广播给所有在线用户 } } }
/// <summary> /// 管理员变更分组信息 /// </summary> /// <param name="card"></param> /// <param name="xmlMsg"></param> /// <param name="session"></param> private void onGroupVcard(GroupVcard card, string xmlMsg, TCPServerSession session) { if (card.GroupID.Trim() == "" || card.GroupName.Trim() == "") { return; } card.GroupID = card.GroupID.Trim(); UserVcard requestUser = session.Tag as UserVcard; //请求用户 if (card.type == type.New && requestUser.isAdmin) //如果用户是管理员 { if (DBHelper.CreateGroupVcard(card) > 0) //保存于数据库 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(xmlMsg); //将创建的分组信息广播给所有在线用户 } } else if (card.type == type.set && requestUser.isAdmin) //如果是更新分组信息 { if (DBHelper.UpdateGroupVcard(card) > 0) //更新数据库 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 SendMessageToUser(requestUser, xmlMsg); //通知管理员创建新用户成功 } } else if (card.type == type.get)//如果是更新 { card.type = type.result; SendMessageToUser(requestUser, Factory.CreateXMLMsg(card)); //将分组信息资料发送给请求者 } else if (card.type == type.delete && requestUser.isAdmin) //如果是删除分组 { if (DBHelper.DelGroup(card.GroupID) > 0) //数据库中删除 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(xmlMsg); //将删除的用户广播给所有在线用户 } } }
/// <summary> /// 获得组织机构版本 /// </summary> /// <param name="UserID"></param> /// <returns></returns> public static IMLibrary3.Protocol.OrgVersion GetOrgVersion(string UserID) { IMLibrary3.Protocol.OrgVersion orgVersion = null; #region 判断登录的用户本地数据库文件夹及文件是否存在,不存在返回空值 //System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(UserID); //if (!dInfo.Exists) // return null; //string FileNamePath = UserID + @"\Record.mdb"; //if (!System.IO.File.Exists(FileNamePath)) // return null; ////如果本地数据库存在,则设置数据库连接字符串 //DBFileNamePath = FileNamePath; //conStr = "data source=" + DBFileNamePath; #endregion try { string sql = "select * from OrgVersion"; System.Data.SQLite.SQLiteDataReader dr = SQLiteDBHelper.ExecuteReader(sql, null); if (dr != null && dr.Read()) { orgVersion = new IMLibrary3.Protocol.OrgVersion(); orgVersion.GroupsVersion = Convert.ToString(dr["GroupsVersion"]).Trim(); orgVersion.UsersVersion = Convert.ToString(dr["UsersVersion"]).Trim(); orgVersion.RoomsVersion = Convert.ToString(dr["RoomsVersion"]).Trim(); } dr.Close(); dr.Dispose(); return(orgVersion); } catch (Exception ex) { MessageBox.Show(ex.Source + ex.Message); } return(null); }
/// <summary> /// 开始服务 /// </summary> /// <param name="P2PFileServerPort">P2P服务传输服务端口(UDP)</param> /// <param name="ImageFileServerPort">图片文件传输服务端口(TCP)</param> /// <param name="MessageServerPort">消息服务端口(TCP)</param> /// <param name="P2PAVServerPort">音视频服务端口(UDP)</param> public void Start(int P2PFileServerPort, int P2PAVServerPort, int ImageFileServerPort, int MessageServerPort) { #region 初始化组织架构 ///初始化数据库连接字符串 IMLibrary.SqlData.DataAccess.ConnectionString = DBHelper.settings.SqlConStr; /// 初始化用户 if (Users == null) Users = DBHelper.GetUserVcards(); /// 获取组织机构版本和信息 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 } #region 初始化群 if (Rooms == null) { Rooms = DBHelper.GetRooms(); foreach (Room room in Rooms.Values) { #region 将内存中的用户添加到内存中的群 string[] userids = room.UserIDs.Split(';'); foreach (string userID in userids) { UserVcard user = getUser(userID); if (user != null) { if (room.Users == null) room.Users = new Dictionary<string, User>(); if (!room.Users.ContainsKey(userID)) { if (room.CreateUserID == userID) user.CreatedRoomsCount++;//标记用户已创建群数 room.Users.Add(user.UserID, user); } if (user.Rooms == null) user.Rooms = new Dictionary<string, Room>(); if (!user.Rooms.ContainsKey(room.RoomID)) user.Rooms.Add(room.RoomID, room); } } room.UserIDs = "";//重新生成群包含的Users foreach (User u in GetUsers(room)) room.UserIDs += u.UserID + ";"; #endregion } } #endregion #endregion //启动消息服务 if (msgServer == null) { msgServer = new MsgServer(MessageServerPort); msgServer.Login += new MsgServer.MsgServerEventHandler(msgServer_Login); msgServer.UserOffline += new MsgServer.MsgServerEventHandler(msgServer_UserOffline); msgServer.Message += new MsgServer.MsgServerEventHandler(msgServer_Message); msgServer.Presence += new MsgServer.MsgServerEventHandler(msgServer_Presence); msgServer.RequestChangeGroupVcard += new MsgServer.MsgServerEventHandler(msgServer_RequestChangeGroupVcard); msgServer.RequestChangePassword += new MsgServer.MsgServerEventHandler(msgServer_RequestChangePassword); msgServer.RequestChangeRoom += new MsgServer.MsgServerEventHandler(msgServer_RequestChangeRoom); msgServer.RequestChangeUserVcard += new MsgServer.MsgServerEventHandler(msgServer_RequestChangeUserVcard); msgServer.RequestOrgGroups += new MsgServer.MsgServerEventHandler(msgServer_RequestOrgGroups); msgServer.RequestOrgRooms += new MsgServer.MsgServerEventHandler(msgServer_RequestOrgRooms); msgServer.RequestOrgUsers += new MsgServer.MsgServerEventHandler(msgServer_RequestOrgUsers); msgServer.RequestUsersPresence += new MsgServer.MsgServerEventHandler(msgServer_RequestUsersPresence); msgServer.RouteDownLoadImageFile += new MsgServer.MsgServerEventHandler(msgServer_RouteDownLoadImageFile); msgServer.RouteP2PFileTransmit += new MsgServer.MsgServerEventHandler(msgServer_RouteP2PFileTransmit); msgServer.RouteAVMsg += new MsgServer.MsgServerEventHandler(msgServer_RouteAVMsg); } msgServer.Start(); //启动P2P文件服务 if (p2pFileServer == null) p2pFileServer = new P2PFileServer(P2PFileServerPort); p2pFileServer.Start(); //启动图片文件服务 if (imageFileServer == null) imageFileServer = new ImageFileServer(ImageFileServerPort); imageFileServer.Start(); //启动音视频传输服务 if (p2pAVServer == null) p2pAVServer = new P2PAVServer(P2PAVServerPort); p2pAVServer.Start(); }
void msgServer_RequestChangeGroupVcard(object sender, MsgServerEventArgs e) { GroupVcard card = e.obj as GroupVcard; if (card.GroupID.Trim() == "" || card.GroupName.Trim() == "") return; card.GroupID = card.GroupID.Trim(); if (card.type == type.New && e.RequestUser.isAdmin)//如果用户是管理员 { if (DBHelper.CreateGroupVcard(card) > 0)//保存于数据库 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(e.XMLMsg);//将创建的分组信息广播给所有在线用户 } } else if (card.type == type.set && e.RequestUser.isAdmin)//如果是更新分组信息 { if (DBHelper.UpdateGroupVcard(card) > 0) //更新数据库 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 SendMessageToUser(e.RequestUser, e.XMLMsg);//通知管理员创建新用户成功 } } else if (card.type == type.get)//如果是更新 { card.type = type.result; SendMessageToUser(e.RequestUser, Factory.CreateXMLMsg(card));//将分组信息资料发送给请求者 } else if (card.type == type.delete && e.RequestUser.isAdmin)//如果是删除分组 { if (DBHelper.DelGroup(card.GroupID) > 0) //数据库中删除 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(e.XMLMsg);//将删除的用户广播给所有在线用户 } } }
void msgServer_RequestChangeUserVcard(object sender, MsgServerEventArgs e) { UserVcard card = e.obj as UserVcard; if (card.UserID.Trim() == "" || card.UserName.Trim() == "" || card.GroupID.Trim() == "") return; card.UserID = card.UserID.Trim(); UserVcard user = getUser(card.UserID); if (card.type == type.New && user == null && e.RequestUser.isAdmin)//如果用户不存在,创建新用户 { Users.Add(card.UserID, card); //设置新用户密码为123456; card.Password = IMLibrary3.Security.Hasher.GetMD5Hash(IMLibrary3.Operation.TextEncoder.textToBytes("123456")); DBHelper.CreateUserVcard(card);//保存于数据库 orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(e.XMLMsg);//将创建的新用户信息广播给所有在线用户 } else if (card.type == type.set && e.RequestUser.isAdmin)//如果是更新用户 { if (user == null) return;//如果用户不存在则退出 string oldInfo = user.UserName + user.GroupID + user.OrderID;//老信息 string newInfo = card.UserName.Trim() + card.GroupID.Trim() + card.OrderID; //新信息 user.UserName = card.UserName.Trim(); user.GroupID = card.GroupID.Trim(); user.OrderID = card.OrderID; user.Job = card.Job.Trim(); user.OfficePhone = card.OfficePhone.Trim(); user.Mail = card.Mail.Trim(); user.Phone = card.Phone.Trim(); user.Post = card.Post.Trim(); user.Remark = card.Remark.Trim(); user.Birthday = card.Birthday.Trim(); user.Sex = card.Sex; user.CreateRooms = card.CreateRooms; user.Disable = card.Disable; user.isAdmin = card.isAdmin; user.isSendNotice = card.isSendNotice; user.isSendSMS = card.isSendSMS; DBHelper.UpdateUserVcard(user);//更新数据库 if (oldInfo != newInfo)//如果用户基本信息发生变更,则变更组织架构 orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 SendMessageToUser(e.RequestUser, e.XMLMsg);//通知管理员创建新用户成功 } else if (card.type == type.get)//如果是获取用户资料 { if (user == null) return;//如果用户不存在则退出 user.type = type.result; SendMessageToUser(e.RequestUser, Factory.CreateXMLMsg(user));//将用户资料发送给请求者 } else if (card.type == type.delete && e.RequestUser.isAdmin)//如果是删除用户 { if (user == null) return;//如果用户不存在则退出 if (e.RequestUser.UserID == user.UserID) return;//如果管理员想删除自己,退出 TCPServerSession se = user.Tag as TCPServerSession; if (se != null && se.IsConnected) { se.Disconnect(); se.Dispose(); }//如果删除的用户在线,则将其踢出 Users.Remove(card.UserID); DBHelper.DelUser(card.UserID);//数据库中删除 orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(e.XMLMsg);//将删除的用户广播给所有在线用户 } }
/// <summary> /// 获得组织机构版本信息 /// </summary> /// <param name="org"></param> private void onOrgVersion(OrgVersion org) { try { #region 判断登录的用户本地数据库文件夹是否存在 System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(MyAuth.UserID); if (!dInfo.Exists) dInfo.Create(); string FileNamePath =Application.StartupPath +"\\"+ MyAuth.UserID + "\\Record.mdb"; if (!System.IO.File.Exists(FileNamePath)) System.IO.File.Copy(Application.StartupPath + "\\Record.db", FileNamePath); ////初始化本地数据库连接字符串 IMLibrary3.Data.SQLiteDBHelper.connectionString = "data source=" + FileNamePath; #endregion OrgVersion localOrg = OpeRecordDB.GetOrgVersion(MyAuth.UserID); #region 如果版本已经改变下载组织机构组信息 if (org.GroupsVersion != localOrg.GroupsVersion)//true { Groups.Clear(); treeView_Organization.Nodes.Clear(); OpeRecordDB.DeleteAllGroup();//删除本地数据库中分组信息 frmOrg = new FormDownOrganization(); frmOrg.Shown += delegate(object sender, EventArgs e) { DownloadGroups dGroups = new DownloadGroups(); dGroups.from = MyAuth.UserID; dGroups.type = type.get; SendMessageToServer(dGroups);//请求下载分组信息 }; frmOrg.MaxValue = org.GroupsCount; frmOrg.ShowText = "正在下载分组信息..."; frmOrg.ShowDialog(); OpeRecordDB.AddGroups(this.Groups);//将下载的分组信息保存于数据库中 } #endregion #region 下载组织机构用户信息 if (org.UsersVersion != localOrg.UsersVersion)//(true) { Users.Clear(); treeView_Organization.Nodes.Clear(); OpeRecordDB.DeleteAllUser();//删除本地数据库中用户信息 frmOrg = new FormDownOrganization(); frmOrg.Shown += delegate(object sender, EventArgs e) { DownloadUsers dUsers = new DownloadUsers(); dUsers.from = MyAuth.UserID; dUsers.type = type.get; SendMessageToServer(dUsers);//请求下载联系人信息 }; frmOrg.MaxValue = org.UsersCount; frmOrg.ShowText = "正在下载用户信息..."; frmOrg.ShowDialog(); OpeRecordDB.AddUsers(this.Users); } #endregion #region 下载群信息 if (org.RoomsCount !=0)//如果群数大于0 { Rooms.Clear(); treeView_Rooms.Nodes.Clear(); //OpeRecordDB.DeleteAllRoom();//删除本地数据库中群信息 frmOrg = new FormDownOrganization(); frmOrg.Shown += delegate(object sender, EventArgs e) { DownloadRooms dRooms = new DownloadRooms(); dRooms.from = MyAuth.UserID; dRooms.type = type.get; SendMessageToServer(dRooms);//请求下载群信息 }; frmOrg.MaxValue = org.RoomsCount; frmOrg.ShowText = "正在下载群资料..."; frmOrg.ShowDialog(); //OpeRecordDB.AddRooms(this.Rooms); } #endregion //如果组织机构已经更改或组织机构未加载到树图 if (this.treeView_Organization.Nodes.Count == 0) { //则更新到本地数据库 OpeRecordDB.UpdateOrgVersion(org); this.Groups = OpeRecordDB.GetGroups(); this.Users = OpeRecordDB.GetUsers(); treeViewAddOrg();//加载树 } if (this.treeView_Rooms.Nodes.Count == 0) { //this.Rooms = OpeRecordDB.GetRooms(); foreach (Room room in this.Rooms) { foreach (string userID in room.UserIDs.Split(';')) { User user = findUser(userID); if (user != null) room.Users.Add(user.UserID, user); } } treeViewAddRooms(this.Rooms); } //请求联系人在线状态信息 Presence pre = new Presence(); pre.from = MyAuth.UserID; pre.type = type.get;//必须设置 set,以表示是设置,如果为get,则是获取所有联系人的状态 SendMessageToServer(pre); } catch (Exception ex) { IMLibrary3.Global.MsgShow(ex.Source + ex.Message); } }
/// <summary> /// 获得组织机构版本 /// </summary> /// <param name="UserID"></param> /// <returns></returns> public static IMLibrary3.Protocol.OrgVersion GetOrgVersion(string UserID) { IMLibrary3.Protocol.OrgVersion orgVersion = null; #region 判断登录的用户本地数据库文件夹及文件是否存在,不存在返回空值 //System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(UserID); //if (!dInfo.Exists) // return null; //string FileNamePath = UserID + @"\Record.mdb"; //if (!System.IO.File.Exists(FileNamePath)) // return null; ////如果本地数据库存在,则设置数据库连接字符串 //DBFileNamePath = FileNamePath; //conStr = "data source=" + DBFileNamePath; #endregion try { string sql = "select * from OrgVersion"; System.Data.SQLite.SQLiteDataReader dr = SQLiteDBHelper.ExecuteReader(sql, null ); if (dr != null && dr.Read()) { orgVersion = new IMLibrary3.Protocol.OrgVersion(); orgVersion.GroupsVersion = Convert.ToString(dr["GroupsVersion"]).Trim(); orgVersion.UsersVersion = Convert.ToString(dr["UsersVersion"]).Trim(); orgVersion.RoomsVersion = Convert.ToString(dr["RoomsVersion"]).Trim(); } dr.Close(); dr.Dispose(); return orgVersion; } catch (Exception ex) { MessageBox.Show(ex.Source + ex.Message); } return null; }
/// <summary> /// 开始服务 /// </summary> /// <param name="P2PFileServerPort">P2P服务传输服务端口(UDP)</param> /// <param name="ImageFileServerPort">图片文件传输服务端口(TCP)</param> /// <param name="MessageServerPort">消息服务端口(TCP)</param> /// <param name="P2PAVServerPort">音视频服务端口(UDP)</param> public void Start(int P2PFileServerPort, int P2PAVServerPort, int ImageFileServerPort, int MessageServerPort) { #region 初始化组织架构 ///初始化数据库连接字符串 IMLibrary.SqlData.DataAccess.ConnectionString = DBHelper.settings.SqlConStr; /// 初始化用户 if (Users == null) { Users = DBHelper.GetUserVcards(); } /// 获取组织机构版本和信息 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 } #region 初始化群 if (Rooms == null) { Rooms = DBHelper.GetRooms(); foreach (Room room in Rooms.Values) { #region 将内存中的用户添加到内存中的群 string[] userids = room.UserIDs.Split(';'); foreach (string userID in userids) { UserVcard user = getUser(userID); if (user != null) { if (room.Users == null) { room.Users = new Dictionary <string, User>(); } if (!room.Users.ContainsKey(userID)) { if (room.CreateUserID == userID) { user.CreatedRoomsCount++; //标记用户已创建群数 } room.Users.Add(user.UserID, user); } if (user.Rooms == null) { user.Rooms = new Dictionary <string, Room>(); } if (!user.Rooms.ContainsKey(room.RoomID)) { user.Rooms.Add(room.RoomID, room); } } } room.UserIDs = "";//重新生成群包含的Users foreach (User u in GetUsers(room)) { room.UserIDs += u.UserID + ";"; } #endregion } } #endregion #endregion //启动消息服务 if (msgServer == null) { msgServer = new MsgServer(MessageServerPort); msgServer.Login += new MsgServer.MsgServerEventHandler(msgServer_Login); msgServer.UserOffline += new MsgServer.MsgServerEventHandler(msgServer_UserOffline); msgServer.Message += new MsgServer.MsgServerEventHandler(msgServer_Message); msgServer.Presence += new MsgServer.MsgServerEventHandler(msgServer_Presence); msgServer.RequestChangeGroupVcard += new MsgServer.MsgServerEventHandler(msgServer_RequestChangeGroupVcard); msgServer.RequestChangePassword += new MsgServer.MsgServerEventHandler(msgServer_RequestChangePassword); msgServer.RequestChangeRoom += new MsgServer.MsgServerEventHandler(msgServer_RequestChangeRoom); msgServer.RequestChangeUserVcard += new MsgServer.MsgServerEventHandler(msgServer_RequestChangeUserVcard); msgServer.RequestOrgGroups += new MsgServer.MsgServerEventHandler(msgServer_RequestOrgGroups); msgServer.RequestOrgRooms += new MsgServer.MsgServerEventHandler(msgServer_RequestOrgRooms); msgServer.RequestOrgUsers += new MsgServer.MsgServerEventHandler(msgServer_RequestOrgUsers); msgServer.RequestUsersPresence += new MsgServer.MsgServerEventHandler(msgServer_RequestUsersPresence); msgServer.RouteDownLoadImageFile += new MsgServer.MsgServerEventHandler(msgServer_RouteDownLoadImageFile); msgServer.RouteP2PFileTransmit += new MsgServer.MsgServerEventHandler(msgServer_RouteP2PFileTransmit); msgServer.RouteAVMsg += new MsgServer.MsgServerEventHandler(msgServer_RouteAVMsg); } msgServer.Start(); //启动P2P文件服务 if (p2pFileServer == null) { p2pFileServer = new P2PFileServer(P2PFileServerPort); } p2pFileServer.Start(); //启动图片文件服务 if (imageFileServer == null) { imageFileServer = new ImageFileServer(ImageFileServerPort); } imageFileServer.Start(); //启动音视频传输服务 if (p2pAVServer == null) { p2pAVServer = new P2PAVServer(P2PAVServerPort); } p2pAVServer.Start(); }
void msgServer_RequestChangeUserVcard(object sender, MsgServerEventArgs e) { UserVcard card = e.obj as UserVcard; if (card.UserID.Trim() == "" || card.UserName.Trim() == "" || card.GroupID.Trim() == "") { return; } card.UserID = card.UserID.Trim(); UserVcard user = getUser(card.UserID); if (card.type == type.New && user == null && e.RequestUser.isAdmin)//如果用户不存在,创建新用户 { Users.Add(card.UserID, card); //设置新用户密码为123456; card.Password = IMLibrary3.Security.Hasher.GetMD5Hash(IMLibrary3.Operation.TextEncoder.textToBytes("123456")); DBHelper.CreateUserVcard(card); //保存于数据库 orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(e.XMLMsg); //将创建的新用户信息广播给所有在线用户 } else if (card.type == type.set && e.RequestUser.isAdmin) //如果是更新用户 { if (user == null) { return; //如果用户不存在则退出 } string oldInfo = user.UserName + user.GroupID + user.OrderID; //老信息 string newInfo = card.UserName.Trim() + card.GroupID.Trim() + card.OrderID; //新信息 user.UserName = card.UserName.Trim(); user.GroupID = card.GroupID.Trim(); user.OrderID = card.OrderID; user.Job = card.Job.Trim(); user.OfficePhone = card.OfficePhone.Trim(); user.Mail = card.Mail.Trim(); user.Phone = card.Phone.Trim(); user.Post = card.Post.Trim(); user.Remark = card.Remark.Trim(); user.Birthday = card.Birthday.Trim(); user.Sex = card.Sex; user.CreateRooms = card.CreateRooms; user.Disable = card.Disable; user.isAdmin = card.isAdmin; user.isSendNotice = card.isSendNotice; user.isSendSMS = card.isSendSMS; DBHelper.UpdateUserVcard(user); //更新数据库 if (oldInfo != newInfo) //如果用户基本信息发生变更,则变更组织架构 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 } SendMessageToUser(e.RequestUser, e.XMLMsg); //通知管理员创建新用户成功 } else if (card.type == type.get) //如果是获取用户资料 { if (user == null) { return; //如果用户不存在则退出 } user.type = type.result; SendMessageToUser(e.RequestUser, Factory.CreateXMLMsg(user)); //将用户资料发送给请求者 } else if (card.type == type.delete && e.RequestUser.isAdmin) //如果是删除用户 { if (user == null) { return; //如果用户不存在则退出 } if (e.RequestUser.UserID == user.UserID) { return; //如果管理员想删除自己,退出 } TCPServerSession se = user.Tag as TCPServerSession; if (se != null && se.IsConnected) { se.Disconnect(); se.Dispose(); } //如果删除的用户在线,则将其踢出 Users.Remove(card.UserID); DBHelper.DelUser(card.UserID); //数据库中删除 orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(e.XMLMsg); //将删除的用户广播给所有在线用户 } }
/// <summary> /// 管理员变更分组信息 /// </summary> /// <param name="card"></param> /// <param name="xmlMsg"></param> /// <param name="session"></param> private void onGroupVcard(GroupVcard card, string xmlMsg, TCPServerSession session) { if (card.GroupID.Trim() == "" || card.GroupName.Trim() == "") return; card.GroupID = card.GroupID.Trim(); UserVcard requestUser = session.Tag as UserVcard;//请求用户 if (card.type == type.New && requestUser.isAdmin)//如果用户是管理员 { if (DBHelper.CreateGroupVcard(card) > 0)//保存于数据库 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(xmlMsg);//将创建的分组信息广播给所有在线用户 } } else if (card.type == type.set && requestUser.isAdmin)//如果是更新分组信息 { if (DBHelper.UpdateGroupVcard(card) > 0) //更新数据库 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 SendMessageToUser(requestUser, xmlMsg);//通知管理员创建新用户成功 } } else if (card.type == type.get)//如果是更新 { card.type = type.result; SendMessageToUser(requestUser, Factory.CreateXMLMsg(card));//将分组信息资料发送给请求者 } else if (card.type == type.delete && requestUser.isAdmin)//如果是删除分组 { if (DBHelper.DelGroup(card.GroupID) > 0) //数据库中删除 { orgVersion = DBHelper.ResetOrgVersion(); //重设组织架构版本相关信息 BroadcastingMessageToOnlineUser(xmlMsg);//将删除的用户广播给所有在线用户 } } }
/// <summary> /// 重设企业组织架构版本信息 /// </summary> public static IMLibrary3.Protocol.OrgVersion ResetOrgVersion() { IMLibrary3.Protocol.OrgVersion OrgVersion = new IMLibrary3.Protocol.OrgVersion(); string GroupsVersion = ""; string UsersVersion = ""; int sendGroupsCount = 80; //每次发送分组信息数量(太多发送不到对方,太少服务器负载增加) int sendUsersCount = 80; //每次发送用户信息数量(太多发送不到对方,太少服务器负载增加) Dictionary <string, Group> Groups = GetGroups(); //获得所有分组信息 if (Groups != null) { IMLibrary3.Protocol.DownloadGroups pGroups = new IMLibrary3.Protocol.DownloadGroups(); pGroups.type = IMLibrary3.Protocol.type.result; int i = 0; foreach (Group group in Groups.Values) { pGroups.Data.Add(group); i++; if (i == sendGroupsCount) { GroupsVersion += IMLibrary3.Protocol.Factory.CreateXMLMsg(pGroups); OrgVersion.GroupsXML.Add(IMLibrary3.Protocol.Factory.CreateXMLMsg(pGroups)); pGroups.Data = new List <object>();//分组信息清零 i = 0; } } if (pGroups.Data.Count > 0)//剩下的分组信息 { GroupsVersion += IMLibrary3.Protocol.Factory.CreateXMLMsg(pGroups); OrgVersion.GroupsXML.Add(IMLibrary3.Protocol.Factory.CreateXMLMsg(pGroups)); } OrgVersion.GroupsCount = Groups.Count; OrgVersion.GroupsVersion = IMLibrary3.Security.Hasher.GetMD5Hash(Encoding.Default.GetBytes(GroupsVersion)); } Dictionary <string, User> Users = GetUsers();//获得所有用户信息 if (Users != null) { IMLibrary3.Protocol.DownloadUsers pUsers = new IMLibrary3.Protocol.DownloadUsers(); pUsers.type = IMLibrary3.Protocol.type.result; int i = 0; foreach (User user in Users.Values) { pUsers.Data.Add(user); i++; if (i == sendUsersCount) { UsersVersion += IMLibrary3.Protocol.Factory.CreateXMLMsg(pUsers); OrgVersion.UsersXML.Add(IMLibrary3.Protocol.Factory.CreateXMLMsg(pUsers)); pUsers.Data = new List <object>();//用户信息清零 i = 0; } } if (pUsers.Data.Count > 0)//剩下的用户信息 { UsersVersion += IMLibrary3.Protocol.Factory.CreateXMLMsg(pUsers); OrgVersion.UsersXML.Add(IMLibrary3.Protocol.Factory.CreateXMLMsg(pUsers)); } OrgVersion.UsersCount = Users.Count; OrgVersion.UsersVersion = IMLibrary3.Security.Hasher.GetMD5Hash(Encoding.Default.GetBytes(UsersVersion)); } return(OrgVersion); }
/// <summary> /// 重设企业组织架构版本信息 /// </summary> public static IMLibrary3.Protocol.OrgVersion ResetOrgVersion() { IMLibrary3.Protocol.OrgVersion OrgVersion = new IMLibrary3.Protocol.OrgVersion(); string GroupsVersion = ""; string UsersVersion = ""; int sendGroupsCount =80;//每次发送分组信息数量(太多发送不到对方,太少服务器负载增加) int sendUsersCount = 80;//每次发送用户信息数量(太多发送不到对方,太少服务器负载增加) Dictionary<string, Group> Groups = GetGroups();//获得所有分组信息 if (Groups != null) { IMLibrary3.Protocol.DownloadGroups pGroups = new IMLibrary3.Protocol.DownloadGroups(); pGroups.type = IMLibrary3.Protocol.type.result; int i = 0; foreach (Group group in Groups.Values) { pGroups.Data.Add(group); i++; if (i == sendGroupsCount) { GroupsVersion += IMLibrary3.Protocol.Factory.CreateXMLMsg(pGroups); OrgVersion.GroupsXML.Add(IMLibrary3.Protocol.Factory.CreateXMLMsg(pGroups)); pGroups.Data = new List<object>();//分组信息清零 i = 0; } } if (pGroups.Data.Count > 0)//剩下的分组信息 { GroupsVersion += IMLibrary3.Protocol.Factory.CreateXMLMsg(pGroups); OrgVersion.GroupsXML.Add(IMLibrary3.Protocol.Factory.CreateXMLMsg(pGroups)); } OrgVersion.GroupsCount = Groups.Count; OrgVersion.GroupsVersion = IMLibrary3.Security.Hasher.GetMD5Hash(Encoding.Default.GetBytes(GroupsVersion)); } Dictionary<string, User> Users = GetUsers();//获得所有用户信息 if (Users != null) { IMLibrary3.Protocol.DownloadUsers pUsers = new IMLibrary3.Protocol.DownloadUsers(); pUsers.type = IMLibrary3.Protocol.type.result; int i = 0; foreach (User user in Users.Values) { pUsers.Data.Add(user); i++; if (i == sendUsersCount) { UsersVersion += IMLibrary3.Protocol.Factory.CreateXMLMsg(pUsers); OrgVersion.UsersXML.Add(IMLibrary3.Protocol.Factory.CreateXMLMsg(pUsers)); pUsers.Data = new List<object>();//用户信息清零 i = 0; } } if (pUsers.Data.Count > 0)//剩下的用户信息 { UsersVersion += IMLibrary3.Protocol.Factory.CreateXMLMsg(pUsers); OrgVersion.UsersXML.Add(IMLibrary3.Protocol.Factory.CreateXMLMsg(pUsers)); } OrgVersion.UsersCount = Users.Count; OrgVersion.UsersVersion = IMLibrary3.Security.Hasher.GetMD5Hash(Encoding.Default.GetBytes(UsersVersion)); } return OrgVersion; }