protected override void ChannelRead0(IChannelHandlerContext ctx, byte[] msg) { try { //解析到头部,然后根据具体的消息Id通过队列去进行消费 //要是一定要解析到数据体可以在JT808MsgIdHandlerBase类中根据具体的消息, //解析具体的消息体,具体调用JT808Serializer.Deserialize<T> JT808HeaderPackage jT808HeaderPackage = JT808Serializer.Deserialize <JT808HeaderPackage>(msg); if (logger.IsEnabled(LogLevel.Trace)) { logger.LogTrace($"accept package success count=>{jT808AtomicCounterService.MsgSuccessCount.ToString()},accept msg=>{ByteBufferUtil.HexDump(msg)}"); } jT808AtomicCounterService.MsgSuccessIncrement(); jT808SessionManager.TryAdd(jT808HeaderPackage.Header.TerminalPhoneNo, ctx.Channel); JT808MsgProducer.ProduceAsync(jT808HeaderPackage.Header.TerminalPhoneNo, msg); } catch (JT808Exception ex) { jT808AtomicCounterService.MsgFailIncrement(); if (logger.IsEnabled(LogLevel.Error)) { logger.LogError(ex, $"accept package fail count=>{jT808AtomicCounterService.MsgFailCount.ToString()},accept msg=>{ByteBufferUtil.HexDump(msg)}"); } } catch (Exception ex) { jT808AtomicCounterService.MsgFailIncrement(); if (logger.IsEnabled(LogLevel.Error)) { logger.LogError(ex, $"accept package fail count=>{jT808AtomicCounterService.MsgFailCount.ToString()},accept msg=>{ByteBufferUtil.HexDump(msg)}"); } } }
protected override void ChannelRead0(IChannelHandlerContext ctx, byte[] msg) { try { jT808SourcePackageDispatcher.SendAsync(msg); jT808UplinkPacket.ProcessorAsync(msg, JT808TransportProtocolType.tcp); //解析到头部,然后根据具体的消息Id通过队列去进行消费 //要是一定要解析到数据体可以在JT808MsgIdHandlerBase类中根据具体的消息, //解析具体的消息体,具体调用JT808Serializer.Deserialize<T> JT808HeaderPackage jT808HeaderPackage = JT808Serializer.Deserialize <JT808HeaderPackage>(msg); jT808AtomicCounterService.MsgSuccessIncrement(); jT808TrafficService.ReceiveSize(msg.Length); if (logger.IsEnabled(LogLevel.Trace)) { logger.LogTrace("accept package success count<<<" + jT808AtomicCounterService.MsgSuccessCount.ToString()); logger.LogTrace("accept msg <<< " + ByteBufferUtil.HexDump(msg)); } jT808SessionManager.TryAdd(jT808HeaderPackage.Header.TerminalPhoneNo, ctx.Channel); if (handler.HandlerDict.TryGetValue(jT808HeaderPackage.Header.MsgId, out var handlerFunc)) { IJT808Reply jT808Response = handlerFunc(new JT808Request(jT808HeaderPackage, msg)); if (jT808Response != null) { if (!jT808TransmitAddressFilterService.ContainsKey(ctx.Channel.RemoteAddress)) { ctx.WriteAndFlushAsync(jT808Response); } } } else { //未知的消息类型已日志形式输出 if (unknownLogger.IsEnabled(LogLevel.Debug)) { unknownLogger.LogDebug(ByteBufferUtil.HexDump(msg)); } } } catch (JT808.Protocol.Exceptions.JT808Exception ex) { jT808AtomicCounterService.MsgFailIncrement(); if (logger.IsEnabled(LogLevel.Error)) { logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg)); } } catch (Exception ex) { jT808AtomicCounterService.MsgFailIncrement(); if (logger.IsEnabled(LogLevel.Error)) { logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg)); } } }
protected override void ChannelRead0(IChannelHandlerContext ctx, byte[] msg) { try { jT808SourcePackageDispatcher?.SendAsync(msg); //解析到头部,然后根据具体的消息Id通过队列去进行消费 //要是一定要解析到数据体可以在JT808MsgIdHandlerBase类中根据具体的消息, //解析具体的消息体,具体调用JT808Serializer.Deserialize<T> JT808HeaderPackage jT808HeaderPackage = JT808Serializer.Deserialize <JT808HeaderPackage>(msg); jT808AtomicCounterService.MsgSuccessIncrement(); jT808TrafficService.ReceiveSize(msg.Length); if (logger.IsEnabled(LogLevel.Debug)) { logger.LogDebug("accept package success count<<<" + jT808AtomicCounterService.MsgSuccessCount.ToString()); } jT808SessionManager.TryAdd(jT808HeaderPackage.Header.TerminalPhoneNo, ctx.Channel); Func <JT808Request, JT808Response> handlerFunc; if (handler.HandlerDict.TryGetValue(jT808HeaderPackage.Header.MsgId, out handlerFunc)) { JT808Response jT808Response = handlerFunc(new JT808Request(jT808HeaderPackage, msg)); if (jT808Response != null) { if (!jT808TransmitAddressFilterService.ContainsKey(ctx.Channel.RemoteAddress)) { var sendData = JT808Serializer.Serialize(jT808Response.Package, jT808Response.MinBufferSize); jT808TrafficService.SendSize(sendData.Length); ctx.WriteAndFlushAsync(Unpooled.WrappedBuffer(sendData)); } } } } catch (JT808.Protocol.Exceptions.JT808Exception ex) { jT808AtomicCounterService.MsgFailIncrement(); if (logger.IsEnabled(LogLevel.Error)) { logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg)); } } catch (Exception ex) { jT808AtomicCounterService.MsgFailIncrement(); if (logger.IsEnabled(LogLevel.Error)) { logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg)); } } }
protected override void ChannelRead0(IChannelHandlerContext ctx, byte[] msg) { try { jT808SourcePackageDispatcher?.SendAsync(msg); //在压力大的情况下可以只解析到头部 //然后根据具体的消息Id通过队列去进行消费 //JT808HeaderPackage jT808HeaderPackage = JT808Serializer.Deserialize<JT808HeaderPackage>(msg); JT808Package jT808Package = JT808Serializer.Deserialize(msg); jT808AtomicCounterService.MsgSuccessIncrement(); if (logger.IsEnabled(LogLevel.Debug)) { logger.LogDebug("accept package success count<<<" + jT808AtomicCounterService.MsgSuccessCount.ToString()); } jT808SessionManager.TryAddOrUpdateSession(new JT808Session(ctx.Channel, jT808Package.Header.TerminalPhoneNo)); Func <JT808Request, JT808Response> handlerFunc; if (handler.HandlerDict.TryGetValue(jT808Package.Header.MsgId, out handlerFunc)) { //JT808Response jT808Response = handlerFunc(new JT808Request(jT808HeaderPackage, msg)); JT808Response jT808Response = handlerFunc(new JT808Request(jT808Package, msg)); if (jT808Response != null) { if (!jT808TransmitAddressFilterService.ContainsKey(ctx.Channel.RemoteAddress)) { ctx.WriteAndFlushAsync(Unpooled.WrappedBuffer(JT808Serializer.Serialize(jT808Response.Package, jT808Response.MinBufferSize))); } } } } catch (JT808.Protocol.Exceptions.JT808Exception ex) { jT808AtomicCounterService.MsgFailIncrement(); if (logger.IsEnabled(LogLevel.Error)) { logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg)); } } catch (Exception ex) { jT808AtomicCounterService.MsgFailIncrement(); if (logger.IsEnabled(LogLevel.Error)) { logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg)); } } }
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(ReadOnlySpan <byte> buffer, Socket socket, SocketReceiveMessageFromResult receiveMessageFromResult) { try { var package = Serializer.HeaderDeserialize(buffer); 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()}"); } //设直连模式和转发模式的会话如何处理 string sessionId = SessionManager.TryLink(package.Header.TerminalPhoneNo, socket, receiveMessageFromResult.RemoteEndPoint); if (Logger.IsEnabled(LogLevel.Information)) { Logger.LogInformation($"[Connected]:{receiveMessageFromResult.RemoteEndPoint}"); } if (Configuration.MessageQueueType == JT808MessageQueueType.InMemory) { MsgProducer.ProduceAsync(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}-{buffer.ToArray().ToHexString()}"); } catch (Exception ex) { if (Logger.IsEnabled(LogLevel.Debug)) { Logger.LogDebug($"[Atomic Fail Counter]:{AtomicCounterService.MsgFailCount}"); } Logger.LogError(ex, $"[ReaderBuffer]:{ buffer.ToArray().ToHexString()}"); } }
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); } }