public JT808ResultDto <bool> Send(string terminalPhoneNo, byte[] data) { JT808ResultDto <bool> resultDto = new JT808ResultDto <bool>(); try { var session = jT808SessionManager.GetSession(terminalPhoneNo); if (session != null) { session.Channel.WriteAndFlushAsync(jT808DatagramPacket.Create(data, session.Sender)); 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); }
protected override void ChannelRead0(IChannelHandlerContext ctx, JT808UdpPackage msg) { try { jT808SourcePackageDispatcher.SendAsync(msg.Buffer); jT808UplinkPacket.ProcessorAsync(msg.Buffer, JT808TransportProtocolType.udp); //解析到头部,然后根据具体的消息Id通过队列去进行消费 //要是一定要解析到数据体可以在JT808MsgIdHandlerBase类中根据具体的消息, //解析具体的消息体,具体调用JT808Serializer.Deserialize<T> JT808HeaderPackage jT808HeaderPackage = JT808Serializer.Deserialize <JT808HeaderPackage>(msg.Buffer); jT808AtomicCounterService.MsgSuccessIncrement(); jT808TrafficService.ReceiveSize(msg.Buffer.Length); jT808UdpSessionManager.TryAdd(ctx.Channel, msg.Sender, jT808HeaderPackage.Header.TerminalPhoneNo); if (logger.IsEnabled(LogLevel.Trace)) { logger.LogTrace("accept package success count<<<" + jT808AtomicCounterService.MsgSuccessCount.ToString()); logger.LogTrace("accept msg <<< " + ByteBufferUtil.HexDump(msg.Buffer)); } if (handler.HandlerDict.TryGetValue(jT808HeaderPackage.Header.MsgId, out var handlerFunc)) { IJT808Reply jT808Response = handlerFunc(new JT808Request(jT808HeaderPackage, msg.Buffer)); if (jT808Response != null) { var sendData = JT808Serializer.Serialize(jT808Response.Package, jT808Response.MinBufferSize); ctx.WriteAndFlushAsync(jT808DatagramPacket.Create(sendData, msg.Sender)); } } else { //未知的消息类型已日志形式输出 if (unknownLogger.IsEnabled(LogLevel.Debug)) { unknownLogger.LogDebug(ByteBufferUtil.HexDump(msg.Buffer)); } } } 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.Buffer)); } } 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.Buffer)); } } }