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) { try { var package = Serializer.HeaderDeserialize(seqReader.Sequence.Slice(totalConsumed, seqReader.Consumed - totalConsumed).FirstSpan); 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 (Configuration.MessageQueueType == JT808MessageQueueType.InMemory) { MsgProducer.ProduceAsync(session.SessionID, package.OriginalData.ToArray()); } else { MsgProducer.ProduceAsync(package.Header.TerminalPhoneNo, package.OriginalData.ToArray()); } } catch (JT808Exception ex) { AtomicCounterService.MsgFailIncrement(); if (Logger.IsEnabled(LogLevel.Debug)) { Logger.LogDebug($"[Atomic Fail Counter]:{AtomicCounterService.MsgFailCount}"); } Logger.LogError(ex, $"[HeaderDeserialize ErrorCode]:{ ex.ErrorCode}"); } 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); } }
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); } }