@@ -11,25 +11,52 @@ namespace StuMgmLib.MyNameSpace | |||||
{ | { | ||||
public class Info | public class Info | ||||
{ | { | ||||
[Serializable] | |||||
public class ClientSend | |||||
internal const Int16 tokenMaxCount = 32767; | |||||
internal static Dictionary<short, int> myToken = new Dictionary<short, int>(); | |||||
} | |||||
#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] | [Serializable] | ||||
public class CourseInfo | public class CourseInfo | ||||
{ | { | ||||
@@ -39,105 +66,43 @@ namespace StuMgmLib.MyNameSpace | |||||
public string Name; | public string Name; | ||||
public string Content; | public string Content; | ||||
} | } | ||||
#endregion | |||||
#region 学生题目信息一览 | |||||
[Serializable] | |||||
public enum CourseStatusEnum | public enum CourseStatusEnum | ||||
{ | { | ||||
undo, // 未开始 | |||||
doing, // 进行中 | |||||
waiting, // 等待验收 | |||||
failed, // 验收失败 | |||||
preSharing, // 准备分享 | |||||
pass // 验收通过 | |||||
//lll, | |||||
//...// | |||||
} | } | ||||
[Serializable] | [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> CourseStatus; | |||||
} | |||||
#endregion | |||||
#region 学生单题详细信息 | |||||
//public class HistoryInfo | |||||
//{ | |||||
// DateTime Time; | |||||
// string Describe; | |||||
//} | |||||
//public class DetailInfo | |||||
//{ | |||||
// public short CourseId; | |||||
// public short JobId; | |||||
// List<HistoryInfo> Describes; | |||||
//} | |||||
#endregion | #endregion | ||||
#region 未使用 | |||||
class Server | |||||
#region ServerClass | |||||
[Serializable] | |||||
public class ServerResponse | |||||
{ | { | ||||
//List<CourseInfo> 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>(); | |||||
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<UserInfo>(bt); | |||||
//MemoryStream ms = new MemoryStream(bt); | |||||
//BinaryFormatter iFormatter = new BinaryFormatter(); | |||||
//UserInfo obj = (UserInfo)iFormatter.Deserialize(ms); | |||||
//return obj; | |||||
} | |||||
} | } | ||||
#endregion | |||||
class StudentInfo | |||||
{ | |||||
//List<CourseInfo> 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 | |||||
} | |||||
@@ -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;//信息描述 | |||||
} | |||||
} |
@@ -40,9 +40,9 @@ namespace StuMgmLib.MyNameSpace | |||||
/// <summary> | /// <summary> | ||||
/// 解析ClientSend | /// 解析ClientSend | ||||
/// </summary> | /// </summary> | ||||
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); // 验证身份 | ss.Permission = loginVerify(cs.Account, cs.Password); // 验证身份 | ||||
if (ss.Permission < 0) // 小于0,则权限有误 | if (ss.Permission < 0) // 小于0,则权限有误 | ||||
{ | { | ||||
@@ -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; | |||||
} | |||||
} | |||||
} | |||||
@@ -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 | |||||
{ | |||||
/// <summary> | |||||
/// 序列化 | |||||
/// </summary> | |||||
static byte[] Serialize<T>(T c) | |||||
{ | |||||
MemoryStream ms = new MemoryStream(); | |||||
BinaryFormatter iFormatter = new BinaryFormatter(); | |||||
iFormatter.Serialize(ms, c); | |||||
byte[] buf = ms.GetBuffer(); | |||||
return buf; | |||||
} | |||||
/// <summary> | |||||
/// 反序列化 | |||||
/// </summary> | |||||
static T Deserialize<T>(byte[] buf) | |||||
{ | |||||
MemoryStream ms = new MemoryStream(buf); | |||||
BinaryFormatter iFormatter = new BinaryFormatter(); | |||||
var obj = (T)iFormatter.Deserialize(ms); | |||||
return obj; | |||||
} | |||||
/// <summary> | |||||
/// 获取返回数据 | |||||
/// </summary> | |||||
public static byte[] GetServerResponse(byte[] clientRequset) | |||||
{ | |||||
try | |||||
{ | |||||
var cs = Deserialize<ClientRequest>(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<short, int>)cs.Object, out job_id)) | |||||
if (getUserCourseStatus(job_id, out uc.Status, out uc.Details)) | |||||
sr.Object = uc; | |||||
break; | |||||
} | |||||
return Serialize<ServerResponse>(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<short, int> 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<short, int> 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<CourseInfo> getCourseInfo() | |||||
{ | |||||
List<CourseInfo> listCI = new List<CourseInfo>(); | |||||
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(); | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,40 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
namespace StuMgmLib.MyNameSpace | |||||
{ | |||||
class SystemData | |||||
{ | |||||
static Dictionary<short, CourseInfo> allCourseInfo; | |||||
static Dictionary<short,CourseStatusEnum> 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> courseInfo; | |||||
err = SystemCtrl.GetCourseInfo(out courseInfo); | |||||
if (err != CommErr.Success) | |||||
return err; | |||||
...//将List<CourseInfo>转为Dictionary<short, CourseInfo> | |||||
return CommErr.Success; | |||||
} | |||||
internal static CommErr RefreshUserCourseInfo(UserCourseInfo info) | |||||
{ | |||||
...//将UserCourseInfo转为Dictionary<short,CourseStatusEnum> | |||||
return CommErr.Success; | |||||
} | |||||
} | |||||
} |
@@ -4,6 +4,7 @@ | |||||
* DateTime : 2021/1/18 | * DateTime : 2021/1/18 | ||||
*/ | */ | ||||
using System; | using System; | ||||
using System.Diagnostics; | |||||
using System.Net; | using System.Net; | ||||
using System.Net.Sockets; | using System.Net.Sockets; | ||||
using System.Windows.Forms; | using System.Windows.Forms; | ||||
@@ -13,7 +14,8 @@ namespace StuMgmLib.MyNameSpace | |||||
// 还有一种验证连接方式: Token | // 还有一种验证连接方式: Token | ||||
public class TcpConn | public class TcpConn | ||||
{ | { | ||||
private IPEndPoint IPP = null; | |||||
public EndPoint Ep; | |||||
private IPEndPoint ipp = null; | |||||
private Socket socket = null; | private Socket socket = null; | ||||
private Socket socketClient = null; | private Socket socketClient = null; | ||||
@@ -44,9 +46,9 @@ namespace StuMgmLib.MyNameSpace | |||||
#region 开启服务器 | #region 开启服务器 | ||||
public void OpenServer(string ipAddr, int port) | 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 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | ||||
socket.Bind(IPP); | |||||
socket.Bind(ipp); | |||||
socket.Listen(0); | socket.Listen(0); | ||||
SocketExist = true; | SocketExist = true; | ||||
} | } | ||||
@@ -67,49 +69,44 @@ namespace StuMgmLib.MyNameSpace | |||||
/// <summary> | /// <summary> | ||||
/// 接收客户端连接 | /// 接收客户端连接 | ||||
/// </summary> | /// </summary> | ||||
public string AcceptConn() | |||||
public void AcceptConn() | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
socketClient = socket.Accept(); // 阻塞等待客户端连接 | 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 | #endregion | ||||
const int recvTimeOut = 3000; // 设置接收超时时间 | |||||
const int recvTimeOut = -1; // 设置接收超时时间 | |||||
const int recvLength = 65535; | const int recvLength = 65535; | ||||
#region 接收数据 | #region 接收数据 | ||||
/// <summary> | /// <summary> | ||||
/// 接收数据 | /// 接收数据 | ||||
/// </summary> | /// </summary> | ||||
public string AcpMsg() | |||||
public void AcpMsg() | |||||
{ | { | ||||
byte[] dataRecv = new byte[recvLength]; // 定义接收数组 | |||||
string reEdPoint = ""; | |||||
byte[] clientSend = new byte[recvLength]; // 定义接收数组 | |||||
try | try | ||||
{ | { | ||||
reEdPoint = socketClient.RemoteEndPoint.ToString(); | |||||
socketClient.ReceiveTimeout = recvTimeOut; | socketClient.ReceiveTimeout = recvTimeOut; | ||||
socketClient.Receive(dataRecv); | |||||
socketClient.Receive(clientSend); | |||||
byte[] serverSend = SystemCtrl.GetServerResponse(clientSend); | |||||
var cs = BinaryED.Deserialize<Info.ClientSend>(dataRecv); | |||||
Info.ServerSend ss = DataAnalyze.ClientSendAnalyze(cs); | |||||
byte[] dataSend = BinaryED.Serialize<Info.ServerSend>(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 | finally | ||||
{ | { | ||||
@@ -43,9 +43,10 @@ | |||||
<Reference Include="System.Xml" /> | <Reference Include="System.Xml" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<Compile Include="MyNameSpace\BinaryED.cs" /> | |||||
<Compile Include="MyNameSpace\DataAnalyze.cs" /> | |||||
<Compile Include="MyNameSpace\CommonData.cs" /> | <Compile Include="MyNameSpace\CommonData.cs" /> | ||||
<Compile Include="MyNameSpace\SystemCtrl.cs"> | |||||
<SubType>Code</SubType> | |||||
</Compile> | |||||
<Compile Include="MyNameSpace\TcpConn.cs" /> | <Compile Include="MyNameSpace\TcpConn.cs" /> | ||||
<Compile Include="Properties\AssemblyInfo.cs" /> | <Compile Include="Properties\AssemblyInfo.cs" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
@@ -191,7 +191,7 @@ | |||||
// tmr | // tmr | ||||
// | // | ||||
this.tmr.Enabled = true; | this.tmr.Enabled = true; | ||||
this.tmr.Tick += new System.EventHandler(this.tmr_Tick); | |||||
//this.tmr.Tick += new System.EventHandler(this.tmr_Tick); | |||||
// | // | ||||
// Server | // Server | ||||
// | // | ||||
@@ -44,13 +44,17 @@ namespace StuMgmServer | |||||
try | try | ||||
{ | { | ||||
if (sFlag == true) | if (sFlag == true) | ||||
{ | |||||
tcpConn.CloseServer(); | tcpConn.CloseServer(); | ||||
btnSerSwitch.Text = "开启服务器"; | |||||
} | |||||
else if (sFlag != true) | else if (sFlag != true) | ||||
{ | { | ||||
int port = Convert.ToInt16(txtPort.Text); | int port = Convert.ToInt16(txtPort.Text); | ||||
tcpConn.OpenServer(cbxIPAddr.Text, port); | tcpConn.OpenServer(cbxIPAddr.Text, port); | ||||
tUpdateUi = new Thread(updateHistory); | tUpdateUi = new Thread(updateHistory); | ||||
tUpdateUi.Start(); | tUpdateUi.Start(); | ||||
btnSerSwitch.Text = "关闭服务器"; | |||||
} | } | ||||
} | } | ||||
catch (Exception ep) | catch (Exception ep) | ||||
@@ -66,21 +70,13 @@ namespace StuMgmServer | |||||
{ | { | ||||
while (tcpConn.SocketExist) | 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"); | |||||
} | } | ||||
} | } | ||||
/// <summary> | |||||
/// 定时器更新btn开关服务器 | |||||
/// </summary> | |||||
private void tmr_Tick(object sender, EventArgs e) | |||||
{ | |||||
if (tcpConn.SocketExist) | |||||
btnSerSwitch.Text = "关闭服务器"; | |||||
else | |||||
btnSerSwitch.Text = "开启服务器"; | |||||
} | |||||
private void Server_Load(object sender, EventArgs e) | private void Server_Load(object sender, EventArgs e) | ||||
{ | { | ||||
@@ -91,11 +87,6 @@ namespace StuMgmServer | |||||
{ | { | ||||
//DialogResult dr = MessageBox.Show("确认退出程序?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); | //DialogResult dr = MessageBox.Show("确认退出程序?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); | ||||
//if (dr != DialogResult.Yes) | //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); | System.Environment.Exit(0); | ||||
} | } | ||||
@@ -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<HistoryInfo> Describes; | |||||
} | |||||
#endregion | |||||
*/ | |||||
//[Serializable] | |||||
//public class ServerSend | |||||
//{ | |||||
// public short permission { get; set; } | |||||
// public DataSet ds { get; set; } | |||||
//} | |||||
} | |||||
namespace StuMgmClient | |||||
{ | |||||
//class Data | |||||
//{ | |||||
// //状态值字符串转字典 | |||||
// public Dictionary<int, int> StateParsing(DataTable table) | |||||
// { | |||||
// Dictionary<int, int> myDictionary = new Dictionary<int, int>(); | |||||
// 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<int, int> 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<int, int> 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<int, int> GetNodesDic(DataTable table) | |||||
// { | |||||
// Dictionary<int, int> nodesDic = new Dictionary<int, int>(); | |||||
// GetAllNodes("0", table, ref nodesDic); | |||||
// return nodesDic; | |||||
// } | |||||
//} | |||||
} |
@@ -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; | |||||
} | |||||
} | |||||
} | |||||
@@ -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) | |||||
{ | |||||
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<CourseInfo>)) | |||||
return ErrCode.ErrData; | |||||
courseInfo = (List<CourseInfo>)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; | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,40 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
namespace StuMgmClient | |||||
{ | |||||
class SystemData | |||||
{ | |||||
static Dictionary<short, CourseInfo> allCourseInfo; | |||||
static Dictionary<short,CourseStatusEnum> 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> courseInfo; | |||||
err = SystemCtrl.GetCourseInfo(out courseInfo); | |||||
if (err != ErrCode.Success) | |||||
return err; | |||||
...//将List<CourseInfo>转为Dictionary<short, CourseInfo> | |||||
return ErrCode.Success; | |||||
} | |||||
internal static ErrCode RefreshUserCourseInfo(UserCourseInfo info) | |||||
{ | |||||
...//将UserCourseInfo转为Dictionary<short,CourseStatusEnum> | |||||
return ErrCode.Success; | |||||
} | |||||
} | |||||
} |
@@ -40,6 +40,7 @@ | |||||
<Reference Include="System.Xml" /> | <Reference Include="System.Xml" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<Compile Include="Data.cs" /> | |||||
<Compile Include="Form1.cs"> | <Compile Include="Form1.cs"> | ||||
<SubType>Form</SubType> | <SubType>Form</SubType> | ||||
</Compile> | </Compile> | ||||
@@ -48,6 +49,9 @@ | |||||
</Compile> | </Compile> | ||||
<Compile Include="Program.cs" /> | <Compile Include="Program.cs" /> | ||||
<Compile Include="Properties\AssemblyInfo.cs" /> | <Compile Include="Properties\AssemblyInfo.cs" /> | ||||
<Compile Include="SystemComm.cs" /> | |||||
<Compile Include="SystemCtrl.cs" /> | |||||
<Compile Include="SystemData.cs" /> | |||||
<EmbeddedResource Include="Form1.resx"> | <EmbeddedResource Include="Form1.resx"> | ||||
<DependentUpon>Form1.cs</DependentUpon> | <DependentUpon>Form1.cs</DependentUpon> | ||||
</EmbeddedResource> | </EmbeddedResource> | ||||