protected override void ChannelRead0(IChannelHandlerContext ctx, byte[] msg)
 {
     if (logger.IsEnabled(LogLevel.Trace))
     {
         logger.LogTrace("accept msg<<<" + ByteBufferUtil.HexDump(msg));
     }
     jT808ReceiveAtomicCounterService.MsgSuccessIncrement();
 }
Exemplo n.º 2
0
        private void ReaderBuffer(ref ReadOnlySequence <byte> buffer, Socket 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 data    = seqReader.Sequence.Slice(totalConsumed, seqReader.Consumed - totalConsumed).ToArray();
                            var package = JT808Serializer.Deserialize(data, minBufferSize: 8096);
                            if (producer != null)
                            {
                                producer.ProduceAsync(package);
                            }
                            ReceiveAtomicCounterService.MsgSuccessIncrement();
                            if (Logger.IsEnabled(LogLevel.Debug))
                            {
                                Logger.LogDebug($"[Atomic Success Counter]:{ReceiveAtomicCounterService.MsgSuccessCount}");
                            }
                            if (Logger.IsEnabled(LogLevel.Trace))
                            {
                                Logger.LogTrace($"[Accept Hex {session.RemoteEndPoint}]:{data.ToHexString()}");
                            }
                        }
                        catch (JT808Exception ex)
                        {
                            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);
            }
        }