public void ClientAccepted(IAsyncResult ar) { var socket = ar.AsyncState as Socket; //这就是客户端的Socket实例,我们后续可以将其保存起来 var client = new mcSocketServerClient(socket.EndAccept(ar)); if (!Clients.Contains(client)) { Clients.Add(client); } //给客户端发送一个欢迎消息 client.Send("Hi there, I accept you request at " + DateTime.Now.ToString()); //实现每隔两秒钟给服务器发一个消息 //这里我们使用了一个定时器 var timer = new System.Timers.Timer(); timer.Interval = 2000D; timer.Enabled = true; timer.Elapsed += (o, a) => { //检测客户端Socket的状态 if (client.Connected) { try { client.Send("Message from server at " + DateTime.Now.ToString()); } catch (SocketException ex) { //Console.WriteLine(ex.Message); //messages.Add(ex.Message); Show("模块:ClientAccepted-" + ex.Message); } } else { Clients.Remove(client); client.dispose(); timer.Stop(); timer.Enabled = false; //messages.Add("Client is disconnected, the timer is stop."); Show("Client is disconnected, the timer is stop."); } }; timer.Start(); //接收客户端的消息(这个和在客户端实现的方式是一样的) client.BeginReceive(new AsyncCallback(ReceiveMessage), client); //准备接受下一个客户端请求 socket.BeginAccept(new AsyncCallback(ClientAccepted), socket); }
private static void searchDepartment(DataTable pDT, mcSocketServerClient pmSocketClient, string pFm) { DataTable tDT = mscDB.Search("*", "departments", null); tDT = addTableHead("searchDepartment", true, string.Empty, pFm, tDT); pmSocketClient.Send(tDT); }
private static void checkVersion(DataTable pDT, mcSocketServerClient pmSocketClient, string pMessage) { bool tFlag = true; string tMessage = string.Empty; string tServerVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); if (string.Compare(pMessage, tServerVersion) < 0) { string tS1 = pMessage.Remove(pMessage.Length - 2, 2); string tS2 = tServerVersion.Remove(tServerVersion.Length - 2, 2); if (tS1 != tS2) { tFlag = false; } tMessage = "客户端版本号过低,当前服务器端版本号: V" + tServerVersion + " 请联系管理员更新客户端版本。"; } if (string.Compare(pMessage, tServerVersion) > 0) { tMessage = "客户端版本号过高,继续使用可能导致错误,当前服务器端版本号: V" + tServerVersion + " 建议选用匹配的客户端版本以获得更好的使用体验。"; } DataTable tDT = new DataTable(); tDT = addTableHead("checkVersion", tFlag, tMessage, "FmLogIn", tDT); pmSocketClient.Send(tDT); }
private static void searchAuthority(DataTable pDT, mcSocketServerClient pmSocketClient) { DataTable tDT = mscDB.Search("*", "authority", null); tDT = addTableHead("searchAuthority", true, string.Empty, "mscAuthority", tDT); pmSocketClient.Send(tDT); }
private static void searchStaffsApp(DataTable pDT, mcSocketServerClient pmSocketClient, string pMessage = "") { string tAccount = pDT.Rows[0]["Account"].ToString(); string tDepartment = pDT.Rows[0]["Department"].ToString(); string tAuthority = mscAuthority.Result(pDT.Rows[0]["Authority"].ToString(), "StaffsManagement"); string tWHERE = null; switch (tAuthority) { case "普通成员": tWHERE = "Authority = '" + "成员" + "'"; tWHERE += " and Department='" + "-" + tDepartment + "'"; break; case "所有成员": tWHERE = "Department='" + tDepartment + "' OR Department='" + "-" + tDepartment + "'"; break; default: break; } DataTable tDT = mscDB.Search("*", "Staffs", tWHERE); tDT.Columns.Remove("Password"); tDT = addTableHead("searchStaffsApp", true, pMessage, "FmStaffs", tDT); pmSocketClient.Send(tDT); }
private static void accountLogIn(DataTable pDT, mcSocketServerClient pmSocketClient, string pFm) { string tAccount = pDT.Rows[0][0].ToString(); string tPassword = pDT.Rows[0][1].ToString(); string tWHERE = "Account='" + tAccount + "'"; tWHERE += " and Password='******'"; DataTable tDT = mscDB.Search("*", "Staffs", tWHERE); if (tDT != null && tDT.Rows.Count > 0) { if (socketServer.ClientsAccount.Keys.Contains(tAccount)) { tDT = addTableHead("accountLogIn", false, "账号已在别处登陆", pFm, tDT); } else { tDT = addTableHead("accountLogIn", true, "登陆成功", pFm, tDT); pmSocketClient.Account = tAccount; } } else { tDT = addTableHead("accountLogIn", false, "账号或密码错误", pFm, tDT); } pmSocketClient.Send(tDT); }
private static void accountSignIn(DataTable pDT, mcSocketServerClient pmSocketClient) { string tAccount = pDT.Rows[0]["Account"].ToString(); string tWHERE = "Account='" + tAccount + "'"; DataTable tDT = mscDB.Search("Account", "Staffs", tWHERE); if (tDT.Rows.Count != 0)//账号已存在 { tDT = addTableHead("accountSignIn", false, "账号已存在", "FmLogIn", tDT); } else//注册新账号 { string tPassword = pDT.Rows[0]["Password"].ToString(); string tName = pDT.Rows[0]["Name"].ToString(); string tEmail = pDT.Rows[0]["Email"].ToString(); string tDepartment = pDT.Rows[0]["Department"].ToString(); string tMajor = pDT.Rows[0]["Major"].ToString(); string tAuthority = pDT.Rows[0]["Authority"].ToString(); string tePassword = tPassword; string tFIELD = "Account,Password,Name,Email,Department,Major,Authority"; string tVALUES = "'" + tAccount + "'," + "'" + tePassword + "'," + "'" + tName + "'," + "'" + tEmail + "'," + "'" + tDepartment + "'," + "'" + tMajor + "'," + "'" + tAuthority + "'"; if (mscDB.Insert("Staffs", tFIELD, tVALUES)) { tWHERE = "Account='" + tAccount + "'"; tWHERE += " and Password='******'"; tDT = mscDB.Search("*", "Staffs", tWHERE); tDT = addTableHead("accountSignIn", true, "", "FmLogIn", tDT); pmSocketClient.Account = tAccount; } else { tDT = addTableHead("accountSignIn", false, "账号注册失败", "FmLogIn", tDT); } } pmSocketClient.Send(tDT); }
private static void updateStaffInfo(DataTable pDT, mcSocketServerClient pmSocketClient, string pMessage, string pFm) { string tAccount = pDT.Rows[0]["Account"].ToString(); string tPW = pDT.Rows[0]["Password"].ToString(); string tName = pDT.Rows[0]["Name"].ToString(); string tEmail = pDT.Rows[0]["Email"].ToString(); string tMajor = pDT.Rows[0]["Major"].ToString(); string tRemark = pDT.Rows[0]["Remark"].ToString(); string tDep = pDT.Rows[0]["Department"].ToString(); string tAuthority = pDT.Rows[0]["Authority"].ToString(); if (tDep.StartsWith("-")) { tAuthority = "成员"; } string tWHERE = "Account='" + tAccount + "'"; DataTable tDT = mscDB.Search("Account", "Staffs", tWHERE); if (tDT.Rows.Count != 0)//账号存在 { string tFIELD = "Name = '" + tName + "' , Email = '" + tEmail + "' , Major = '" + tMajor + "' , Remark = '" + tRemark + "' , Department = '" + tDep + "' , Authority = '" + tAuthority + "'"; if (pMessage == "PW") { tFIELD += " , Password = '******'"; } tWHERE = "Account='" + tAccount + "'"; DataTable rtDT = new DataTable(); if (mscDB.Update("staffs", tFIELD, tWHERE)) { rtDT = addTableHead("updateStaffInfo", true, "更新人员信息成功", pFm, new DataTable()); } else { rtDT = addTableHead("updateStaffInfo", false, "更新人员信息失败", pFm, rtDT); } pmSocketClient.Send(rtDT); } }
private static void deleteMission(DataTable pDT, mcSocketServerClient pmSocketClient, string pFm) { string tIN = string.Empty; List <string> pushList = new List <string>();//推送列表 foreach (DataRow feDR in pDT.Rows) { string tKey = feDR[0].ToString(); tIN += tKey + ","; var tAccountDT = mscDB.Search("Executor", "missions", "`Key` = " + tKey); var tExecutor = tAccountDT.Rows[0][0].ToString(); if (!pushList.Contains(tExecutor)) { pushList.Add(tExecutor); } } if (tIN == string.Empty) { return; } tIN = tIN.Remove(tIN.Length - 1, 1); string tWHERE = "`Key` IN (" + tIN + ")"; bool tFlag = mscDB.Delete("missions", tWHERE); DataTable tDT = new DataTable(); tDT = addTableHead("deleteMission", tFlag, string.Empty, pFm, tDT); //if (tFlag) //{ // tDT = addTableHead("deleteMission", true, string.Empty, pFm, tDT); //} //else //{ // tDT = addTableHead("deleteMission", false, string.Empty, pFm, tDT); //} pmSocketClient.Send(tDT); pushMessage(pmSocketClient, pushList); }
private static void searchProject(DataTable pDT, mcSocketServerClient pmSocketClient, string pMessage, string pFm) { string tWHERE = null; if (pMessage == "blurry") { string tSearchStr = pDT.Rows[0][0].ToString(); tWHERE = "CONCAT(IFNULL(`Name`,''),IFNULL(`Project_Index`,''),IFNULL(`Project_Name`,''),IFNULL(`Project_Location`,''),IFNULL(`Schedule`,''),IFNULL(`MajorDepartment`,''),IFNULL(`Major`,''),IFNULL(`Version`,''),IFNULL(`Date_Handover`,''),IFNULL(`Date_Submit`,''),IFNULL(`Date_Publish`,''),IFNULL(`Status`,''),IFNULL(`Project_Manager`,''),IFNULL(`Technical_Director`,''),IFNULL(`Designer`,''),IFNULL(`Proofreader`,''),IFNULL(`Auditor`,''),IFNULL(`Remark`,''),IFNULL(`Date_Finish`,'')) LIKE '%%" + tSearchStr + "%%'"; } else { #region 精确检索条件 string tIndex = pDT.Rows[0][0].ToString(); string tName = pDT.Rows[0][1].ToString(); string tLocation = pDT.Rows[0][2].ToString(); string tMajorDep = pDT.Rows[0][3].ToString(); string tPM = pDT.Rows[0][4].ToString(); string tTD = pDT.Rows[0][5].ToString(); string tExe = pDT.Rows[0][6].ToString(); if (tIndex != string.Empty) { tWHERE = "Project_Index LIKE '%%" + tIndex + "%%'"; } if (tName != string.Empty) { if (tWHERE != null) { tWHERE += " AND Project_Name LIKE '%%" + tName + "%%'"; } else { tWHERE = "Project_Name LIKE '%%" + tName + "%%'"; } } if (tLocation != string.Empty) { if (tWHERE != null) { tWHERE += " AND Project_Location LIKE '%%" + tLocation + "%%'"; } else { tWHERE = "Project_Location LIKE '%%" + tLocation + "%%'"; } } if (tMajorDep != string.Empty) { if (tWHERE != null) { tWHERE += " AND MajorDepartment LIKE '%%" + tMajorDep + "%%'"; } else { tWHERE = "MajorDepartment LIKE '%%" + tMajorDep + "%%'"; } } if (tPM != string.Empty) { if (tWHERE != null) { tWHERE += " AND Project_Manager LIKE '%%" + tPM + "%%'"; } else { tWHERE = "Project_Manager LIKE '%%" + tPM + "%%'"; } } if (tTD != string.Empty) { if (tWHERE != null) { tWHERE += " AND Technical_Director LIKE '%%" + tTD + "%%'"; } else { tWHERE = "Technical_Director LIKE '%%" + tTD + "%%'"; } } if (tExe != string.Empty) { if (tWHERE != null) { tWHERE += " AND Executor LIKE '%%" + tExe + "%%'"; } else { tWHERE = "Executor LIKE '%%" + tExe + "%%'"; } } #endregion } string tField = "Project_Index,Project_Name,MajorDepartment,Project_Manager,Technical_Director,Designer,Proofreader,Auditor"; DataTable tDT = mscDB.Search("DISTINCT " + tField, "missions", tWHERE); if (tDT.Rows.Count >= 35) { tDT = addTableHead("searchProject", false, "符合条件的记录过多,请深化检索条件", pFm, new DataTable()); } else { tDT = addTableHead("searchProject", true, string.Empty, pFm, tDT); } pmSocketClient.Send(tDT); }
private static void searchMissions(DataTable pDT, mcSocketServerClient pmSocketClient, string pMessage, string pFm) { DataTable tDT = null; string tWHERE = null; switch (pMessage) { case "staffs": foreach (DataRow feDR in pDT.Rows) { string tExecutor = feDR["Account"].ToString(); tWHERE = "Executor = '" + tExecutor + "' AND Status <> '已完成'"; tDT = searchAddDT(tDT, "*", "missions", tWHERE); } break; case "projects": foreach (DataRow feDR in pDT.Rows) { tWHERE = string.Empty; for (int i = 0; i < pDT.Columns.Count; i++) { tWHERE += " AND " + pDT.Columns[i].ColumnName + "='" + feDR[i].ToString() + "'"; } if (tWHERE != string.Empty) { tWHERE = tWHERE.Remove(0, 5); } tDT = searchAddDT(tDT, "*", "missions", tWHERE); } break; case "plan": string tDate_Submit1 = "date_format(Date_Submit, '%YY/%mm/%dd')"; string tStratDate1 = "date_format('" + pDT.Rows[0][0].ToString() + "','%YY/%mm/%dd')"; string tEndDate1 = "date_format('" + pDT.Rows[0][1].ToString() + "','%YY/%mm/%dd')"; tWHERE = tDate_Submit1 + " >= " + tStratDate1 + " AND " + tDate_Submit1 + "<=" + tEndDate1 + " AND Status <> '已完成'"; //tDT = mscDB.Search("*", "missions", tWHERE); foreach (DataRow feDR in pDT.Rows) { tDT = searchAddDT(tDT, "*", "missions", tWHERE + " AND Executor = '" + feDR[2] + "'"); } break; case "achievement": string tDate_Finish = "date_format(Date_Finish, '%YY/%mm/%dd')"; string tStratDate2 = "date_format('" + pDT.Rows[0][0].ToString() + "','%YY/%mm/%dd')"; string tEndDate2 = "date_format('" + pDT.Rows[0][1].ToString() + "','%YY/%mm/%dd')"; tWHERE = tDate_Finish + " >= " + tStratDate2 + " AND " + tDate_Finish + "<=" + tEndDate2 + " AND Status = '已完成' AND Fee <> ''"; //tDT = mscDB.Search("*", "missions", tWHERE); foreach (DataRow feDR in pDT.Rows) { tDT = searchAddDT(tDT, "*", "missions", tWHERE + " AND Executor = '" + feDR[2] + "'"); } break; case "any": break; } tDT = addTableHead("searchMissions", true, string.Empty, pFm, tDT); pmSocketClient.Send(tDT); }
private static void uploadMissions(DataTable pDT, mcSocketServerClient pmSocketClient, string pFm) { int UpdateSucc = 0; int UpdateCnt = 0; int InsertSucc = 0; int InsertCnt = 0; List <string> pushList = new List <string>();//推送列表 foreach (DataRow feDR in pDT.Rows) { DataTable feDT = uploadMission(feDR, pmSocketClient); string tExecutor = string.Empty; if (feDR.Table.Columns.Contains("Executor")) { tExecutor = feDR["Executor"].ToString(); } switch (feDT.Rows[0][2].ToString()) { case "编辑任务失败": UpdateCnt++; break; case "添加任务失败": InsertCnt++; break; case "编辑任务成功": UpdateSucc++; UpdateCnt++; if (!pushList.Contains(tExecutor)) { pushList.Add(tExecutor); } break; case "添加任务成功": InsertSucc++; InsertCnt++; if (!pushList.Contains(tExecutor)) { pushList.Add(tExecutor); } break; default: break; } } DataTable tDT = new DataTable(); string tStr = "编辑任务" + UpdateCnt.ToString() + "个,成功" + UpdateSucc.ToString() + "个;"; tStr += "\r\n" + "添加任务" + InsertCnt.ToString() + "个,成功" + InsertSucc.ToString() + "个;"; if (UpdateSucc + InsertSucc == 0) { tDT = addTableHead("uploadMissions", false, tStr, pFm, tDT); } else { tDT = addTableHead("uploadMissions", true, tStr, pFm, tDT); } pmSocketClient.Send(tDT); pushMessage(pmSocketClient, pushList); }