Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

101 Zeilen
3.7 KiB

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