diff --git a/Ems.CollectService.Analysis/BufferAnalysis.cs b/Ems.CollectService.Analysis/BufferAnalysis.cs index 204cadd..c693a01 100644 --- a/Ems.CollectService.Analysis/BufferAnalysis.cs +++ b/Ems.CollectService.Analysis/BufferAnalysis.cs @@ -36,7 +36,10 @@ namespace Ems.CollectService.Analysis } } - private BufferAnalysis() { } + private BufferAnalysis() + { + logger.Info($"版本更新,修复连接ID重复,主动断开原有连接信息建立新连接"); + } /// /// 登录指令 @@ -45,7 +48,7 @@ namespace Ems.CollectService.Analysis /// /// /// - public bool LoginMessageHandling(SocketClient client, byte[] buffer, int count) + public bool LoginMessageHandling(SocketClient client, byte[] buffer, int count,TcpService tcpService) { byte[] array = new byte[2]; var flag = ""; @@ -72,18 +75,37 @@ namespace Ems.CollectService.Analysis Array.Copy(buffer, 2, array, 0, 2); string text = Encoding.ASCII.GetString(buffer, 1, 1) + msgUtil.ConverToString(array); - if (SendMessageToClient(client, messagePack)) + string clientIdStr = flag.ToString(); + if (clientIdStr.Contains("45")) { - logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};回复登录指令成功"); - client.ResetID(flag.ToString()); - - if(client.ID.Length >= 4 ) + var clients = tcpService.GetClients(); + var info = clients.Where(x => x.ID == clientIdStr).ToList(); + if (info.Count > 0) + { + logger.Trace($"客户端:{client.ID};地址:{client.GetIPPort()};已存在连接信息,主动断开已有连接"); + foreach (var item in info) + { + item.Close(); + logger.Trace($"客户端:{item.ID};地址:{item.GetIPPort()};已断开"); + } + } + if (client.ID != clientIdStr) { - string clientId = string.Format("E{0}", client.ID.ToString().Substring(2, client.ID.ToString().Length - 2)); - UpdateCollectDeviceOnLineState(clientId, 1); + client.ResetID(clientIdStr); } + + if (SendMessageToClient(client, messagePack)) + { + logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};回复登录指令成功"); + + if(client.ID.Length >= 4 ) + { + string clientId = string.Format("E{0}", client.ID.ToString().Substring(2, client.ID.ToString().Length - 2)); + UpdateCollectDeviceOnLineState(clientId, 1); + } - return true; + return true; + } } return false; @@ -91,6 +113,7 @@ namespace Ems.CollectService.Analysis catch (Exception ex) { logger.Error($"LoginMessageHandling登录指令解析异常:{ex.Message}"); + client.Close(); return false; } } @@ -130,6 +153,8 @@ namespace Ems.CollectService.Analysis if(SendMessageAsync(client, SendMessagePackInfo)) { logger.Debug($"向客户端:{client.ID};地址:{client.GetIPPort()};回复心跳指令成功"); + //bool isRes = SendTimeSyncToClient(client, SendMessagePackInfo); + //logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};发送校时指令{(isRes ? "成功":"失败")}"); return true; } else @@ -881,5 +906,61 @@ namespace Ems.CollectService.Analysis return false; } } + + public bool SendTimeSyncToClient(SocketClient client, MessagePack pMessagePack) + { + ushort num = 0; + try + { + byte[] SendBuffer = new byte[24]; + SendBuffer[num] = pMessagePack.m_BeginChar; + num = (ushort)(num + 1); + SendBuffer[num] = pMessagePack.m_EnergyType; + num = (ushort)(num + 1); + Array.Copy(pMessagePack.m_Meteraddr, 0, SendBuffer, num, pMessagePack.m_Meteraddr.Length); + num = (ushort)(num + 2); + Array.Copy(pMessagePack.m_Msta, 0, SendBuffer, num, pMessagePack.m_Msta.Length); + num = (ushort)(num + 2); + SendBuffer[num] = pMessagePack.m_StartFlag; + num = (ushort)(num + 1); + SendBuffer[num] = pMessagePack.m_MessageType; + num = (ushort)(num + 1); + Array.Copy(pMessagePack.m_PackLen, 0, SendBuffer, num, pMessagePack.m_PackLen.Length); + num = (ushort)(num + 2); + + DateTime currentTime = DateTime.Now; + byte[] timeBuffer = new byte[] + { + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + (byte)currentTime.Second, + (byte)currentTime.Minute, + (byte)currentTime.Hour, + (byte)(currentTime.Year%100), + (byte)currentTime.Month, + (byte)currentTime.Day, + }; + Array.Copy(timeBuffer, 0, SendBuffer, num, timeBuffer.Length); + num = (ushort)(num + 12); + + pMessagePack.m_Verify = MsgUtil.CalculateVerify(SendBuffer, SendBuffer.Length - 1)[0]; + SendBuffer[num] = pMessagePack.m_Verify; + num = (ushort)(num + 1); + SendBuffer[num] = pMessagePack.m_EndChar; + logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};发送终端消息:{msgUtil.bytesToHexStr(SendBuffer, SendBuffer.Length)}"); + + client.SendAsync(SendBuffer); + return true; + } + catch (Exception ex) + { + logger.Error($"SendMessageToClient异常:{ex.Message}"); + return false; + } + } } } diff --git a/Ems.CollectService.TouchSocket/TcpServer.cs b/Ems.CollectService.TouchSocket/TcpServer.cs index 949b176..35d3885 100644 --- a/Ems.CollectService.TouchSocket/TcpServer.cs +++ b/Ems.CollectService.TouchSocket/TcpServer.cs @@ -69,7 +69,7 @@ namespace Ems.CollectService.TouchSocket { string body = Encoding.UTF8.GetString(myRequestInfo.Body, 0, myRequestInfo.Body.Length); - bufferMemory(client, myRequestInfo); + bufferMemory(client, myRequestInfo,service); } }; @@ -102,7 +102,7 @@ namespace Ems.CollectService.TouchSocket /// /// /// - private void bufferMemory(SocketClient client, MyFixedHeaderRequestInfo requestInfo) + private void bufferMemory(SocketClient client, MyFixedHeaderRequestInfo requestInfo,TcpService tcpService) { try { @@ -120,7 +120,7 @@ namespace Ems.CollectService.TouchSocket //登录指令 =>回复登录指令、下发校时指令 //db.StringSet("LoginMessage:" + clientId, body, TimeSpan.FromMinutes(10)); logger.Info($"{client.ID}登录指令,Header:{header};Body:{body}"); - bufferAnalysis.LoginMessageHandling(client, buffer, requestInfo.Header.Length + requestInfo.Body.Length); + bufferAnalysis.LoginMessageHandling(client, buffer, requestInfo.Header.Length + requestInfo.Body.Length,tcpService); break; case 0X24: //心跳指令