Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

96 linhas
3.3 KiB

  1. using DotNetty.Handlers.Timeout;
  2. using DotNetty.Transport.Channels;
  3. using JT808.Protocol.MessageBody;
  4. using Microsoft.Extensions.Logging;
  5. using System;
  6. using System.Threading.Tasks;
  7. namespace JT808.DotNetty.Client.Handlers
  8. {
  9. /// <summary>
  10. /// JT808客户端连接通道处理程序
  11. /// </summary>
  12. public class JT808TcpClientConnectionHandler : ChannelHandlerAdapter
  13. {
  14. private readonly ILogger<JT808TcpClientConnectionHandler> logger;
  15. private readonly JT808TcpClient jT808TcpClient;
  16. public JT808TcpClientConnectionHandler(
  17. JT808TcpClient jT808TcpClient)
  18. {
  19. logger = jT808TcpClient.LoggerFactory.CreateLogger<JT808TcpClientConnectionHandler>();
  20. this.jT808TcpClient = jT808TcpClient;
  21. }
  22. /// <summary>
  23. /// 通道激活
  24. /// </summary>
  25. /// <param name="context"></param>
  26. public override void ChannelActive(IChannelHandlerContext context)
  27. {
  28. string channelId = context.Channel.Id.AsShortText();
  29. if (logger.IsEnabled(LogLevel.Debug))
  30. logger.LogDebug($"<<<{ channelId } Successful client connection to server.");
  31. base.ChannelActive(context);
  32. }
  33. /// <summary>
  34. /// 设备主动断开
  35. /// </summary>
  36. /// <param name="context"></param>
  37. public override void ChannelInactive(IChannelHandlerContext context)
  38. {
  39. string channelId = context.Channel.Id.AsShortText();
  40. if (logger.IsEnabled(LogLevel.Debug))
  41. logger.LogDebug($">>>{ channelId } The client disconnects from the server.");
  42. base.ChannelInactive(context);
  43. }
  44. /// <summary>
  45. /// 服务器主动断开
  46. /// </summary>
  47. /// <param name="context"></param>
  48. /// <returns></returns>
  49. public override Task CloseAsync(IChannelHandlerContext context)
  50. {
  51. string channelId = context.Channel.Id.AsShortText();
  52. if (logger.IsEnabled(LogLevel.Debug))
  53. logger.LogDebug($"<<<{ channelId } The server disconnects from the client.");
  54. return base.CloseAsync(context);
  55. }
  56. public override void ChannelReadComplete(IChannelHandlerContext context)=> context.Flush();
  57. /// <summary>
  58. /// 超时策略
  59. /// </summary>
  60. /// <param name="context"></param>
  61. /// <param name="evt"></param>
  62. public override void UserEventTriggered(IChannelHandlerContext context, object evt)
  63. {
  64. IdleStateEvent idleStateEvent = evt as IdleStateEvent;
  65. if (idleStateEvent != null)
  66. {
  67. if(idleStateEvent.State== IdleState.WriterIdle)
  68. {
  69. string channelId = context.Channel.Id.AsShortText();
  70. logger.LogInformation($"{idleStateEvent.State.ToString()}>>>{channelId}");
  71. jT808TcpClient.Send(new JT808_0x0002());
  72. }
  73. }
  74. base.UserEventTriggered(context, evt);
  75. }
  76. public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
  77. {
  78. string channelId = context.Channel.Id.AsShortText();
  79. logger.LogError(exception,$"{channelId} {exception.Message}" );
  80. context.CloseAsync();
  81. }
  82. }
  83. }