Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

99 righe
3.6 KiB

  1. using DotNetty.Handlers.Timeout;
  2. using DotNetty.Transport.Channels;
  3. using Microsoft.Extensions.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace JT808.DotNetty.Handlers
  10. {
  11. internal class JT808ConnectionHandler : ChannelHandlerAdapter
  12. {
  13. private readonly ILogger<JT808ConnectionHandler> logger;
  14. private readonly JT808SessionManager jT808SessionManager;
  15. public JT808ConnectionHandler(
  16. JT808SessionManager jT808SessionManager,
  17. ILoggerFactory loggerFactory)
  18. {
  19. this.jT808SessionManager = jT808SessionManager;
  20. logger = loggerFactory.CreateLogger<JT808ConnectionHandler>();
  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. jT808SessionManager.RemoveSessionByID(channelId);
  43. base.ChannelInactive(context);
  44. }
  45. /// <summary>
  46. /// 服务器主动断开
  47. /// </summary>
  48. /// <param name="context"></param>
  49. /// <returns></returns>
  50. public override Task CloseAsync(IChannelHandlerContext context)
  51. {
  52. string channelId = context.Channel.Id.AsShortText();
  53. if (logger.IsEnabled(LogLevel.Debug))
  54. logger.LogDebug($"<<<{ channelId } The server disconnects from the client.");
  55. jT808SessionManager.RemoveSessionByID(channelId);
  56. return base.CloseAsync(context);
  57. }
  58. public override void ChannelReadComplete(IChannelHandlerContext context)
  59. {
  60. context.Flush();
  61. }
  62. /// <summary>
  63. /// 超时策略
  64. /// </summary>
  65. /// <param name="context"></param>
  66. /// <param name="evt"></param>
  67. public override void UserEventTriggered(IChannelHandlerContext context, object evt)
  68. {
  69. IdleStateEvent idleStateEvent = evt as IdleStateEvent;
  70. if (idleStateEvent != null)
  71. {
  72. string channelId = context.Channel.Id.AsShortText();
  73. logger.LogInformation($"{idleStateEvent.State.ToString()}>>>{channelId}");
  74. // 由于808是设备发心跳,如果很久没有上报数据,那么就由服务器主动关闭连接。
  75. jT808SessionManager.RemoveSessionByID(channelId);
  76. context.CloseAsync();
  77. }
  78. base.UserEventTriggered(context, evt);
  79. }
  80. public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
  81. {
  82. string channelId = context.Channel.Id.AsShortText();
  83. logger.LogError(exception,$"{channelId} {exception.Message}" );
  84. jT808SessionManager.RemoveSessionByID(channelId);
  85. context.CloseAsync();
  86. }
  87. }
  88. }