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.
 
 

98 lines
3.6 KiB

  1. using DotNetty.Buffers;
  2. using DotNetty.Handlers.Timeout;
  3. using DotNetty.Transport.Channels;
  4. using JT809.DotNetty.Core.Metadata;
  5. using JT809.Protocol;
  6. using JT809.Protocol.Enums;
  7. using JT809.Protocol.Extensions;
  8. using Microsoft.Extensions.Logging;
  9. using System;
  10. using System.Threading.Tasks;
  11. namespace JT809.DotNetty.Core.Handlers
  12. {
  13. /// <summary>
  14. /// JT809主链路服务端连接处理器
  15. /// </summary>
  16. public class JT809MainServerConnectionHandler : ChannelHandlerAdapter
  17. {
  18. private readonly ILogger<JT809MainServerConnectionHandler> logger;
  19. public JT809MainServerConnectionHandler(
  20. ILoggerFactory loggerFactory)
  21. {
  22. logger = loggerFactory.CreateLogger<JT809MainServerConnectionHandler>();
  23. }
  24. /// <summary>
  25. /// 通道激活
  26. /// </summary>
  27. /// <param name="context"></param>
  28. public override void ChannelActive(IChannelHandlerContext context)
  29. {
  30. string channelId = context.Channel.Id.AsShortText();
  31. if (logger.IsEnabled(LogLevel.Debug))
  32. logger.LogDebug($"<<<{ channelId } Successful client connection to server.");
  33. base.ChannelActive(context);
  34. }
  35. /// <summary>
  36. /// 客户端主动断开
  37. /// </summary>
  38. /// <param name="context"></param>
  39. public override void ChannelInactive(IChannelHandlerContext context)
  40. {
  41. string channelId = context.Channel.Id.AsShortText();
  42. if (logger.IsEnabled(LogLevel.Debug))
  43. logger.LogDebug($">>>{ channelId } The client disconnects from the server.");
  44. base.ChannelInactive(context);
  45. }
  46. /// <summary>
  47. /// 服务器主动断开
  48. /// </summary>
  49. /// <param name="context"></param>
  50. /// <returns></returns>
  51. public override Task CloseAsync(IChannelHandlerContext context)
  52. {
  53. string channelId = context.Channel.Id.AsShortText();
  54. if (logger.IsEnabled(LogLevel.Debug))
  55. logger.LogDebug($"<<<{ channelId } The server disconnects from the client.");
  56. return base.CloseAsync(context);
  57. }
  58. public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();
  59. /// <summary>
  60. /// 超时策略
  61. /// </summary>
  62. /// <param name="context"></param>
  63. /// <param name="evt"></param>
  64. public override void UserEventTriggered(IChannelHandlerContext context, object evt)
  65. {
  66. IdleStateEvent idleStateEvent = evt as IdleStateEvent;
  67. if (idleStateEvent != null)
  68. {
  69. if (idleStateEvent.State == IdleState.WriterIdle)
  70. {
  71. string channelId = context.Channel.Id.AsShortText();
  72. logger.LogInformation($"{idleStateEvent.State.ToString()}>>>Heartbeat-{channelId}");
  73. //发送主链路保持请求数据包
  74. var package = JT809BusinessType.主链路连接保持请求消息.Create();
  75. JT809Response jT809Response = new JT809Response(package, 100);
  76. context.WriteAndFlushAsync(jT809Response);
  77. }
  78. }
  79. base.UserEventTriggered(context, evt);
  80. }
  81. public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
  82. {
  83. string channelId = context.Channel.Id.AsShortText();
  84. logger.LogError(exception, $"{channelId} {exception.Message}");
  85. context.CloseAsync();
  86. }
  87. }
  88. }