/// <summary> /// 发送指令到客户端。 /// </summary> /// <param name="data"></param> /// <param name="clientIP"></param> protected bool SendOrderToClient(Msg data, string clientIP) { if (!string.IsNullOrEmpty(clientIP) && (data != null) && (this.ports != null)) { return this.SendData(data, clientIP, this.ports.HostOrder); } return false; }
/// <summary> /// 接收客户端消息数据。 /// </summary> /// <param name="data"></param> protected virtual void ReceiveClientData(Msg data) { if (data == null) return; this.RaiseChanged("监听:" + data.ToString()); switch (data.Kind) { case MSGKind.ClientClose://学生机下线。 { ClientClose cc = data as ClientClose; if (cc != null) { StudentEx student = Program.STUDENTS[cc.StudentID]; if (student != null) { if ((student.Status & StudentControl.EnumStudentState.Online) == StudentControl.EnumStudentState.Online) { student.Status &= ~StudentControl.EnumStudentState.Online; } student.Status |= StudentControl.EnumStudentState.Offline; this.RaiseChanged(string.Format("学生机:({0}){1}[{2}] 关闭客户端!", student.MachineName, student.StudentName, student.IP)); this.OnUpdateControls(student); } } } break; case MSGKind.ReqLogin://请求登录。 { ReqLogin req = data as ReqLogin; if (req != null) { this.RaiseChanged(string.Format("学生机IP:{0},{1:HH-mm-ss} 请求登录!", req.UIP, req.Time)); RespLogin resp = new RespLogin(); resp.Method = this.sci.LoginMethod; resp.Students = this.sci.ClassInfo.Students; resp.Catalog = this.sci.CatalogInfo; resp.Time = DateTime.Now; resp.UID = this.userInfo.UserID; bool result = this.SendOrderToClient(resp, req.UIP); this.RaiseChanged(string.Format("学生机IP:{0},{1:HH-mm-ss} 请求登录,{2}!", req.UIP, req.Time, result ? "成功" : "失败")); } } break; case MSGKind.Logining://开始登录。 { StartLogin s = data as StartLogin; if (s != null) { object o = STU_Login_Cache[s.UserAccount]; if (o == null) { STU_Login_Cache[s.UserAccount] = s.UserAccount; this.VerifyLogin(s); } else { EndLogin end = new EndLogin(); end.UID = this.userInfo.UserID; end.Time = DateTime.Now; end.Result = false; end.Error = string.Format("学生[{0}]正在登录中,请稍后...", s.UserAccount); this.SendOrderToClient(end, s.UIP); } } } break; } }
/// <summary> /// 发送消息。 /// </summary> /// <param name="data"></param> /// <param name="clientIP"></param> /// <param name="port"></param> /// <returns></returns> public bool SendData(Msg data, string clientIP, int port) { lock (this) { bool result = false; if (!string.IsNullOrEmpty(clientIP) && (data != null)) { IPEndPoint remote = new IPEndPoint(IPAddress.Parse(clientIP), port); byte[] buf = this.Serialize(data); using (UdpClient udp = new UdpClient()) { return udp.Send(buf, buf.Length, remote) > 0; } } return result; } }
/// <summary> /// 序列化消息。 /// </summary> /// <param name="data"></param> /// <returns></returns> protected byte[] Serialize(Msg data) { lock (this) { byte[] buf = null; if (data != null) { using (MemoryStream ms = new MemoryStream()) { this.binaryFormatter.Serialize(ms, data); buf = ms.ToArray(); } } return buf; } }
/// <summary> /// 发送数据。 /// </summary> /// <param name="netWorkStream"></param> /// <param name="data"></param> /// <param name="message"></param> protected virtual void SendData(NetworkStream netWorkStream, Msg data, string message) { if (netWorkStream != null && data != null) { byte[] buf = this.Serialize(data); if (buf != null && buf.Length > 0) { netWorkStream.Write(buf, 0, buf.Length); this.RaiseChanged(string.Format("{0}【{1}...】", data.Time, message)); } } }
/// <summary> /// 用广播端口发送消息。 /// </summary> /// <param name="data"></param> /// <param name="ip"></param> public bool SendBroadcastPortMSG(Msg data, string ip) { bool result = false; if (data != null && !string.IsNullOrEmpty(ip)) { data.Time = DateTime.Now; result = this.hostListenService.SendData(data, ip, this.ports.HostBroadcast); } return result; }