private static async Task ManyAcceptConnections(CancellationToken token) { while (!token.IsCancellationRequested) { try { var client = await ManyListener.AcceptTcpClientAsync(); Logger.Warn($"设备已连接,IP:{client.Client.RemoteEndPoint}"); var md = new ManyMetadata(client); ManyClients.TryAdd(client.Client.RemoteEndPoint.ToString(), md); #pragma warning disable 4014 Task.Run(() => ManyDataReceiver(md), md.Token); #pragma warning restore 4014 } catch (Exception e) { Logger.Error(e); throw; } } }
private static async Task ManyDataReceiver(ManyMetadata md) { var header = "[" + md.TcpClient.Client.RemoteEndPoint.ToString() + "]"; $"{header} many data receiver started".Info(); try { while (true) { if (!IsClientConnected(md.TcpClient)) { Logger.Error(header + $" client no longer connected[{md.LinkId}]"); break; } if (Token.IsCancellationRequested) { //if (isDebug) Logger.Info(header + " cancellation requested"); break; } var data = await DataReadAsync(md.TcpClient, Token); if (data == null || data.Length < 1) { Thread.Sleep(100); continue; } var length = data.Length; var linkId = 0; var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(10) }; switch (data.Length) { case 3: linkId = data[2]; lock (ClientInfo.locker) { ClientInfo.cache.Set("many", "", policy); } switch (data[1]) { case 0xC7: $"收到连接[3][{ManyClients.Count}][{linkId}]注册包".Info(); md.LinkId = linkId; var policy1 = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddSeconds(30 * 3) }; lock (ClientInfo.locker) { ClientInfo.cache.Set(linkId.ToString(), "", policy1); } break; case 0xC8: //if (isDebug) { Logger.Info($"收到连接[3][{ManyClients.Count}][{linkId}]心跳包"); } md.LinkId = linkId; var policy2 = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddSeconds(30 * 3) }; lock (ClientInfo.locker) { ClientInfo.cache.Set(linkId.ToString(), "", policy2); } break; } break; case 4: var linkIdHigh = data[2]; var linkIdLow = data[3]; linkId = (linkIdHigh << 8) | (linkIdLow); lock (ClientInfo.locker) { ClientInfo.cache.Set("many", "", policy); } switch (data[1]) { case 0xC7: $"收到连接[4][{ManyClients.Count}][{linkId}]注册包".Info(); md.LinkId = linkId; var policy1 = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddSeconds(30 * 3) }; lock (ClientInfo.locker) { ClientInfo.cache.Set(linkId.ToString(), "", policy1); } break; case 0xC8: //if (isDebug) Logger.Info($"收到连接[4][{ManyClients.Count}][{linkId}]心跳包"); md.LinkId = linkId; var policy2 = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddSeconds(30 * 3) }; lock (ClientInfo.locker) { ClientInfo.cache.Set(linkId.ToString(), "", policy2); } break; } break; default: if (length <= 5) { continue; } Logger.Info($"[M][{md.TcpClient.Client.RemoteEndPoint}]:{BytesToHexString(data)}"); var crc32 = data.Take(length - 2).ToArray().CRC16(); if (data[length - 2] == crc32[1] && data[length - 1] == crc32[0]) { if (useLinkId) { if (md.TcpClient.Client.RemoteEndPoint.ToString() == ClientInfo.IpAddress) { var dt = new DateTime(); lock (ClientInfo.locker) { dt = ClientInfo.RequestTime; } var ts = DateTime.Now - dt; if (ts.TotalMilliseconds < modbusTimeout + 500) { lock (ClientInfo.locker) { if (!isUseStrictCheckMode || (ClientInfo.ExpectedType == data[1] && ClientInfo.ExpectedDataLen == data[2])) { OneQueue.Enqueue(data.Take(data.Length).ToArray()); $"[M->1]{header}:{ BytesToHexString(data)}".Info(); } else { Logger.Warn($"[M->1] [{ClientInfo.ExpectedType}][{ClientInfo.ExpectedDataLen}],{BytesToHexString(data)}"); } } } else { Logger.Warn($"TimeOut[{ts.TotalMilliseconds}][M->1] [{ClientInfo.ExpectedType}][{ClientInfo.ExpectedDataLen}],{BytesToHexString(data)}"); } } else { var dt = new DateTime(); lock (ClientInfo.locker) { dt = ClientInfo.RequestTime; } var ts = DateTime.Now - dt; if (ts.TotalMilliseconds < modbusTimeout) { //Logger.Error($"[M->?][{md.TcpClient.Client.RemoteEndPoint}] :[{ ClientInfo.ManyIpAddress}]"); //$"[M->?]{header}:{ BytesToHexString(data)}".Info(); } else { //Logger.Error("[M->?] timeout!"); } } } else { OneQueue.Enqueue(data.Take(data.Length).ToArray()); $"[M->1]{header}: { BytesToHexString(data)}".Info(); } } else { Logger.Error($"CRC Error:{crc32[1]}:{crc32[0]}"); } break; } } } catch (Exception e) { Logger.Error( Environment.NewLine + header + " ManyDataReceiver Exception: " + Environment.NewLine + e.ToString() + Environment.NewLine + e.StackTrace ); } //if (isDebug) Logger.Info(header + " data receiver terminating"); ManyClients.TryRemove(md.TcpClient.Client.RemoteEndPoint.ToString(), out _); md.Dispose(); }