Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

83 řádky
3.8 KiB

  1. using DotNetty.Buffers;
  2. using DotNetty.Codecs.Http;
  3. using DotNetty.Common.Utilities;
  4. using DotNetty.Transport.Channels;
  5. using JT808.DotNetty.Core.Handlers;
  6. using JT808.DotNetty.Core.Metadata;
  7. using Microsoft.Extensions.Logging;
  8. using System;
  9. using System.Text;
  10. namespace JT808.DotNetty.WebApi.Handlers
  11. {
  12. /// <summary>
  13. /// jt808 webapi服务
  14. /// 请求量不大,只支持JSON格式并且只支持post发数据
  15. /// ref: dotnetty HttpServer
  16. /// </summary>
  17. internal class JT808WebAPIServerHandler : SimpleChannelInboundHandler<IFullHttpRequest>
  18. {
  19. private static readonly AsciiString TypeJson = AsciiString.Cached("application/json");
  20. private static readonly AsciiString ServerName = AsciiString.Cached("JT808WebAPINetty");
  21. private static readonly AsciiString ContentTypeEntity = HttpHeaderNames.ContentType;
  22. private static readonly AsciiString DateEntity = HttpHeaderNames.Date;
  23. private static readonly AsciiString ContentLengthEntity = HttpHeaderNames.ContentLength;
  24. private static readonly AsciiString ServerEntity = HttpHeaderNames.Server;
  25. private readonly JT808MsgIdHttpHandlerBase jT808MsgIdHttpHandlerBase;
  26. private readonly ILogger<JT808WebAPIServerHandler> logger;
  27. public JT808WebAPIServerHandler(
  28. JT808MsgIdHttpHandlerBase jT808MsgIdHttpHandlerBase,
  29. ILoggerFactory loggerFactory)
  30. {
  31. this.jT808MsgIdHttpHandlerBase = jT808MsgIdHttpHandlerBase;
  32. logger = loggerFactory.CreateLogger<JT808WebAPIServerHandler>();
  33. }
  34. protected override void ChannelRead0(IChannelHandlerContext ctx, IFullHttpRequest msg)
  35. {
  36. if (logger.IsEnabled(LogLevel.Debug))
  37. {
  38. logger.LogDebug($"Uri:{msg.Uri}");
  39. logger.LogDebug($"Content:{msg.Content.ToString(Encoding.UTF8)}");
  40. }
  41. JT808HttpResponse jT808HttpResponse = null;
  42. if (jT808MsgIdHttpHandlerBase.HandlerDict.TryGetValue(msg.Uri,out var funcHandler))
  43. {
  44. jT808HttpResponse = funcHandler( new JT808HttpRequest() { Json = msg.Content.ToString(Encoding.UTF8)});
  45. }
  46. else
  47. {
  48. jT808HttpResponse = jT808MsgIdHttpHandlerBase.NotFoundHttpResponse();
  49. }
  50. if (jT808HttpResponse != null)
  51. {
  52. WriteResponse(ctx, Unpooled.WrappedBuffer(jT808HttpResponse.Data), TypeJson, jT808HttpResponse.Data.Length);
  53. }
  54. }
  55. private void WriteResponse(IChannelHandlerContext ctx, IByteBuffer buf, ICharSequence contentType, int contentLength)
  56. {
  57. // Build the response object.
  58. var response = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK, buf, false);
  59. HttpHeaders headers = response.Headers;
  60. headers.Set(ContentTypeEntity, contentType);
  61. headers.Set(ServerEntity, ServerName);
  62. headers.Set(DateEntity, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  63. headers.Set(ContentLengthEntity, contentLength);
  64. // Close the non-keep-alive connection after the write operation is done.
  65. ctx.WriteAndFlushAsync(response);
  66. ctx.CloseAsync();
  67. }
  68. public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
  69. {
  70. WriteResponse(context, Unpooled.WrappedBuffer(jT808MsgIdHttpHandlerBase.ErrorHttpResponse(exception).Data), TypeJson, jT808MsgIdHttpHandlerBase.ErrorHttpResponse(exception).Data.Length);
  71. logger.LogError(exception, exception.Message);
  72. context.CloseAsync();
  73. }
  74. public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();
  75. }
  76. }