/// <summary> /// 服务端收到数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AsyncNetUdpServer_UdpPacketArrived(object sender, UdpPacketArrivedEventArgs e) { String dataJson = AesEncrypt.Decrypt(Program.AppSettings.ControlKey, e.PacketData).TrimEnd('\0'); Program.Logger.Log("UdpSocketServer", $"UdpPacketArrived=>{e.RemoteEndPoint.Address}:{e.RemoteEndPoint.Port}=>{dataJson}"); if (String.IsNullOrWhiteSpace(dataJson) || dataJson.IndexOf("ActionId") < 0 || dataJson.IndexOf("ActionName") < 0) { this.AsyncNetUdpServer.Post($"UnknownDataPacket,Logged\"{e.RemoteEndPoint.Address}:{e.RemoteEndPoint.Port}\"".GetBytes_Utf8(), e.RemoteEndPoint); return; } Entity.UdpSocketPacketRecive packet = null; try { packet = JsonConvert.DeserializeObject <Entity.UdpSocketPacketRecive>(dataJson); } catch {} if (packet == null) { this.AsyncNetUdpServer.Post($"UnknownDataPacket,Logged\"{e.RemoteEndPoint.Address}:{e.RemoteEndPoint.Port}\"".GetBytes_Utf8(), e.RemoteEndPoint); return; } switch (packet.ActionId) { case 1: this.PacketAction1(e.RemoteEndPoint); break; case 2: this.PacketAction2(e.RemoteEndPoint); break; case 1001: this.PacketAction1001(e.RemoteEndPoint); break; case 1002: this.PacketAction1002(e.RemoteEndPoint); break; case 1003: this.PacketAction1003(e.RemoteEndPoint, packet); break; case 1004: this.PacketAction1004(e.RemoteEndPoint); break; case 1005: this.PacketAction1005(e.RemoteEndPoint, packet); break; } }
/// <summary> /// 数据处理 StopUnit /// </summary> /// <param name="_IPEndPoint"></param> /// <param name="_UdpSocketPacketRecive"></param> private void PacketAction1005(IPEndPoint _IPEndPoint, Entity.UdpSocketPacketRecive _UdpSocketPacketRecive) { if (!this.IncludeRemote(_IPEndPoint)) { return; } else { this.UpdateRemote(_IPEndPoint); } Object packet = new{ ActionId = 1005, ActionName = "StopUnit", ErrorCode = 0, ErrorMessage = String.Empty }; if (String.IsNullOrWhiteSpace(_UdpSocketPacketRecive.UnitName)) { packet = new{ ActionId = 1005, ActionName = "StopUnit", ErrorCode = 101, ErrorMessage = "单元名称无效" }; } //TODO 优化 UnitControl.StopUnit(_UdpSocketPacketRecive.UnitName); String json = JsonConvert.SerializeObject(packet); Program.Logger.Log("UdpSocketServer", $"UdpPacketSent=>{_IPEndPoint.Address}:{_IPEndPoint.Port}=>{json}"); this.AsyncNetUdpServer.SendAsync(AesEncrypt.Encrypt(Program.AppSettings.ControlKey, json), _IPEndPoint); }