public SeedTcpSession() { for (var i = 0; i < 10; i++) { var channel = new EmbeddedChannel(new JT808DefaultChannelId()); jT80TcpSessionManager.TryAdd(i.ToString(), channel); } }
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)); } } }