private void ReaderBuffer(ReadOnlySpan <byte> buffer, Socket socket, SocketReceiveMessageFromResult receiveMessageFromResult) { try { var package = Serializer.HeaderDeserialize(buffer, minBufferSize: 10240); AtomicCounterService.MsgSuccessIncrement(); if (Logger.IsEnabled(LogLevel.Debug)) { Logger.LogDebug($"[Atomic Success Counter]:{AtomicCounterService.MsgSuccessCount}"); } if (Logger.IsEnabled(LogLevel.Trace)) { Logger.LogTrace($"[Accept Hex {receiveMessageFromResult.RemoteEndPoint}]:{package.OriginalData.ToArray().ToHexString()}"); } var session = SessionManager.TryLink(package.Header.TerminalPhoneNo, socket, receiveMessageFromResult.RemoteEndPoint); if (Logger.IsEnabled(LogLevel.Information)) { Logger.LogInformation($"[Connected]:{receiveMessageFromResult.RemoteEndPoint}"); } if (JT808UseType == JT808UseType.Normal) { JT808NormalReplyMessageHandler.Processor(package, session); } else if (JT808UseType == JT808UseType.Queue) { MsgProducer.ProduceAsync(package.Header.TerminalPhoneNo, package.OriginalData.ToArray()); } } catch (NotImplementedException ex) { Logger.LogError(ex.Message); } catch (JT808Exception ex) { AtomicCounterService.MsgFailIncrement(); if (Logger.IsEnabled(LogLevel.Information)) { Logger.LogInformation($"[Atomic Fail Counter]:{AtomicCounterService.MsgFailCount}"); } Logger.LogError($"[HeaderDeserialize ErrorCode]:{ ex.ErrorCode},[ReaderBuffer]:{buffer.ToArray().ToHexString()}"); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { if (Logger.IsEnabled(LogLevel.Debug)) { Logger.LogDebug($"[Atomic Fail Counter]:{AtomicCounterService.MsgFailCount}"); } Logger.LogError(ex, $"[ReaderBuffer]:{ buffer.ToArray().ToHexString()}"); } #pragma warning restore CA1031 // Do not catch general exception types }
private void ReaderBuffer(ref ReadOnlySequence <byte> buffer, JT808TcpSession session, out SequencePosition consumed, out SequencePosition examined) { consumed = buffer.Start; examined = buffer.End; SequenceReader <byte> seqReader = new SequenceReader <byte>(buffer); if (seqReader.TryPeek(out byte beginMark)) { if (beginMark != JT808Package.BeginFlag) { throw new ArgumentException("Not JT808 Packages."); } } byte mark = 0; long totalConsumed = 0; while (!seqReader.End) { if (seqReader.IsNext(JT808Package.BeginFlag, advancePast: true)) { if (mark == 1) { ReadOnlySpan <byte> contentSpan = ReadOnlySpan <byte> .Empty; try { contentSpan = seqReader.Sequence.Slice(totalConsumed, seqReader.Consumed - totalConsumed).FirstSpan; //过滤掉不是808标准包(14) //(头)1+(消息 ID )2+(消息体属性)2+(终端手机号)6+(消息流水号)2+(检验码 )1+(尾)1 if (contentSpan.Length > 14) { var package = Serializer.HeaderDeserialize(contentSpan, minBufferSize: 10240); AtomicCounterService.MsgSuccessIncrement(); if (Logger.IsEnabled(LogLevel.Debug)) { Logger.LogDebug($"[Atomic Success Counter]:{AtomicCounterService.MsgSuccessCount}"); } if (Logger.IsEnabled(LogLevel.Trace)) { Logger.LogTrace($"[Accept Hex {session.Client.RemoteEndPoint}]:{package.OriginalData.ToArray().ToHexString()}"); } SessionManager.TryLink(package.Header.TerminalPhoneNo, session); if (JT808UseType == JT808UseType.Normal) { JT808NormalReplyMessageHandler.Processor(package, session); } else if (JT808UseType == JT808UseType.Queue) { MsgProducer.ProduceAsync(package.Header.TerminalPhoneNo, package.OriginalData.ToArray()); } } } catch (JT808Exception ex) { AtomicCounterService.MsgFailIncrement(); if (Logger.IsEnabled(LogLevel.Information)) { Logger.LogInformation($"[Atomic Fail Counter]:{AtomicCounterService.MsgFailCount}"); } Logger.LogError($"[HeaderDeserialize ErrorCode]:{ ex.ErrorCode},[ReaderBuffer]:{contentSpan.ToArray().ToHexString()}"); } totalConsumed += (seqReader.Consumed - totalConsumed); if (seqReader.End) { break; } seqReader.Advance(1); mark = 0; } mark++; } else { seqReader.Advance(1); } } if (seqReader.Length == totalConsumed) { examined = consumed = buffer.End; } else { consumed = buffer.GetPosition(totalConsumed); } }