コード例 #1
0
 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();
         jT808TcpTrafficService.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);
                     jT808TcpTrafficService.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));
         }
     }
 }
コード例 #2
0
        public JT808ResultDto <bool> Send(string terminalPhoneNo, byte[] data)
        {
            JT808ResultDto <bool> resultDto = new JT808ResultDto <bool>();

            try
            {
                var session = jT808SessionManager.GetSession(terminalPhoneNo);
                if (session != null)
                {
                    //判断转发数据是下发不了消息的
                    if (jT808SessionManager.GetAll().Count(c => c.Channel.Id == session.Channel.Id) > 1)
                    {
                        resultDto.Code    = JT808ResultCode.Ok;
                        resultDto.Data    = false;
                        resultDto.Message = "not support transmit data send.";
                    }
                    else
                    {
                        jT808TcpTrafficService.SendSize(data.Length);
                        session.Channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(data));
                        resultDto.Code = JT808ResultCode.Ok;
                        resultDto.Data = true;
                    }
                }
                else
                {
                    resultDto.Code    = JT808ResultCode.Ok;
                    resultDto.Data    = false;
                    resultDto.Message = "offline";
                }
            }
            catch (Exception ex)
            {
                resultDto.Data    = false;
                resultDto.Code    = JT808ResultCode.Error;
                resultDto.Message = Newtonsoft.Json.JsonConvert.SerializeObject(ex);
            }
            return(resultDto);
        }