You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

53 lines
1.7 KiB

  1. using DotNetty.Buffers;
  2. using DotNetty.Codecs;
  3. using JT808.Protocol;
  4. using DotNetty.Transport.Channels;
  5. using JT808.DotNetty.Core.Interfaces;
  6. using Microsoft.Extensions.Logging;
  7. using JT808.Protocol.Interfaces;
  8. namespace JT808.DotNetty.Core.Codecs
  9. {
  10. /// <summary>
  11. /// tcp统一下发出口
  12. /// </summary>
  13. public class JT808TcpEncoder : MessageToByteEncoder<IJT808Reply>
  14. {
  15. private readonly ILogger<JT808TcpEncoder> logger;
  16. private readonly JT808Serializer JT808Serializer;
  17. public JT808TcpEncoder(
  18. IJT808Config jT808Config,
  19. ILoggerFactory loggerFactory)
  20. {
  21. logger = loggerFactory.CreateLogger<JT808TcpEncoder>();
  22. this.JT808Serializer = jT808Config.GetSerializer();
  23. }
  24. protected override void Encode(IChannelHandlerContext context, IJT808Reply message, IByteBuffer output)
  25. {
  26. if (message.Package != null)
  27. {
  28. try
  29. {
  30. var sendData = JT808Serializer.Serialize(message.Package, minBufferSize: message.MinBufferSize);
  31. output.WriteBytes(Unpooled.WrappedBuffer(sendData));
  32. }
  33. catch (JT808.Protocol.Exceptions.JT808Exception ex)
  34. {
  35. logger.LogError(ex, context.Channel.Id.AsShortText());
  36. }
  37. catch (System.Exception ex)
  38. {
  39. logger.LogError(ex, context.Channel.Id.AsShortText());
  40. }
  41. }
  42. else if (message.HexData != null)
  43. {
  44. output.WriteBytes(Unpooled.WrappedBuffer(message.HexData));
  45. }
  46. }
  47. }
  48. }