Example #1
0
        private void ReaderBuffer(ReadOnlySpan <byte> buffer, Socket socket, SocketReceiveMessageFromResult receiveMessageFromResult)
        {
            try
            {
                var package = JT1078Serializer.Deserialize(buffer);
                package.SIM = package.SIM.TrimStart('0');
                if (Logger.IsEnabled(LogLevel.Trace))
                {
                    Logger.LogTrace($"[Accept Hex {receiveMessageFromResult.RemoteEndPoint}]:{buffer.ToArray().ToHexString()}");
                }
                var session = SessionManager.TryLink(package.SIM, socket, receiveMessageFromResult.RemoteEndPoint);
                if (Logger.IsEnabled(LogLevel.Information))
                {
                    Logger.LogInformation($"[Connected]:{receiveMessageFromResult.RemoteEndPoint}");
                }
                if (jT1078UseType == JT1078UseType.Queue)
                {
                    jT1078MsgProducer.ProduceAsync(package.SIM, buffer.ToArray());
                }
                else
                {
                    jT1078PackageProducer.ProduceAsync(package.SIM, package);
                }
            }
            catch (NotImplementedException ex)
            {
                Logger.LogError(ex.Message);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                Logger.LogError(ex, $"[ReaderBuffer]:{ buffer.ToArray().ToHexString()}");
            }
#pragma warning restore CA1031 // Do not catch general exception types
        }
        private void ReaderBuffer(ref ReadOnlySequence <byte> buffer, JT1078TcpSession session, out SequencePosition consumed, out SequencePosition examined)
        {
            consumed = buffer.Start;
            examined = buffer.End;
            if (buffer.Length < 15)
            {
                throw new ArgumentException("not JT1078 package");
            }
            SequenceReader <byte> seqReader = new SequenceReader <byte>(buffer);
            long totalConsumed = 0;

            while (!seqReader.End)
            {
                if ((seqReader.Length - seqReader.Consumed) < 15)
                {
                    throw new ArgumentException("not JT1078 package");
                }
                var header      = seqReader.Sequence.Slice(seqReader.Consumed, 4);
                var headerValue = BinaryPrimitives.ReadUInt32BigEndian(header.FirstSpan);
                if (JT1078Package.FH == headerValue)
                {
                    //sim
                    var sim = ReadBCD(seqReader.Sequence.Slice(seqReader.Consumed + 8, 6).FirstSpan, 12);
                    //根据数据类型处理对应的数据长度
                    seqReader.Advance(15);
                    if (seqReader.TryRead(out byte dataType))
                    {
                        JT1078Label3 label3     = new JT1078Label3(dataType);
                        int          bodyLength = 0;
                        //透传的时候没有该字段
                        if (label3.DataType != JT1078DataType.透传数据)
                        {
                            //时间戳
                            bodyLength += 8;
                        }
                        //非视频帧时没有该字段
                        if (label3.DataType == JT1078DataType.视频I帧 ||
                            label3.DataType == JT1078DataType.视频P帧 ||
                            label3.DataType == JT1078DataType.视频B帧)
                        {
                            //上一个关键帧 + 上一帧 = 2 + 2
                            bodyLength += 4;
                        }
                        seqReader.Advance(bodyLength);
                        var bodyLengthFirstSpan = seqReader.Sequence.Slice(seqReader.Consumed, 2).FirstSpan;
                        //数据体长度
                        seqReader.Advance(2);
                        bodyLength = BinaryPrimitives.ReadUInt16BigEndian(bodyLengthFirstSpan);
                        //数据体
                        seqReader.Advance(bodyLength);
                        if (string.IsNullOrEmpty(sim))
                        {
                            sim = session.SessionID;
                        }
                        SessionManager.TryLink(sim, session);
                        var package = seqReader.Sequence.Slice(totalConsumed, seqReader.Consumed - totalConsumed);
                        try
                        {
                            if (jT1078UseType == JT1078UseType.Queue)
                            {
                                jT1078MsgProducer.ProduceAsync(sim, package.ToArray());
                            }
                            else
                            {
                                jT1078PackageProducer.ProduceAsync(sim, JT1078Serializer.Deserialize(package.FirstSpan));
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex, $"[Parse]:{package.ToArray().ToHexString()}");
                        }
                        totalConsumed += (seqReader.Consumed - totalConsumed);
                        if (seqReader.End)
                        {
                            break;
                        }
                    }
                }
            }
            if (seqReader.Length == totalConsumed)
            {
                examined = consumed = buffer.End;
            }
            else
            {
                consumed = buffer.GetPosition(totalConsumed);
            }
        }