change - 修复连接ID重复,主动断开原有连接信息建立新连接

master
wenjy 10 months ago
parent f89d8c2dc4
commit caca29533d

@ -36,7 +36,10 @@ namespace Ems.CollectService.Analysis
} }
} }
private BufferAnalysis() { } private BufferAnalysis()
{
logger.Info($"版本更新修复连接ID重复主动断开原有连接信息建立新连接");
}
/// <summary> /// <summary>
/// 登录指令 /// 登录指令
@ -45,7 +48,7 @@ namespace Ems.CollectService.Analysis
/// <param name="buffer"></param> /// <param name="buffer"></param>
/// <param name="count"></param> /// <param name="count"></param>
/// <returns></returns> /// <returns></returns>
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]; byte[] array = new byte[2];
var flag = ""; var flag = "";
@ -72,18 +75,37 @@ namespace Ems.CollectService.Analysis
Array.Copy(buffer, 2, array, 0, 2); Array.Copy(buffer, 2, array, 0, 2);
string text = Encoding.ASCII.GetString(buffer, 1, 1) + msgUtil.ConverToString(array); 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()};回复登录指令成功"); var clients = tcpService.GetClients();
client.ResetID(flag.ToString()); var info = clients.Where(x => x.ID == clientIdStr).ToList();
if (info.Count > 0)
if(client.ID.Length >= 4 ) {
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)); client.ResetID(clientIdStr);
UpdateCollectDeviceOnLineState(clientId, 1);
} }
return true; 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 false; return false;
@ -91,6 +113,7 @@ namespace Ems.CollectService.Analysis
catch (Exception ex) catch (Exception ex)
{ {
logger.Error($"LoginMessageHandling登录指令解析异常{ex.Message}"); logger.Error($"LoginMessageHandling登录指令解析异常{ex.Message}");
client.Close();
return false; return false;
} }
} }
@ -130,6 +153,8 @@ namespace Ems.CollectService.Analysis
if(SendMessageAsync(client, SendMessagePackInfo)) if(SendMessageAsync(client, SendMessagePackInfo))
{ {
logger.Debug($"向客户端:{client.ID};地址:{client.GetIPPort()};回复心跳指令成功"); logger.Debug($"向客户端:{client.ID};地址:{client.GetIPPort()};回复心跳指令成功");
//bool isRes = SendTimeSyncToClient(client, SendMessagePackInfo);
//logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};发送校时指令{(isRes ? "成功":"失败")}");
return true; return true;
} }
else else
@ -881,5 +906,61 @@ namespace Ems.CollectService.Analysis
return false; 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;
}
}
} }
} }

@ -69,7 +69,7 @@ namespace Ems.CollectService.TouchSocket
{ {
string body = Encoding.UTF8.GetString(myRequestInfo.Body, 0, myRequestInfo.Body.Length); 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
/// </summary> /// </summary>
/// <param name="clientId"></param> /// <param name="clientId"></param>
/// <param name="requestInfo"></param> /// <param name="requestInfo"></param>
private void bufferMemory(SocketClient client, MyFixedHeaderRequestInfo requestInfo) private void bufferMemory(SocketClient client, MyFixedHeaderRequestInfo requestInfo,TcpService tcpService)
{ {
try try
{ {
@ -120,7 +120,7 @@ namespace Ems.CollectService.TouchSocket
//登录指令 =>回复登录指令、下发校时指令 //登录指令 =>回复登录指令、下发校时指令
//db.StringSet("LoginMessage:" + clientId, body, TimeSpan.FromMinutes(10)); //db.StringSet("LoginMessage:" + clientId, body, TimeSpan.FromMinutes(10));
logger.Info($"{client.ID}登录指令Header{header}Body{body}"); 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; break;
case 0X24: case 0X24:
//心跳指令 //心跳指令

Loading…
Cancel
Save