From fc24506b96d1404a5e76d87e41c770bd8519726c Mon Sep 17 00:00:00 2001 From: Somuns Date: Fri, 22 Jan 2021 14:11:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=94=E8=B0=83=E4=BA=86=E4=B8=80=E5=A4=A9?= =?UTF-8?q?=EF=BC=8C=E6=9A=82=E5=AD=98=EF=BC=8C=E7=AD=89=E5=BE=85=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E5=AE=8C=E6=88=90=E5=88=86=E5=89=B2=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E6=98=BE=E7=A4=BA=E5=88=97=E8=A1=A8=20?= =?UTF-8?q?=E5=87=86=E5=A4=87=E6=95=B0=E6=8D=AE=E5=BA=93=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StuMgmLib/MyNameSpace/CommonData.cs | 155 +++++-------- StuMgmServer/StuMgmLib/MyNameSpace/Data.cs | 85 +++++++ .../StuMgmLib/MyNameSpace/DataAnalyze.cs | 4 +- .../StuMgmLib/MyNameSpace/SystemComm.cs | 95 ++++++++ .../StuMgmLib/MyNameSpace/SystemCtrl.cs | 217 ++++++++++++++++++ .../StuMgmLib/MyNameSpace/SystemData.cs | 40 ++++ StuMgmServer/StuMgmLib/MyNameSpace/TcpConn.cs | 43 ++-- StuMgmServer/StuMgmLib/StuMgmLib.csproj | 5 +- StuMgmServer/StuMgmServer/Server.Designer.cs | 2 +- StuMgmServer/StuMgmServer/Server.cs | 25 +- StuMgmServer/Test/Data.cs | 201 ++++++++++++++++ StuMgmServer/Test/SystemComm.cs | 95 ++++++++ StuMgmServer/Test/SystemCtrl.cs | 88 +++++++ StuMgmServer/Test/SystemData.cs | 40 ++++ StuMgmServer/Test/Test.csproj | 4 + 15 files changed, 959 insertions(+), 140 deletions(-) create mode 100644 StuMgmServer/StuMgmLib/MyNameSpace/Data.cs create mode 100644 StuMgmServer/StuMgmLib/MyNameSpace/SystemComm.cs create mode 100644 StuMgmServer/StuMgmLib/MyNameSpace/SystemCtrl.cs create mode 100644 StuMgmServer/StuMgmLib/MyNameSpace/SystemData.cs create mode 100644 StuMgmServer/Test/Data.cs create mode 100644 StuMgmServer/Test/SystemComm.cs create mode 100644 StuMgmServer/Test/SystemCtrl.cs create mode 100644 StuMgmServer/Test/SystemData.cs diff --git a/StuMgmServer/StuMgmLib/MyNameSpace/CommonData.cs b/StuMgmServer/StuMgmLib/MyNameSpace/CommonData.cs index 561e6cf..0f98490 100644 --- a/StuMgmServer/StuMgmLib/MyNameSpace/CommonData.cs +++ b/StuMgmServer/StuMgmLib/MyNameSpace/CommonData.cs @@ -11,25 +11,52 @@ namespace StuMgmLib.MyNameSpace { public class Info { - [Serializable] - public class ClientSend + internal const Int16 tokenMaxCount = 32767; + internal static Dictionary myToken = new Dictionary(); + } + + #region ClientClass + + public enum ClientFunc + { + VerifyLogin = 1, + GetCourseInfo = 2, + GetUserCourseInfo = 3, + } + public enum LvErr + { + NotFound = -1, + Error = -2, + Admin = 1, + Teacher = 2, + Student = 3, + } + [Serializable] + public class ClientRequest + { + public ClientFunc Func; + public object Object; + public ClientRequest(ClientFunc func, object obj) { - public short Account { get; set; } - public string Password { get; set; } - public string[] SqlStr { get; set; } + Func = func; + Object = obj; } - [Serializable] - public class ServerSend + } + + [Serializable] + public class UserInfo + { + public short Account; + public string Password; + public Int16 Token; + public LvErr UserLev; + public UserInfo(short account, string password) // Changed { - public short Permission { get; set; } - public DataSet Ds { get; set; } - public bool SqlSucceed { get; set; } + Account = account; + Password = password; } } - #region Unused - - #region 题目信息 [Serializable] public class CourseInfo { @@ -39,105 +66,43 @@ namespace StuMgmLib.MyNameSpace public string Name; public string Content; } - #endregion - #region 学生题目信息一览 + [Serializable] public enum CourseStatusEnum { - undo, // 未开始 - doing, // 进行中 - waiting, // 等待验收 - failed, // 验收失败 - preSharing, // 准备分享 - pass // 验收通过 + //lll, + //...// } [Serializable] - public class CourseStatus + public class UserCourseInfo { - public short CourseId; - public CourseStatusEnum Status; + public short JobId;//工号 + public string Name;//姓名 + public string Status; + // ..public DateTime Entry; + public string Details;//信息描述 } - [Serializable] - public class UserInfo - { - public short JobId; - public string Name; - public List CourseStatus; - } - #endregion - - #region 学生单题详细信息 - //public class HistoryInfo - //{ - // DateTime Time; - // string Describe; - //} - //public class DetailInfo - //{ - // public short CourseId; - // public short JobId; - // List Describes; - //} #endregion - #region 未使用 - - class Server + #region ServerClass + [Serializable] + public class ServerResponse { - //List GetCourseInfo(); - //UserInfo GetUserInfo(short jobId); - //DetailInfo GetDetailInfo(short jobId, short courseId); - - public byte[] GetUser(short jobId) + public LvErr Lev; + public int Token; + //public string CourseStatus; + public object Object; + public ServerResponse(object obj) { - UserInfo info = new UserInfo(); - info.JobId = 111; - info.Name = "aaaa"; - info.CourseStatus = new List(); - CourseStatus aa = new CourseStatus(); - aa.CourseId = 222; - aa.Status = CourseStatusEnum.undo; // 做题状态 - info.CourseStatus.Add(aa); - - - return BinaryED.Serialize(info); - + Object = obj; } - - public UserInfo Parse(byte[] bt) - { - return BinaryED.Deserialize(bt); - //MemoryStream ms = new MemoryStream(bt); - //BinaryFormatter iFormatter = new BinaryFormatter(); - //UserInfo obj = (UserInfo)iFormatter.Deserialize(ms); - //return obj; - } - - - } + #endregion - class StudentInfo - { - //List GetCourseInfo(); - //UserInfo GetUserInfo(short jobId); - //DetailInfo GetDetailInfo(short jobId, short courseId); - } - class Program - { - //static void Main(string[] args) - //{ - // Server s = new Server(); - // byte[] buf = s.GetUser(0); - // UserInfo aa = s.Parse(buf); - //} - } - #endregion +} - #endregion -} diff --git a/StuMgmServer/StuMgmLib/MyNameSpace/Data.cs b/StuMgmServer/StuMgmLib/MyNameSpace/Data.cs new file mode 100644 index 0000000..c6b321b --- /dev/null +++ b/StuMgmServer/StuMgmLib/MyNameSpace/Data.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Data; + +namespace StuMgmLib.MyNameSpace +{ + + enum CommErr + { + Success, + FailSerial, + FailDeserial, + FailConnect, + FailSend, + FailReceive, + ErrData, + } + + public enum ClientFunc + { + VerifLogin = 1, + GetCourseInfo = 2, + GetUserCourseInfo = 3, + } + public enum LvErr + { + NotFound = -1, + Error = -2, + Admin = 1, + Teacher = 2, + Student = 3, + } + + [Serializable] + public class ClientRequest + { + public ClientFunc Func; + public object Object; + public ClientRequest(ClientFunc func, object obj) + { + Func = func; + Object = obj; + } + } + + [Serializable] + public class UserInfo + { + public string Account; + public string Password; + public string Token; + public LvErr UserRole; + public UserInfo(string account, string password) + { + Account = account; + Password = password; + } + } + + public class CourseInfo + { + public short Id; + public short Pid; + public int Time; + public string Name; + public string Content; + } + + + [Serializable] + public enum CourseStatusEnum + { + //lll, + //...// + } + + [Serializable] + public class UserCourseInfo + { + public short JobId;//工号 + public string Name;//姓名 +// ..public DateTime Entry; + public string InfoDescribe;//信息描述 + } +} \ No newline at end of file diff --git a/StuMgmServer/StuMgmLib/MyNameSpace/DataAnalyze.cs b/StuMgmServer/StuMgmLib/MyNameSpace/DataAnalyze.cs index 0b46ce1..abb192e 100644 --- a/StuMgmServer/StuMgmLib/MyNameSpace/DataAnalyze.cs +++ b/StuMgmServer/StuMgmLib/MyNameSpace/DataAnalyze.cs @@ -40,9 +40,9 @@ namespace StuMgmLib.MyNameSpace /// /// 解析ClientSend /// - public static Info.ServerSend ClientSendAnalyze(Info.ClientSend cs) + public static ServerSend ClientSendAnalyze(ClientSend cs) { - Info.ServerSend ss = new Info.ServerSend(); + ServerSend ss = new ServerSend(); ss.Permission = loginVerify(cs.Account, cs.Password); // 验证身份 if (ss.Permission < 0) // 小于0,则权限有误 { diff --git a/StuMgmServer/StuMgmLib/MyNameSpace/SystemComm.cs b/StuMgmServer/StuMgmLib/MyNameSpace/SystemComm.cs new file mode 100644 index 0000000..35e833b --- /dev/null +++ b/StuMgmServer/StuMgmLib/MyNameSpace/SystemComm.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Net; +using System.Net.Sockets; + +namespace StuMgmLib.MyNameSpace +{ + class SystemComm + { + #region 基本函数 + const int bufSize = 1024 * 1024; + static IPEndPoint m_ipEndpoint; + static Socket m_socket; + static byte[] m_buf; + + internal static void Init(string ip, int port) + { + IPAddress ipAdress = IPAddress.Parse(ip); + m_ipEndpoint = new IPEndPoint(ipAdress, port); + m_buf = new byte[bufSize]; + } + static bool Connect() + { + try + { + m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + m_socket.Connect(m_ipEndpoint); + return m_socket.Connected; + } + catch (Exception ex) + { + Debug.Print(ex.Message); + return false; + } + } + static bool Send(byte[] data) + { + try + { + m_socket.Send(data); + return true; + } + catch(Exception e) + { + Debug.Print(e.Message); + return false; + } + } + static bool Receive() + { + try + { + Array.Clear(m_buf, 0, m_buf.Length); + m_socket.Receive(m_buf); + return true; + } + catch (Exception e) + { + Debug.Print(e.Message); + return false; + } + } + static void DisConnect() + { + m_socket.Close(); + } + #endregion + + internal static CommErr GetData(ClientRequest req,out object o) + { + o = null; + + if (!Connect()) + return CommErr.FailConnect; + + byte[] sendBuf; + if (!Utility.BinSerialize(req, out sendBuf)) + return CommErr.FailSerial; + + if (!Send(sendBuf)) + return CommErr.FailSend; + + if (!Receive()) + return CommErr.FailReceive; + + DisConnect(); + + if (!Utility.BinDeserialize(m_buf, out o)) + return CommErr.FailDeserial; + return CommErr.Success; + } + } +} + diff --git a/StuMgmServer/StuMgmLib/MyNameSpace/SystemCtrl.cs b/StuMgmServer/StuMgmLib/MyNameSpace/SystemCtrl.cs new file mode 100644 index 0000000..fd1d931 --- /dev/null +++ b/StuMgmServer/StuMgmLib/MyNameSpace/SystemCtrl.cs @@ -0,0 +1,217 @@ +using MySql.Data.MySqlClient; +using System; +using System.Collections.Generic; +using System.Data; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; +using System.Text; + +namespace StuMgmLib.MyNameSpace +{ + class SystemCtrl + { + /// + /// 序列化 + /// + static byte[] Serialize(T c) + { + MemoryStream ms = new MemoryStream(); + BinaryFormatter iFormatter = new BinaryFormatter(); + iFormatter.Serialize(ms, c); + byte[] buf = ms.GetBuffer(); + return buf; + } + + /// + /// 反序列化 + /// + static T Deserialize(byte[] buf) + { + MemoryStream ms = new MemoryStream(buf); + BinaryFormatter iFormatter = new BinaryFormatter(); + var obj = (T)iFormatter.Deserialize(ms); + return obj; + } + + /// + /// 获取返回数据 + /// + public static byte[] GetServerResponse(byte[] clientRequset) + { + try + { + var cs = Deserialize(clientRequset); + + ServerResponse sr = new ServerResponse(null); + + switch (cs.Func) + { + case ClientFunc.VerifyLogin: + UserInfo ui = (UserInfo)cs.Object; + + if (verifyLogin(ui, out sr.Lev)) + sr.Token = addToken(ui.Account, ref Info.myToken); + break; + + case ClientFunc.GetCourseInfo: + sr.Object = getCourseInfo(); + Debug.Print(sr.Object.GetType().ToString()); + break; + + case ClientFunc.GetUserCourseInfo: + short job_id; + UserCourseInfo uc = new UserCourseInfo(); + if (verifyToken((Dictionary)cs.Object, out job_id)) + if (getUserCourseStatus(job_id, out uc.Status, out uc.Details)) + sr.Object = uc; + break; + + } + + return Serialize(sr); + } + catch + { + return null; + } + } + + const string conStr = "data source=localhost; initial catalog=xinje; user id=root; pwd=980505;charset = utf8"; + + static bool verifyLogin(UserInfo userInfo, out LvErr level) + { + level = LvErr.Error; + string qStu = "select * from user where account = " + userInfo.Account + " and password = '" + userInfo.Password + "'"; + MySqlConnection con = new MySqlConnection(conStr); + try + { + con.Open(); + MySqlCommand mCmd = new MySqlCommand(qStu, con); + MySqlDataReader mReader = mCmd.ExecuteReader(); + if (mReader.HasRows) + { + mReader.Read(); + level = (LvErr)mReader.GetInt16("level"); + return true; + } + else + { + level = LvErr.NotFound; + return false; + } + } + catch (MySqlException) + { + level = LvErr.Error; + return false; + } + finally + { + con.Close(); + } + } + + static int addToken(short job_id, ref Dictionary myToken) + { + if (myToken != null) + { + if (myToken.Count > Info.tokenMaxCount) // 清除缓存 + myToken.Clear(); + if (myToken.ContainsKey(job_id)) + { + myToken.Remove(job_id); + } + } + Random r = new Random(); // 伪随机 + int token = r.Next(); + myToken.Add(job_id, token); + return token; + } + + static bool verifyToken(Dictionary dic, out short account) + { + account = 0; + if (dic.Count != 1) + return false; + + foreach (var item in dic) + { + if (!Info.myToken.ContainsKey(item.Key)) + return false; + if (Info.myToken[item.Key] == item.Value) + { + account = item.Key; + return true; + } + } + return false; + } + + const int nameColumn = 1; + const int contentColumn = 3; + static List getCourseInfo() + { + List listCI = new List(); + string str = "select * from course_info"; + MySqlConnection con = new MySqlConnection(conStr); + try + { + con.Open(); + MySqlCommand mCmd = new MySqlCommand(str, con); + MySqlDataReader mReader = mCmd.ExecuteReader(); + while (mReader.Read()) + { + CourseInfo ci = new CourseInfo(); + ci.Id = mReader.GetInt16("id"); + ci.Pid = mReader.GetInt16("pid"); + ci.Time = mReader.GetInt16("time"); + ci.Name = (mReader.IsDBNull(nameColumn)) ? "无" : mReader.GetString("name"); + ci.Content = (mReader.IsDBNull(contentColumn)) ? "无" : mReader.GetString("content"); + listCI.Add(ci); + } + return listCI; + } + catch (MySqlException mySqlEx) + { + Debug.Print(DateTime.Now + " : " + mySqlEx.Message); + return null; + } + finally + { + con.Close(); + } + } + + static bool getUserCourseStatus(short jobId, out string status, out string details) + { + status = ""; + details = ""; + string qStu = "select * from usercouse_info where job_id = " + jobId; + MySqlConnection con = new MySqlConnection(conStr); + try + { + con.Open(); + MySqlCommand mCmd = new MySqlCommand(qStu, con); + MySqlDataReader mReader = mCmd.ExecuteReader(); + mReader.Read(); + status = (mReader.IsDBNull(nameColumn)) ? " " : mReader.GetString("status"); + details = (mReader.IsDBNull(nameColumn)) ? " " : mReader.GetString("details"); + return true; + } + catch (MySqlException) + { + return false; + } + finally + { + con.Close(); + } + } + + } + + +} diff --git a/StuMgmServer/StuMgmLib/MyNameSpace/SystemData.cs b/StuMgmServer/StuMgmLib/MyNameSpace/SystemData.cs new file mode 100644 index 0000000..fbcdd62 --- /dev/null +++ b/StuMgmServer/StuMgmLib/MyNameSpace/SystemData.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace StuMgmLib.MyNameSpace +{ + class SystemData + { + static Dictionary allCourseInfo; + static Dictionary allCourseStatus; + + + internal static CommErr InitSystemData() + { + CommErr err; + err = InitCourseInfo(); + if (err != CommErr.Success) + return err; + return CommErr.Success; + } + static CommErr InitCourseInfo() + { + CommErr err; + List courseInfo; + err = SystemCtrl.GetCourseInfo(out courseInfo); + if (err != CommErr.Success) + return err; + ...//将List转为Dictionary + return CommErr.Success; + } + + internal static CommErr RefreshUserCourseInfo(UserCourseInfo info) + { + ...//将UserCourseInfo转为Dictionary + return CommErr.Success; + } + + + } +} diff --git a/StuMgmServer/StuMgmLib/MyNameSpace/TcpConn.cs b/StuMgmServer/StuMgmLib/MyNameSpace/TcpConn.cs index f85efee..faeb9fe 100644 --- a/StuMgmServer/StuMgmLib/MyNameSpace/TcpConn.cs +++ b/StuMgmServer/StuMgmLib/MyNameSpace/TcpConn.cs @@ -4,6 +4,7 @@ * DateTime : 2021/1/18 */ using System; +using System.Diagnostics; using System.Net; using System.Net.Sockets; using System.Windows.Forms; @@ -13,7 +14,8 @@ namespace StuMgmLib.MyNameSpace // 还有一种验证连接方式: Token public class TcpConn { - private IPEndPoint IPP = null; + public EndPoint Ep; + private IPEndPoint ipp = null; private Socket socket = null; private Socket socketClient = null; @@ -44,9 +46,9 @@ namespace StuMgmLib.MyNameSpace #region 开启服务器 public void OpenServer(string ipAddr, int port) { - IPP = new IPEndPoint(IPAddress.Parse(ipAddr), port); + ipp = new IPEndPoint(IPAddress.Parse(ipAddr), port); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - socket.Bind(IPP); + socket.Bind(ipp); socket.Listen(0); SocketExist = true; } @@ -67,49 +69,44 @@ namespace StuMgmLib.MyNameSpace /// /// 接收客户端连接 /// - public string AcceptConn() + public void AcceptConn() { try { socketClient = socket.Accept(); // 阻塞等待客户端连接 - return DateTime.Now.ToLongTimeString() + " : " + socketClient.RemoteEndPoint.ToString() + " 已连接 \n"; + Ep = socketClient.RemoteEndPoint; } - catch (Exception) + catch (SocketException se) { - return null; + Debug.Print(se.Message); } } #endregion - const int recvTimeOut = 3000; // 设置接收超时时间 + const int recvTimeOut = -1; // 设置接收超时时间 const int recvLength = 65535; #region 接收数据 /// /// 接收数据 /// - public string AcpMsg() + public void AcpMsg() { - byte[] dataRecv = new byte[recvLength]; // 定义接收数组 - string reEdPoint = ""; + byte[] clientSend = new byte[recvLength]; // 定义接收数组 try { - reEdPoint = socketClient.RemoteEndPoint.ToString(); + socketClient.ReceiveTimeout = recvTimeOut; - socketClient.Receive(dataRecv); + socketClient.Receive(clientSend); + + byte[] serverSend = SystemCtrl.GetServerResponse(clientSend); - var cs = BinaryED.Deserialize(dataRecv); - Info.ServerSend ss = DataAnalyze.ClientSendAnalyze(cs); - byte[] dataSend = BinaryED.Serialize(ss); - socketClient.Send(dataSend); + if (serverSend != null) + socketClient.Send(serverSend); - return DateTime.Now.ToLongTimeString() + " : " + reEdPoint + " 断开连接 \n"; } - catch // 客户端断开连接 + catch (SocketException se) // 客户端断开连接 { - if (socketClient != null) - return DateTime.Now.ToLongTimeString() + " : " + reEdPoint + " 断开连接 \n"; - else - return null; + Debug.Print(se.Message); } finally { diff --git a/StuMgmServer/StuMgmLib/StuMgmLib.csproj b/StuMgmServer/StuMgmLib/StuMgmLib.csproj index 33fd607..80299fe 100644 --- a/StuMgmServer/StuMgmLib/StuMgmLib.csproj +++ b/StuMgmServer/StuMgmLib/StuMgmLib.csproj @@ -43,9 +43,10 @@ - - + + Code + diff --git a/StuMgmServer/StuMgmServer/Server.Designer.cs b/StuMgmServer/StuMgmServer/Server.Designer.cs index fd1bd0e..c1125de 100644 --- a/StuMgmServer/StuMgmServer/Server.Designer.cs +++ b/StuMgmServer/StuMgmServer/Server.Designer.cs @@ -191,7 +191,7 @@ // tmr // this.tmr.Enabled = true; - this.tmr.Tick += new System.EventHandler(this.tmr_Tick); + //this.tmr.Tick += new System.EventHandler(this.tmr_Tick); // // Server // diff --git a/StuMgmServer/StuMgmServer/Server.cs b/StuMgmServer/StuMgmServer/Server.cs index 6b6cbcc..7048cde 100644 --- a/StuMgmServer/StuMgmServer/Server.cs +++ b/StuMgmServer/StuMgmServer/Server.cs @@ -44,13 +44,17 @@ namespace StuMgmServer try { if (sFlag == true) + { tcpConn.CloseServer(); + btnSerSwitch.Text = "开启服务器"; + } else if (sFlag != true) { int port = Convert.ToInt16(txtPort.Text); tcpConn.OpenServer(cbxIPAddr.Text, port); tUpdateUi = new Thread(updateHistory); tUpdateUi.Start(); + btnSerSwitch.Text = "关闭服务器"; } } catch (Exception ep) @@ -66,21 +70,13 @@ namespace StuMgmServer { while (tcpConn.SocketExist) { - setText(tcpConn.AcceptConn()); - setText(tcpConn.AcpMsg()); + tcpConn.AcceptConn(); + setText(DateTime.Now.ToLongTimeString() + " : " + tcpConn.Ep.ToString() + " 建立连接 \n"); + tcpConn.AcpMsg(); + setText(DateTime.Now.ToLongTimeString() + " : " + tcpConn.Ep.ToString() + " 断开连接 \n"); } } - /// - /// 定时器更新btn开关服务器 - /// - private void tmr_Tick(object sender, EventArgs e) - { - if (tcpConn.SocketExist) - btnSerSwitch.Text = "关闭服务器"; - else - btnSerSwitch.Text = "开启服务器"; - } private void Server_Load(object sender, EventArgs e) { @@ -91,11 +87,6 @@ namespace StuMgmServer { //DialogResult dr = MessageBox.Show("确认退出程序?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); //if (dr != DialogResult.Yes) - //e.Cancel = true; - //tmr.Dispose(); - //if (tUpdateUi != null) - // tUpdateUi.Abort(); - //if (MessageBox.Show("确认退出程序?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) System.Environment.Exit(0); } diff --git a/StuMgmServer/Test/Data.cs b/StuMgmServer/Test/Data.cs new file mode 100644 index 0000000..1ca13f7 --- /dev/null +++ b/StuMgmServer/Test/Data.cs @@ -0,0 +1,201 @@ +using System; +using System.Collections.Generic; +using System.Data; + + + +namespace StuMgmClient +{ + + enum ErrCode + { + Success, + FailSerial, + FailDeserial, + FailConnect, + FailSend, + FailReceive, + ErrData, + } + + public enum FuncCode + { + VerifLogin = 1, + GetCourseInfo, + GetUserCourseInfo, + } + public enum Roles + { + NotFound = -1, + Error = -2, + Admin = 1, + Teacher = 2, + Student = 3, + } + + [Serializable] + public class ClientRequest + { + public FuncCode Func; + public object Object; + public ClientRequest(FuncCode func, object obj) + { + Func = func; + Object = obj; + } + } + + [Serializable] + public class UserInfo + { + public short Account; // Changed + public string Password; + public Int16 Token; + public Roles UserRole; + public UserInfo(short account, string password) // Changed + { + Account = account; + Password = password; + } + } + + public class CourseInfo + { + public short Id; + public short Pid; + public int Time; + public string Name; + public string Content; + } + + + [Serializable] + public enum CourseStatusEnum + { + lll, + ...// + } + + [Serializable] + public class UserCourseInfo + { + public short JobId;//工号 + public string Name;//姓名 + ..public DateTime Entry; + public string InfoDescribe;//信息描述 + } + + + + + /* + + + #region 学生题目信息一览 + + #endregion + + #region 学生单题详细信息 + public class HistoryInfo + { + DateTime Time; + string Describe; + } + public class DetailInfo + { + public short CourseId; + public short JobId; + List Describes; + } + #endregion + + */ + + + + + + + + //[Serializable] + //public class ServerSend + //{ + // public short permission { get; set; } + // public DataSet ds { get; set; } + //} + + +} + + +namespace StuMgmClient +{ + + //class Data + //{ + // //状态值字符串转字典 + // public Dictionary StateParsing(DataTable table) + // { + // Dictionary myDictionary = new Dictionary(); + // ClientMysql cm = new ClientMysql(); + // DataSet ds = cm.SelectState(); + // DataRow dr = ds.Tables["user_info"].Rows[0]; + // string state = dr["course_status"].ToString(); + // int num = 0; + // int oldTem = 0; + // //切割字符串 + // string[] sArray = state.Split(new char[2] { ':', ';' }); + // foreach (string i in sArray) + // { + // if (i.Equals("")) { break; } + // int tem = Convert.ToInt32(i); + // num++; + // if (num % 2 != 0) + // { + // myDictionary.Add(tem, 0); + // oldTem = tem; + // } + // else + // { + // myDictionary[oldTem] = tem; + // } + // } + // return myDictionary; + // } + // //字典转字符串 + // public string DicParsing(Dictionary dic) + // { + // string stateText = ""; + // foreach (var item in dic) + // { + // stateText = stateText + item.Key + ":" + item.Value + ";"; + // } + // return stateText; + // } + // //查询所有子节点 + // public void GetAllNodes(string id, DataTable table, ref Dictionary nodesDic) + // { + + // //把父节点的数据筛选出来 + // DataRow[] rows = table.Select("pid =" + id);//取根 + // if (rows.Length <= 0) + // { + // nodesDic.Add(Convert.ToInt32(id), 0); + // return; + // } + // foreach (DataRow dr in rows) + // { + // GetAllNodes(dr["id"].ToString(), table, ref nodesDic); + // } + + // } + // //获取所有子节点Dictionary + // public Dictionary GetNodesDic(DataTable table) + // { + // Dictionary nodesDic = new Dictionary(); + // GetAllNodes("0", table, ref nodesDic); + // return nodesDic; + // } + //} + +} diff --git a/StuMgmServer/Test/SystemComm.cs b/StuMgmServer/Test/SystemComm.cs new file mode 100644 index 0000000..3041005 --- /dev/null +++ b/StuMgmServer/Test/SystemComm.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Net; +using System.Net.Sockets; + +namespace StuMgmClient +{ + class SystemComm + { + #region 基本函数 + const int bufSize = 1024 * 1024; + static IPEndPoint m_ipEndpoint; + static Socket m_socket; + static byte[] m_buf; + + internal static void Init(string ip, int port) + { + IPAddress ipAdress = IPAddress.Parse(ip); + m_ipEndpoint = new IPEndPoint(ipAdress, port); + m_buf = new byte[bufSize]; + } + static bool Connect() + { + try + { + m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + m_socket.Connect(m_ipEndpoint); + return m_socket.Connected; + } + catch (Exception ex) + { + Debug.Print(ex.Message); + return false; + } + } + static bool Send(byte[] data) + { + try + { + m_socket.Send(data); + return true; + } + catch(Exception e) + { + Debug.Print(e.Message); + return false; + } + } + static bool Receive() + { + try + { + Array.Clear(m_buf, 0, m_buf.Length); + m_socket.Receive(m_buf); + return true; + } + catch (Exception e) + { + Debug.Print(e.Message); + return false; + } + } + static void DisConnect() + { + m_socket.Close(); + } + #endregion + + internal static ErrCode GetData(ClientRequest req,out object o) + { + o = null; + + if (!Connect()) + return ErrCode.FailConnect; + + byte[] sendBuf; + if (!Utility.BinSerialize(req, out sendBuf)) + return ErrCode.FailSerial; + + if (!Send(sendBuf)) + return ErrCode.FailSend; + + if (!Receive()) + return ErrCode.FailReceive; + + DisConnect(); + + if (!Utility.BinDeserialize(m_buf, out o)) + return ErrCode.FailDeserial; + return ErrCode.Success; + } + } +} + diff --git a/StuMgmServer/Test/SystemCtrl.cs b/StuMgmServer/Test/SystemCtrl.cs new file mode 100644 index 0000000..32a2be8 --- /dev/null +++ b/StuMgmServer/Test/SystemCtrl.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace StuMgmClient +{ + class SystemCtrl + { + internal static ErrCode VerifLogin(string userName, string pawssword, out Roles role, out string token) + { + role = Roles.Error; + token = string.Empty; + + UserInfo cs = new UserInfo(userName, pawssword); + ClientRequest req = new ClientRequest(FuncCode.VerifLogin, cs); + + object o; + ErrCode err = SystemComm.GetData(req, out o); + if (err != ErrCode.Success) + return err; + + if (!(o is UserInfo)) + return ErrCode.ErrData; + + role = ((UserInfo)o).UserRole; + token = ((UserInfo)o).Token; + return ErrCode.Success; + } + + internal static ErrCode GetCourseInfo(out List courseInfo) + { + courseInfo = null; + //****** + ClientRequest req = new ClientRequest(FuncCode.GetCourseInfo, null); + + object o; + ErrCode err = SystemComm.GetData(req, out o); + if (err != ErrCode.Success) + return err; + + if (!(o is List)) + return ErrCode.ErrData; + + courseInfo = (List)o; + return ErrCode.Success; + } + + internal static ErrCode RefreshUserCourseInfo(string token) + { + UserCourseInfo info; + ErrCode err; + + err = GetUserCourseInfo(token, out info); + if (err != ErrCode.Success) + return err; + + err = SystemData.RefreshUserCourseInfo(info); + if (err != ErrCode.Success) + return err; + + return ErrCode.Success; + } + + static ErrCode GetUserCourseInfo(string token ,out UserCourseInfo info) + { + info = null; + ClientRequest req = new ClientRequest(FuncCode.GetUserCourseInfo, token); + + object o; + ErrCode err = SystemComm.GetData(req, out o); + if (err != ErrCode.Success) + return err; + + if (!(o is UserCourseInfo)) + return ErrCode.ErrData; + + info = (UserCourseInfo)o; + return ErrCode.Success; + } + + + + + + + + } +} diff --git a/StuMgmServer/Test/SystemData.cs b/StuMgmServer/Test/SystemData.cs new file mode 100644 index 0000000..7818bc5 --- /dev/null +++ b/StuMgmServer/Test/SystemData.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace StuMgmClient +{ + class SystemData + { + static Dictionary allCourseInfo; + static Dictionary allCourseStatus; + + + internal static ErrCode InitSystemData() + { + ErrCode err; + err = InitCourseInfo(); + if (err != ErrCode.Success) + return err; + return ErrCode.Success; + } + static ErrCode InitCourseInfo() + { + ErrCode err; + List courseInfo; + err = SystemCtrl.GetCourseInfo(out courseInfo); + if (err != ErrCode.Success) + return err; + ...//将List转为Dictionary + return ErrCode.Success; + } + + internal static ErrCode RefreshUserCourseInfo(UserCourseInfo info) + { + ...//将UserCourseInfo转为Dictionary + return ErrCode.Success; + } + + + } +} diff --git a/StuMgmServer/Test/Test.csproj b/StuMgmServer/Test/Test.csproj index 13113c7..00f25e9 100644 --- a/StuMgmServer/Test/Test.csproj +++ b/StuMgmServer/Test/Test.csproj @@ -40,6 +40,7 @@ + Form @@ -48,6 +49,9 @@ + + + Form1.cs